[Introduction to Spring Common Annotations]

1. Why to introduce annotations

The traditional Spring approach is to use .xml files to inject beans or configure aops and things. This has two disadvantages:

1. If all the content is configured in the .xml file, the .xml file will be very large; if the .xml file is separated according to the requirements, then the .xml file will be very large. In short, this will lead to very low readability and maintainability of configuration files.

2. It is a troublesome thing to constantly switch between .java files and .xml files during development, and this incoherence of thinking will also reduce the efficiency of development.

In order to solve these two problems, Spring has introduced annotations. Through the "@XXX" method, the annotations are closely integrated with Java Beans, which not only greatly reduces the size of configuration files, but also increases the readability and cohesion of Java Beans. .

 

2. Common Annotation Collection

@Configuration treats a class as an IoC container, and if @Bean is registered on one of its method headers, it will be used as a Bean in this Spring container.

@Scope annotation scope

@Lazy(true) means lazy initialization

@Service is used to mark business layer components, 

@Controller is used to mark control layer components (such as action in struts)

@Repository is used to annotate data access components, namely DAO components.

@Component generally refers to components. When components are not well classified, we can use this annotation to mark them.

@Scope is used to specify the scope scope (used on classes)

@PostConstruct is used to specify the initialization method (used on methods)

@PreDestory is used to specify the destruction method (used on the method)

@DependsOn: Defines the order in which beans are initialized and destroyed

@Primary: Autowiring When there are multiple bean candidates, the bean annotated as @Primary will be the first choice, otherwise an exception will be thrown

@Autowired is assembled by type by default. If we want to use assembly by name, it can be used in conjunction with the @Qualifier annotation. as follows:

@Autowired @Qualifier("personDaoBean") There are multiple instances to use together

@Resource is assembled by name by default, and will be assembled by type when no bean matching the name is found.

@PostConstruct initialization annotation

@PreDestroy destroys the default singleton of the annotation and loads it at startup

@Async asynchronous method call

 

 

3. Common annotations

1、@Autowired

@Autowired, as the name suggests, is automatic wiring, its function is to eliminate the getter/setter in the code Java code and the property in the bean property. Of course, the getter depends on personal needs. If private properties need to be provided externally, they should be reserved.

 

2. Qualifier (specify the name of the injected bean)

If there is more than one matching bean in the container, the name of the bean can be qualified by the @Qualifier annotation

 

 

3. Assembly order of @Resource:

(1), there is nothing after @Resource. By default, the name attribute is used to match the bean. If it cannot be found, then press the type to match.

(2) If the name or type is specified, the bean is matched according to the specified type

(3) If the name and type are specified, the beans are matched according to the specified name and type. Any mismatch will result in an error.

 

Then, distinguish the difference between the @Autowired and @Resource annotations:

(1), @Autowired defaults to match beans according to byType, and @Resource defaults to match beans according to byName

(2), @Autowired is the annotation of Spring, @Resource is the annotation of J2EE, when you look at the import annotation, the package names of these two annotations are clear

Spring belongs to a third party, and J2EE is Java's own thing. Therefore, it is recommended to use the @Resource annotation to reduce the coupling between the code and Spring.

 

4、@Component

Equivalent to a generic annotation, used when it is not known to which layer some classes belong, but not recommended.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326563362&siteId=291194637