用Spring的Resource注解完成属性依赖注入

          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!完毕!  

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值