Training & Cer tification
Welcome To Ducat India
Language | Industrial Training | Digital Marketing |
Web Technology | Testing+ | Database | Networking |
Mobile Application | ERP | Graphic | Big Data | Cloud
Computing
Apply Now
Call us:
70-70-90-50-90
www.ducatindia.com
Spring – Java-based Container Configuration
The central artifacts in Spring’s new Java-configuration support are @Configuration-annotated classes and
@Bean-annotated methods.
The @Bean annotation is used to indicate that a method instantiates, configures, and initializes a new object to
be managed by the Spring IoC container. For those familiar with Spring’s XML configuration, the @Bean
annotation plays the same role as the element. You can use @Bean-annotated methods with any Spring
@Component. However, they are most often used with @Configuration beans.
Annotating a class with @Configuration indicates that its primary purpose is as a source of bean definitions.
Furthermore, @Configuration classes let inter-bean dependencies be defined by calling other @Bean methods
in the same class. The simplest possible @Configuration class reads as follows:
The below simple example show usage of @Bean and @Configuration annotations.
importorg.springframework.context.annotation.Bean;
importorg.springframework.context.annotation.Configuration;
importcom.companyname.projectname.customer.CustomerService;
importcom.companyname.projectname.order.OrderService;
@Configuration
public class Application {
@Bean
publicCustomerServicecustomerService() {
return new CustomerService();
}
@Bean
publicOrderServiceorderService() {
return new OrderService();
}
}
The preceding configuration is exactly equivalent to the following Spring XML:
< beans>
< bean id="customerService" class="com.companyname.projectname.CustomerService"/>
< bean id="orderService" class="com.companyname.projectname.OrderService"/>
< /beans>
Note that the method name and bean name in XML are exactly the same.
Instantiating the Spring Container by Using AnnotationConfigApplicationContext
Spring 3.0 introduced AnnotationConfigApplicationContext class which is implementation ApplicationContext
interface. It is capable of accepting not only @Configuration classes as input but also plain @Component classes
and classes annotated with JSR-330 metadata.
When @Configuration classes are provided as input, the @Configuration class itself is registered as a bean
definition and all declared @Bean methods within the class are also registered as bean definitions.
When @Component and JSR-330 classes are provided, they are registered as bean definitions, and it is assumed
that DI metadata such as @Autowired or @Inject is used within those classes where necessary.
Read More: https://tutorials.ducatindia.com/java/spring-java-based-container-configuration/

Spring – Java-based Container Configuration

  • 1.
    Training & Certification Welcome To Ducat India Language | Industrial Training | Digital Marketing | Web Technology | Testing+ | Database | Networking | Mobile Application | ERP | Graphic | Big Data | Cloud Computing Apply Now Call us: 70-70-90-50-90 www.ducatindia.com
  • 2.
    Spring – Java-basedContainer Configuration The central artifacts in Spring’s new Java-configuration support are @Configuration-annotated classes and @Bean-annotated methods. The @Bean annotation is used to indicate that a method instantiates, configures, and initializes a new object to be managed by the Spring IoC container. For those familiar with Spring’s XML configuration, the @Bean annotation plays the same role as the element. You can use @Bean-annotated methods with any Spring @Component. However, they are most often used with @Configuration beans. Annotating a class with @Configuration indicates that its primary purpose is as a source of bean definitions. Furthermore, @Configuration classes let inter-bean dependencies be defined by calling other @Bean methods in the same class. The simplest possible @Configuration class reads as follows: The below simple example show usage of @Bean and @Configuration annotations. importorg.springframework.context.annotation.Bean; importorg.springframework.context.annotation.Configuration; importcom.companyname.projectname.customer.CustomerService; importcom.companyname.projectname.order.OrderService;
  • 3.
    @Configuration public class Application{ @Bean publicCustomerServicecustomerService() { return new CustomerService(); } @Bean publicOrderServiceorderService() { return new OrderService(); } } The preceding configuration is exactly equivalent to the following Spring XML: < beans> < bean id="customerService" class="com.companyname.projectname.CustomerService"/> < bean id="orderService" class="com.companyname.projectname.OrderService"/> < /beans> Note that the method name and bean name in XML are exactly the same.
  • 4.
    Instantiating the SpringContainer by Using AnnotationConfigApplicationContext Spring 3.0 introduced AnnotationConfigApplicationContext class which is implementation ApplicationContext interface. It is capable of accepting not only @Configuration classes as input but also plain @Component classes and classes annotated with JSR-330 metadata. When @Configuration classes are provided as input, the @Configuration class itself is registered as a bean definition and all declared @Bean methods within the class are also registered as bean definitions. When @Component and JSR-330 classes are provided, they are registered as bean definitions, and it is assumed that DI metadata such as @Autowired or @Inject is used within those classes where necessary. Read More: https://tutorials.ducatindia.com/java/spring-java-based-container-configuration/