Annotation: is a class using @ annotation name Under development: using annotations to replace the xml configuration file
@Component use case
@component取代:<bean class="">
Add a @Component on UserServiceImpl
Bean5.xml configuration, using this method requires opening annotations and configuring the location of annotation scanning
Use and effect (successfully injected value)
@Component("id") Use Case
@Component(“id”)取代: <bean id="" class="">
Add a @Component("userService") on UserServiceImpl
The beans5.xml configuration is the same as above, just open the annotation and configure the annotation position
Use and effect (value injection is also successful)
Web-service-dao configuration without annotations
Dao implementation class (simple to use, no database for the time being)
Service implementation class (userDao is assigned through spring)
The use of web-action (userService is assigned via spring)
beans6.xml configuration
Use and effect
Annotated web-service-dao configuration
For web development, provide 3 @Component annotation derivative annotations (the same function) instead of
<bean class=""> @Repository("name"): dao layer @Service("name"): service layer @Controller("name") : Web layer
@Autowired: automatically inject according to the type @Qualifier("name"): specify the id name of the automatic injection @Resource(name="name"): the combination of @Autowired and @Qualifier("name")
@Scope(" prototype”): multiple cases, the default is singleton
Use @Repository annotation on dao implementation class
Use @Service annotation on the service implementation class, and use @Autowired on userDao to automatically inject according to type
Use @Controller annotation on UserAction to indicate the web layer
For beans7.xml configuration, just turn on the annotation and configure the scan annotation
Same effect, easy and fast to use
Use of @Qualifier("name") (specify the id name to be automatically injected) In UserAction: In UserServiceImpl:
Use of @Resource(name="name")
The use of @Scope("prototype") (multiple cases, the default is singleton) is not configured, the default is single column, the UserAction class object is obtained from the spring container,
after the same configuration, there are multiple cases, each time Will return a new object, which is
equivalent to the single column and multiple instances previously configured in beans.xml
The use of @ PostConstruct custom initialization and @ PreDestroy custom destruction is configured in the implementation class of UserDao:
pay attention to the use of context to call the method of closing the container to execute the custom destruction method