In this article, we will cover how to use annotations in our Java code to configure the Spring container.
Basic Concepts: @Bean and @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.
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.companyname.projectname.customer.CustomerService;
import com.companyname.projectname.order.OrderService;
@Configuration
public class Application {
@Bean
public CustomerService customerService() {
return new CustomerService();
}
@Bean
public OrderService orderService() {
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.
We have separate in-depth articles of @Bean and @Configuration annotations here:
>> Spring @Bean Annotation with Example
>> Spring @Configuration Annotation with Example
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.
Create Spring IOC Container
We can use @Configuration classes as input when instantiating an AnnotationConfigApplicationContext. This allows for completely XML-free usage of the Spring container, as the following example shows:
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Application {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
MessageService messageService = context.getBean(MessageService.class);
messageService.setMessage("TwitterMessageService Implementation");
System.out.println(messageService.getMessage());
MessageService messageService1 = context.getBean(MessageService.class);
System.out.println(messageService1.getMessage());
context.close();
}
}
AnnotationConfigApplicationContext is not limited to working only with @Configuration classes. Any @Component or JSR-330 annotated class may be supplied as input to the constructor, as the following example shows:
public static void main(String[] args) {
ApplicationContext ctx = new AnnotationConfigApplicationContext(MyServiceImpl.class, Dependency1.class, Dependency2.class);
MyService myService = ctx.getBean(MyService.class);
myService.doStuff();
}
Building the Container Programmatically by Using register(Class<?>… )
You can instantiate an AnnotationConfigApplicationContext by using a no-arg constructor and then configure it by using the register() method. This approach is particularly useful when programmatically building an AnnotationConfigApplicationContext. The following example shows how to do so:
public static void main(String[] args) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.register(AppConfig.class, OtherConfig.class);
ctx.register(AdditionalConfig.class);
ctx.refresh();
MyService myService = ctx.getBean(MyService.class);
myService.doStuff();
}
Enabling Component Scanning with scan(String… )
To enable component scanning, you can annotate your @Configuration class as follows:
package net.javaguides.spring.scope;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan(basePackages = "net.javaguides.spring")
public class AppConfig {
}
Use @ComponentScan annotation to enable auto component scanning in java based configuration, annotate your @Configuration class as shown above code sample.
In-depth Java-based Container Configuration Annotations
>> Spring Java Based Configuration Example
In this post, we will demonstrate the usage of Spring Java-based container configuration.
>> Spring @Bean Annotation with Example
In this article, we will discuss Spring Java configuration based @Bean annotation with examples. We will also discuss different scenarios of how and when to use @Bean annotation.
>> Spring @Configuration Annotation with Example
In this article, we will discuss a very important Spring Java based configuration annotation that is a @Configuration annotation with examples.
In this post, we will demonstrate the usage of Spring Java-based container configuration.
>> Spring @Bean Annotation with Example
In this article, we will discuss Spring Java configuration based @Bean annotation with examples. We will also discuss different scenarios of how and when to use @Bean annotation.
>> Spring @Configuration Annotation with Example
In this article, we will discuss a very important Spring Java based configuration annotation that is a @Configuration annotation with examples.
In Spring, you can use @PropertySource annotation to externalize your configuration to a properties file. In this article, we will discuss how to use @PropertySource to read a properties file and display the values with @Value and Environment.
>> Spring @Import Annotation with Example
In this article, we will discuss how to use Spring @Import annotation with examples.
>> Spring @ImportResource Annotation Example
In this article, we will quickly discuss how to use @ImportResource annotation with a simple Spring boot application.
In this article, we will discuss how to use Spring @Import annotation with examples.
>> Spring @ImportResource Annotation Example
In this article, we will quickly discuss how to use @ImportResource annotation with a simple Spring boot application.
Comments
Post a Comment
Leave Comment