注解(Annotation)的使用
目录:
1、在一个类中引用其他对象
2、把一个类对象放到容器中
分为两种情况:
1、在一个类中引用其他对象
(1)@AutoWired 根据byType需找,使用@Qualifier("name") 来指定名字查找byName
样例:
<?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 /> <bean id="aDAO" class="com.spring.app.test.ADAO"/> <bean id="aService" class="com.spring.app.test.AService"></bean> </beans>
public class ADAO { public void save(){ System.out.println("this is save()..."); } }
public class AService { private ADAO aDAO; @Autowired public void setADAO(ADAO aDAO){ this.aDAO = aDAO; } public ADAO getADAO(){ return aDAO; } public static void main(String[] args){ ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); AService aService = (AService) context.getBean("aService"); aService.getADAO().save(); } }
beans.xml
<?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 /> <bean id="aDAO" class="com.spring.app.test.ADAO"/> <bean id="aDAO1" class="com.spring.app.test.ADAO"/> <bean id="aService" class="com.spring.app.test.AService"></bean> </beans>
public class AService { private ADAO aDAO; @Autowired public void setADAO(@Qualifier("aDAO1") ADAO aDAO){ this.aDAO = aDAO; } public ADAO getADAO(){ return aDAO; } public static void main(String[] args){ ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); AService aService = (AService) context.getBean("aService"); aService.getADAO().save(); } }(2)@Required:这个注解不是用来实现注入的,是用来在编译时,检查错误的
(3)@Resource 这个注解比较常用
@Resource的作用相当于@Autowired,只不过@Autowired按byType自动注入。
@Resource(这个注解属于J2EE的)建议使用这个注解,不使用@AutoWired(属于sping的)以减小与spring的耦合度。
网上许多资料都说是先byName匹配,再byType匹配,但是我试验了几次都没有成功
<?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 /> <bean id="aDAO" class="com.spring.app.test.ADAO"/> <bean id="bDAO1" class="com.spring.app.test.ADAO"/> <bean id="aService" class="com.spring.app.test.AService"></bean> </beans>
public class AService { private ADAO aDAO; @Resource(name="aDAO")//这个地方不写就会提示type匹配有两个抛异常 public void setADAO(ADAO aDAO){ this.aDAO = aDAO; } public ADAO getADAO(){ return aDAO; } public static void main(String[] args){ ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); AService aService = (AService) context.getBean("aService"); aService.getADAO().save(); } }
2、把一个类对象放到容器中
@Component @Repository @Service @Controller这四个目前为止功能是一样的,放到类上面,容器初始化时读取
<?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 /> <context:component-scan base-package="com.spring.app.test"></context:component-scan> <!-- <bean id="aDAO" class="com.spring.app.test.ADAO"/> --> <!-- <bean id="bDAO1" class="com.spring.app.test.ADAO"/> --> <bean id="aService" class="com.spring.app.test.AService"></bean> </beans>
@Component public class ADAO { public void save(){ System.out.println("this is save()..."); } }
@Component public class AService { private ADAO aDAO; @Resource public void setADAO(ADAO aDAO){ this.aDAO = aDAO; } public ADAO getADAO(){ return aDAO; } public static void main(String[] args){ ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); AService aService = (AService) context.getBean("aService"); aService.getADAO().save(); } @PostConstruct public void init(){ System.out.println("init....www"); } @PreDestroy public void destroy(){ System.out.println("destroy...."); } }
init....www
init....www
this is save()...
打印两次init....www是因为解析beans.xml时,发现有@Component所以对这个类进行了加载,还发现有
<bean id="aService" class="com.spring.app.test.AService"></bean>这句话,又加载一次。每次加载类型都会执行初始化方法,销毁时执行销毁方法。