Spring annotation accumulation and Spring annotation Accumulation
Use annotations to register beans with Spring containers.
Register in applicationContext. xml:
1 <context:component-scan base-package=”pagkage1[,pagkage2,…,pagkageN]”/>
For example, specify a package in base-package.
<context:component-scan base-package="cn.wind"/>
You can also specify multiple packages in <context: component-scan base-package = ""/>, for example:
<context:component-scan base-package="cn.wind.dao.impl,cn.wind.service.impl,cn.wind.action"/>
============
1: @ Repository is marked on the persistence layer, that is, dao, used to mark data access.
2: @ Service is marked at the business layer, that is, service, used to mark the business logic.
3: @ Controller is marked on the control layer.
4: @ Component when it is not clear that this class belongs to that layer, you can use @ Component for annotation, but it is not recommended to use
5: After @ Autowired is added to the property of @ Autowired, The getter () and setter () methods are not required, and Spring will automatically inject them.
@ Autowired (required = false) indicates that new ServiceImpl () is used if no bean of this type is found in the spring context ();
6: @ Qualifier when there are two implementation classes in the interface, you must use @ Qualifier to specify the implementation class to be injected. Otherwise, you can omit it and write only @ Autowired.
For example, define a Vehicle class: Vehicle. There are two sub-classes: Bus and Sedan. If you use @ Autowired to find Vehicle, there will be two matching options: Bus and Sedan. To limit the options, you can:
@Autowired @Qualifier("bus") private Vehicle vehicle;
The XXX in @ Qualifier ("XXX") is the Bean name, so when @ Autowired and @ Qualifier are used together, the automatic injection policy is changed from byType to byName.
7: similar to @ Service ("xXX"): This class is defined as a bean with the bean name xXX. The first letter is lowercase by default.
For example, if @ Service ("Student") is added to a student implementation class, a name is given to the instantiated Student object. This is how to differentiate implementation classes when one interface has multiple implementation classes.
For example, Student implements the Person interface. When @ Autowired Person in your controller, if there is another implementation class User in Person, in order to determine whether Student or User is instantiated, @ Service brackets are useful,
@Autowired @Qualifier("student") private Person person;
In this way, it is determined that a Person is instantiated and points to a Student object.
8: @ Resource
@ Resource is equivalent to @ Autowired, but @ Autowired is automatically injected by byType, while @ Resource is automatically injected by byName by default. @ Resource has two attributes, namely name and type. Spring resolves the name attribute of @ Resource annotation to the bean name, And the type attribute to the bean type. Therefore, if the name attribute is used, the automatic injection policy of byName is used, and the automatic injection policy of byType is used when the type attribute is used. If neither name nor type is specified, the byName automatic injection policy is used through reflection.
@ Resource Assembly Sequence
1) if both name and type are specified, a unique matching bean is found in the Spring context for assembly. If no matching bean is found, an exception is thrown.
2) If name is specified, query the bean with the matching name (id) in the context for assembly. If no name is found, an exception is thrown.
3) If type is specified, an exception will be thrown if a unique bean of type matching is found in the context for assembly and no or multiple beans can be found.
4) if neither name nor type is specified, the system automatically performs the Assembly in byName mode. If no match exists, the system returns to the original type for matching. If the match exists, the system automatically assembles the data;
9: @ PostConstruct, @ PreDestroy
These two annotations can only be applied to methods. The method labeled @ PostConstruct annotation will be called after the class is instantiated, and the method labeled @ PreDestroy will be called before the class is destroyed.
@PostConstruct public void init(){ System.out.println("I'm init method using @PostConstrut...."); } @PreDestroy public void dostory(){ System.out.println("I'm destory method using @PreDestroy....."); }