Spring中属性注入的方式有三种:
1.使用属性setter方法注入
2.使用构造器注入
3.使用注解方式注入
使用属性setter方法注入
使用属性setter方法注入就是给属性添加set()方法,在前面都是使用这种方法。
package com.szy.spring.service;
import com.szy.spring.dao.PersonDao;
public class UserServiceImplBySetter implements UserService
{
private PersonDao personDao;
public void show()
{
personDao.show();
}
public PersonDao getPersonDao()
{
return personDao;
}
public void setPersonDao(PersonDao personDao)
{
this.personDao = personDao;
}
}
然后在配置文件中如下配置
<bean id="personDao" class="com.szy.spring.dao.PersonDaoBean"/> <!-- 使用属性Setter方法注入配置 --> <bean id="userService1" class="com.szy.spring.service.UserServiceImplBySetter"> <property name="personDao" ref="personDao"></property> </bean>
使用构造器注入
使用构造器注入就是在类中添加含参构造函数
package com.szy.spring.service;
import com.szy.spring.dao.PersonDao;
public class UserServiceImplConstructor implements UserService
{
private PersonDao personDao;
private String name;
public UserServiceImplConstructor()
{
}
public UserServiceImplConstructor(PersonDao personDao, String name)
{
this.personDao = personDao;
this.name = name;
}
public void show()
{
personDao.show();
System.out.println("name属性:"+name);
}
}
下面就是在配置文件中添加配置信息,给每个参数注入值
<bean id="personDao" class="com.szy.spring.dao.PersonDaoBean"/> <!-- 使用构造器参数方法注入配置 --> <bean id="userService2" class="com.szy.spring.service.UserServiceImplConstructor"> <constructor-arg index="0" type="com.szy.spring.dao.PersonDao" ref="personDao"/> <constructor-arg index="1" value="Kuka"/> </bean>
注意:constructor-arg index是从0开始的
使用注解方式注入
如果使用前面的两种方法,配置文件将会显得很臃肿,因此我们可以使用注解的方式注入,使用注解方式注入有两种方法,第一种使用javax.annotation.Resource中提供的注解方式方法如下:
package com.szy.spring.service;
import javax.annotation.Resource;
import com.szy.spring.dao.PersonDao;
public class UserServiceImplByAnnotation4Resource implements UserService
{
//@Resource默认是按照名称装配,找不到与名称匹配的bean时按类型装配
@Resource(name="personDao")private PersonDao personDao;
public void show()
{
personDao.show();
}
// 下面方法同样可以
// @Resource
// public void setPersonDao(PersonDao personDao)
// {
// this.personDao = personDao;
// }
}
此时配置文件要做相应的改变
<?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" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-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="com.szy.spring.dao.PersonDaoBean"/> <bean id="userService" class="com.szy.spring.service.UserServiceImplByAnnotation4Autowired"> </bean> </beans>
注意添加这句配置信息
<context:annotation-config/>
第二中方式就是使用spring提供的注解方式
org.springframework.beans.factory.annotation.Autowired;
注入使用时需要导入spring目录lib\j2ee\common-annotations.jar这个包
使用方法如下:
package com.szy.spring.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import com.szy.spring.dao.PersonDao;
public class UserServiceImplByAnnotation4Autowired implements UserService
{
//@Autowired默认使用类型进行装配,
@Autowired private PersonDao personDao;
// 如果使用按名称进行装配,则需要如下
// @Autowired @Qualifier("personDao")private PersonDao personDao;
public void show()
{
personDao.show();
}
}
配置文件和上面一样。
在使用时建议使用@Resource,因为@Resource不依赖于spring框架。
本文介绍了Spring框架中的三种依赖注入方式:使用属性setter方法注入、使用构造器注入和使用注解方式注入。通过具体实例展示了每种注入方式的实现方法及配置。
823

被折叠的 条评论
为什么被折叠?



