I’m participating in nuggets Creators Camp # 4, click here to learn more and learn together!
preface
Hi, everybody. I’m Helu.
Recently, an intern colleague in the company was looking at the project code and found that some classes use @Configuration annotations, some classes use @Component annotations, some places use @bean annotations, and some use @Autowire annotations. Aren’t these annotations registered as Bean components? What’s the difference?
So this article will take a look at common spring annotations and their differences.
What are the annotations that declare a class as a Spring bean?
-
Component: generic annotation for any spring-class Component. If a Bean does not know which layer it belongs to, it can be annotated using the @Component annotation.
-
@Configuration: Declares this class as a Configuration class in which one or more @bean methods can be declared.
-
@Controller: corresponds to the Spring MVC control layer, which is mainly used to accept user requests and call the Service layer to return data to the front-end page.
-
@service: corresponding to the Service layer, mainly designed some complex logic, need to use the Dao layer.
-
@repository: Corresponds to the persistence layer (Dao layer), which is used for database-related operations.
What is the difference between @Component and @Configuration annotations?
Both of these are configuration class annotations that apply to a class and declare it to be a component. The differences are: 1. @Component is a meta-annotation that can annotate other class annotations. The @Configuration annotation is also decorated by the @Component annotation.
2. Bean sets different class properties.
- Full if it is @Configuration and the property proxyBeanMethods is true (the default)
- If @Component @ComponentScan @import @importSource is Lite
3. The returned bean instance is different.
- Class modified by the @Configuration annotation, and the value of the proxyBeanMethods attribute in the annotation is true (the default), the Cglib dynamic proxy is used to create a proxy class for the bean that intercepts all methods modified by the @Bean. In the intercepted method logic, the required singleton is returned from the container.
- Class modified by the @Component annotation will not create a proxy class for the bean. Then we will execute the user’s method directly, so we will return a new object each time.
The code is as follows (example) : a Java class
/ * * *@authorWhen leave *@descriptionPlain Java classes *@date2022/2/22 * /
@Data
public class Hello {
private String name;
}
Copy the code
@ the Configuration notes
/ * * *@authorWhen leave *@descriptionuse@ConfigurationNote *@date2022/2/22 * /
@Configuration
public class MyConfig {
@Bean
public Hello hello(a){
return newHello(); }}Copy the code
@ Component annotation class
/ * * *@authorWhen leave *@descriptionuse@ComponentAnnotation. *@date2022/2/22 * /
@Component
public class MyComponent {
@Bean
public Hello hello(a){
return newHello(); }}Copy the code
The test class
public class Test {
public static void main(String[] args) {
// Create and initialize the container
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(MyConfig.class);
// We take the Hello object directly from the container
System.out.println(context.getBean("hello", Hello.class));
MyConfig myConfig = context.getBean("myConfig", MyConfig.class);
Get the hello object by calling hello() in the config object
System.out.println(myConfig.hello());
AnnotationConfigApplicationContext contextCom = new AnnotationConfigApplicationContext(MyComponent.class);
// We take the Hello object directly from the container
System.out.println(contextCom.getBean("hello", Hello.class));
MyComponent myComponent = contextCom.getBean("myComponent", MyComponent.class);
Get the Hello object by calling Hello () in the Component objectSystem.out.println(myComponent.hello()); }}Copy the code
Result: You can see that the @Configuration annotation returns the same bean object and the @Component annotation returns a different object.
What is the difference between @Autowire and @Resource annotations?
1. @Autowire is provided by Spring, @resource is provided by J2EE;
By default, @autowire must require the dependent object to exist. To allow null values, set its required property to false.
3. @Resource is assembled by name by default and by type when no bean matching the name can be found. The name can be specified by the name attribute. If the name attribute is not specified, the annotation defaults to the field name when written to the field, and the annotation defaults to the property name when written to setter methods for assembly.
What is the difference between @qualifier and @primary annotations?
When you have multiple implementation classes for an interface, you register your implementation classes with @Component, but use the @AutoWired annotation for injection. The problem is that Spring doesn’t know which one to inject. Now you can fix it with the following two annotations.
- @qualifier is declared before it is used. It is like having multiple implementations with different names. Tell me which one you want to inject.
- @primary Priority scenario, annotated implementation, priority injected.
Note that the @qualifier annotation is used with the @autoWired annotation, and the @primary annotation is used with the @bean or @Component annotation. If both @qualifier and @primary annotations exist, The @qualifier annotation takes precedence. Basically, @primary defines the default value, while @qualifier is very specific.
The @qualifier annotation uses the following code:
@Autowired
@Qualifier("stringRedisTemplate")
private RedisTemplate<String, String> redisTemplate;
Copy the code
The @primary annotation uses the following code:
@Bean
@Primary
public Employee johnEmployee(a) {
return new Employee("john");
}
Copy the code
conclusion
That’s the end of this article. Thank you for reading and feel free to leave a comment in the comments section