You are on page 1of 2

Q1.

what is bean and scope of bean


it is nothing but pojo class,which have all feilds private.
• It should have a no-arg constructor.
• It should be Serializable.

• It should provide methods to set and get the values of the properties, known as getter and setter methods.

Bean Scopes refers to the lifecycle of Bean that means when the object of Bean will be instantiated, how long does that
object live, and how many objects will be created for that bean throughout. Basically, it controls the instance creation of
the bean and it is managed by the spring container.
Bean Scopes in Spring
The spring framework provides five scopes for a bean. We can use three of them only in the context of web-
aware Spring ApplicationContext and the rest of the two is available for both IoC container and Spring-MVC container.
The following are the different scopes provided for a bean:

1. Singleton: Only one instance will be created for a single bean definition per Spring IoC container and the same
object will be shared for each request made for that bean.

2. Prototype: A new instance will be created for a single bean definition every time a request is made for that
bean.

3. Request: A new instance will be created for a single bean definition every time an HTTP request is made for
that bean. But Only valid in the context of a web-aware Spring ApplicationContext.

4. Session: Scopes a single bean definition to the lifecycle of an HTTP Session. But Only valid in the context of a
web-aware Spring ApplicationContext.

5. Global-Session: Scopes a single bean definition to the lifecycle of a global HTTP Session. It is also only valid
in the context of a web-aware Spring ApplicationContext.

Q2.what is @componentscan,@ configuration


@ComponentScan -annotation enables component scanning in
Spring.@ComponentScan without arguments tells Spring to scan the current
package and all of its sub-packages.
@ComponentScan(basePackages = "com.zetcode")
@Configuration

@Configuration:-One of the most important annotations in spring is @Configuration annotation which indicates that
the class has @Bean definition methods. So Spring container can process the class and generate Spring Beans to be
used in the application

Q.3which annotation u have to used in spring


@Required: It applies to the bean setter method. It indicates that the annotated bean must be populated at configuration time
with the required property, else it throws an exception BeanInitilizationException.

@Autowired: Spring provides annotation-based auto-wiring by providing @Autowired annotation. It is used to autowire
spring bean on setter methods, instance variable, and constructor.
@Configuration: It is a class-level annotation. The class annotated with @Configuration used by Spring Containers as a
source of bean definitions.
@Bean: It is a method-level annotation. It is an alternative of XML <bea
n> tag. It tells the method to produce a bean to be managed by Spring Container
@Component: It is a class-level annotation. It is used to mark a Java class as a bean.
@Controller: The @Controller is a class-level annotation. It is a specialization of @Component. It marks a class as a web
request handler
@Service: It is also used at class level. It tells the Spring that class contains the business logic.
@Repository: It is a class-level annotation. The repository is a DAOs (Data Access Object) that access the database directly.
The repository does all the operations related to the database.

Q4what is @controller
@Controller annotation indicates that a particular class serves the role of a controller. Spring Controller annotation is
typically used in combination with annotated handler methods based on the @RequestMapping annotation. It can be
applied to classes only. It’s used to mark a class as a web request handler.
@qualifier -A qualifier is an annotation that you apply to a bean.The @Qualifier annotation is used to resolve the
autowiring conflict, when there are multiple beans of same type

You might also like