Spring学习笔记(8)----属性注入的方式

Spring中属性注入的方式有三种:

1.使用属性setter方法注入

2.使用构造器注入

3.使用注解方式注入

 

使用属性setter方法注入

使用属性setter方法注入就是给属性添加set()方法,在前面都是使用这种方法。

Java代码
  1. package com.szy.spring.service;   
  2.   
  3. import com.szy.spring.dao.PersonDao;   
  4.   
  5. public class UserServiceImplBySetter implements UserService   
  6. {   
  7.     private PersonDao personDao;   
  8.        
  9.     public void show()   
  10.     {   
  11.         personDao.show();   
  12.     }   
  13.     public PersonDao getPersonDao()   
  14.     {   
  15.         return personDao;   
  16.     }   
  17.     public void setPersonDao(PersonDao personDao)   
  18.     {   
  19.         this.personDao = personDao;   
  20.     }   
  21. }  

然后在配置文件中如下配置

Xml代码
  1. <bean id="personDao" class="com.szy.spring.dao.PersonDaoBean"/>  
  2.     <!-- 使用属性Setter方法注入配置 -->  
  3.     <bean id="userService1" class="com.szy.spring.service.UserServiceImplBySetter">  
  4.         <property name="personDao" ref="personDao"></property>  
  5.     </bean>  

使用构造器注入

使用构造器注入就是在类中添加含参构造函数

Java代码
  1. package com.szy.spring.service;   
  2.   
  3. import com.szy.spring.dao.PersonDao;   
  4.   
  5. public class UserServiceImplConstructor implements UserService   
  6. {   
  7.     private PersonDao personDao;   
  8.     private String name;   
  9.        
  10.     public UserServiceImplConstructor()   
  11.     {   
  12.     }   
  13.   
  14.     public UserServiceImplConstructor(PersonDao personDao, String name)   
  15.     {   
  16.         this.personDao = personDao;   
  17.         this.name = name;   
  18.     }   
  19.   
  20.     public void show()   
  21.     {   
  22.         personDao.show();   
  23.         System.out.println("name属性:"+name);   
  24.     }   
  25. }  

下面就是在配置文件中添加配置信息,给每个参数注入值

Xml代码
  1. <bean id="personDao" class="com.szy.spring.dao.PersonDaoBean"/>  
  2.     <!-- 使用构造器参数方法注入配置 -->  
  3.     <bean id="userService2" class="com.szy.spring.service.UserServiceImplConstructor">  
  4.         <constructor-arg index="0" type="com.szy.spring.dao.PersonDao" ref="personDao"/>  
  5.         <constructor-arg index="1" value="Kuka"/>  
  6.     </bean>  

 注意:constructor-arg index是从0开始的

 

使用注解方式注入

如果使用前面的两种方法,配置文件将会显得很臃肿,因此我们可以使用注解的方式注入,使用注解方式注入有两种方法,第一种使用javax.annotation.Resource中提供的注解方式方法如下:

Java代码
  1. package com.szy.spring.service;   
  2.   
  3. import javax.annotation.Resource;   
  4.   
  5. import com.szy.spring.dao.PersonDao;   
  6.   
  7. public class UserServiceImplByAnnotation4Resource implements UserService   
  8. {   
  9.     //@Resource默认是按照名称装配,找不到与名称匹配的bean时按类型装配   
  10.     @Resource(name="personDao")private PersonDao personDao;   
  11.   
  12.     public void show()   
  13.     {   
  14.         personDao.show();   
  15.     }   
  16. //  下面方法同样可以   
  17. //  @Resource   
  18. //  public void setPersonDao(PersonDao personDao)   
  19. //  {   
  20. //      this.personDao = personDao;   
  21. //  }   
  22.        
  23. }  

此时配置文件要做相应的改变

Xml代码
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.     xmlns:context="http://www.springframework.org/schema/context"  
  5.     xmlns:tx="http://www.springframework.org/schema/tx"  
  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd   
  7.                 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd   
  8.                 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">  
  9.     <context:annotation-config/>  
  10.     <bean id="personDao" class="com.szy.spring.dao.PersonDaoBean"/>  
  11.     <bean id="userService" class="com.szy.spring.service.UserServiceImplByAnnotation4Autowired">  
  12.     </bean>  
  13. </beans>  

 注意添加这句配置信息

<context:annotation-config/>

第二中方式就是使用spring提供的注解方式

org.springframework.beans.factory.annotation.Autowired;

注入使用时需要导入spring目录lib\j2ee\common-annotations.jar这个包

使用方法如下:

Java代码
  1. package com.szy.spring.service;   
  2.   
  3. import org.springframework.beans.factory.annotation.Autowired;   
  4. import org.springframework.beans.factory.annotation.Qualifier;   
  5.   
  6. import com.szy.spring.dao.PersonDao;   
  7.   
  8. public class UserServiceImplByAnnotation4Autowired implements UserService   
  9. {   
  10.     //@Autowired默认使用类型进行装配,   
  11.     @Autowired private PersonDao personDao;   
  12. //  如果使用按名称进行装配,则需要如下   
  13. //  @Autowired @Qualifier("personDao")private PersonDao personDao;   
  14.     public void show()   
  15.     {   
  16.         personDao.show();   
  17.     }   
  18.        
  19. }  

配置文件和上面一样。

 

在使用时建议使用@Resource,因为@Resource不依赖于spring框架。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值