bean的注入方式有3种:
第一种:使用构造器注入
第二钟:使用属性setter方法注入
第三种:使用Field注入(用于注解方式)
下面使用注解的方式完成bean的注入
java代码注入配置,需要spring解压文件夹下lib/j2ee/common-annotation.jar这个库文件,添加玩以后,修改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/> </beans> 引入注解命名空间和标签 xmlns:context="http://www.springframework.org/schema/context" http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd 打开注解 <context:annotation-config/> 如果要出现标签提示,那么和前面一样在本地添加spring-context-2.5.xsd 现在配置工作完成了,现在开始用java代码来完成注入 @Autowired方式:默认按类型装配,默认情况下它要求以来对象必须存在,如果允许null值,可以设置它required属性为false。 如果我们想使用按名称装配,可以结合@Qualifier注解一起使用 @Autowired @Qualifier("personDaoBean") private PersonDao personDao; @Resource方式:默认按名称装配,名称可以通过@Resource的name属性指定,如果没有指定的name属性,当注解标注在字段上, 即默认取字段的名称作为bean名称寻找以来对象,当注解标注在属性的setter方法上,即迷人取属性名作为bean名称寻找以来对象。 @Resource(name="personDaoBean") private PersonDao personDao; 推荐使用@Resource方式,因为@Resource是j2ee里的一个注解,而@AutoWired是spring里的注解,使用@Resource可以降低与框架 的耦合度。 完整配置 <?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="persondao" class="cn.itcast.service.impl.PersonDaoBean"></bean> <bean id="personservicebean" class="cn.itcast.service.impl.PersonServiceBean"></bean> </beans> 实体类 package cn.itcast.service.impl; import javax.annotation.Resource; import cn.itcast.service.PersonDao; import cn.itcast.service.PersonService; public class PersonServiceBean implements PersonService { private PersonDao persondao; private String name; public PersonServiceBean(){} public void save(){ System.out.println("我是save()方法"); this.persondao.add(); //System.out.println("name----------->"+name); } public PersonDao getPersondao() { return persondao; } @Resource public void setPersondao(PersonDao persondao) { this.persondao = persondao; } public String getName() { return name; } public void setName(String name) { this.name = name; } public PersonServiceBean(PersonDao persondao, String name) { this.persondao = persondao; //this.name = name; } } 这里的注解是用在set方法上,可以用在属性声明部分 @Resource private PersonDao persondao; 实现效果一样 通过测试 注意:1 使用注解的实体类中,如果存在一个有参的构造方法,那么必须重写一个无参的构造方法 如果再实体类中,没有构造方法,其构造方法是缺省的无参的构造方法,但是有了有参的构造方法, spring在实例化的时候,会找不到无参的构造方法,因为有参的构造方法把缺省的构造方法给覆盖了 所以这时要显性的写上无参的构造方法 否则在spring初始化的时候会报错 2 这两个注解的区别:在java代码中使用@Autowired或@Resource注解方式进行装配,这两个注解的区别是: @Autowired 默认按类型装配,@Resource默认按名称装配,当找不到与名称匹配的bean才会按类型装配。 end!完毕!