Spring entry Blog [5. Spring container build annotation @ Component and Resouces implement complete annotation configuration]

Source: Internet
Author: User

[Java]
@ Resource and @ Component implement zero XML configuration
 
 
1. @ Resource annotation:
@ Resource is the annotation of J2EE. It means to find the corresponding resources in the container. You can also specify its name resource through the name attribute. It can be annotated to field or setter.
Public class UserAction {
Private UserService userService;

@ Resource (name = "userService ")
Public void setUserService (UserService userService ){
This. userService = userService;
}
Public void addUser (){
UserService. HelloWorld ();
}
}
 
 
2. @ Component
@ Component and <context: component-scan base-package = "com. spring "> </context: component-scan> is used in combination to implement non-XML configuration. You can only configure annotations and add classes to resource containers.
 
Case Analysis:
1. xml file: configure the container resource scan package
<? Xml version = "1.0" encoding = "UTF-8"?>
<Beans xmlns = "http://www.springframework.org/schema/beans"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xmlns: context = "http://www.springframework.org/schema/context"
Xsi: schemaLocation = "http://www.springframework.org/schema/beans
Http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
Http://www.springframework.org/schema/context
Http://www.springframework.org/schema/context/spring-context-2.5.xsd>

<Context: annotation-config/>
<! -- Configure the container resource scan package -->
<Context: component-scan base-package = "com. spring"> </context: component-scan>
</Beans>
 
2. Java file:
Annotation of the resource @ Component. The name attribute of the saved container is userService.
In the future, we can use getBean ("userService. If we do not specify userService, the key of the bean to be stored in the container is userServiceImpl by default, so that we can get a container containing the UserServiceImpl object.
Package com. spring. service;
Import org. springframework. stereotype. Component;
Import com. spring. dao. UserDao;
 
@ Component ("userService ")
Public class UserServiceImpl implements UserService {
Private UserDao userDao;
Public void init (){
System. out. println ("init ");
}
Public void destory (){
System. out. println ("destory ");
}
Public UserDao getUserDao (){
Return userDao;
}
Public void setUserDao (UserDao userDao ){
This. userDao = userDao;
}
Public void HelloWorld (){
System. out. println ("helloworld ");
}
}
 
Inject resources through @ Resource
Because the resources in the container have been configured above. So here I will inject resources to related attributes through the @ Resource annotation. See the Code:
@ Component ("userAction ")
Public class UserAction {
Private UserService userService;

// It adds the xml file configuration of beans
// You can also specify byName using the name attribute
@ Resource (name = "userService ")
Public void setUserService (UserService userService ){
This. userService = userService;
}
Public void addUser (){
UserService. HelloWorld ();
}
}
 
 
Test implementation:
Use the userAction case configured above to obtain this object and then call the method in it, because the @ Component annotation is used to configure the UserAction object in the container. So after obtaining the container, you can use this method to getbean.
 
@ Test
Public void test01 (){
BeanFactory applicationContext = new ClassPathXmlApplicationContext ("beans. xml ");
UserAction user = (UserAction) applicationContext. getBean ("userAction ");
User. addUser ();
}
 
 
@ Scope and @ PostConstruct, @ PreDestroy
 
It is very simple. It is equivalent to the initmethod and destorymethod mentioned above. Please refer to the following code. Not to mention:
@ Scope ("Singleton ")
@ Component ("userService ")
Public class UserServiceImpl implements UserService {
Private UserDao userDao;
@ PostConstruct
Public void init (){
System. out. println ("init ");
}
@ PreDestroy
Public void destory (){
System. out. println ("destory ");
}
Public UserDao getUserDao (){
Return userDao;
}
Public void setUserDao (UserDao userDao ){
This. userDao = userDao;
}
Public void HelloWorld (){
System. out. println ("helloworld ");
}
}
Author: zhang6622056

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.