spring学习之属性注入
首先准备工作
本项目的目录结构如下:
spring中属性的相互关系是通过applicationContext.xml来管理的,spring提倡面向接口的编程,因此在dao层使用接口抽象方法。
下面是各层的代码:
public interface StudentsDAO {
//保存学生
public boolean saveStudents(Students s);
}
接口的实现类。
public class StudentsDAOImpl implements StudentsDAO {
@Override
public boolean saveStudents(Students s) {
if(s!=null)
{
System.out.println("学号"+s.getSid());
System.out.println("姓名"+s.getSname());
System.out.println("年龄"+s.getAge());
return true;
}
else
{
return false;
}
}
}
public class Students {
private String sid;
private String sname;
private int age;
public Students() {
super();
}
public Students(String sid, String sname, int age) {
super();
this.sid = sid;
this.sname = sname;
this.age = age;
}
public String getSid() {
return sid;
}
public void setSid(String sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
一定要实现get和set方法,不然在applicationContext.xml中无法实现属性的注入。
public class StudentsService {
private StudentsDAO sDAO;
private Students s;
public StudentsDAO getsDAO() {
return sDAO;
}
public void setsDAO(StudentsDAO sDAO) {
this.sDAO = sDAO;
}
public Students getS() {
return s;
}
public void setS(Students s) {
this.s = s;
}
//保存学生
public boolean save(){
if(sDAO.saveStudents(s))
{
return true;
}
else
{
return false;
}
}
}
接着我们来说明spring的 第一种注入方式 setter注入
applicationContext.xml文件如下:
bean studentsService这个类依赖于属性s和sDAO,而s和sDAO又是对bean students和studentsDAO的引用。它们的注入是通过类studentsService中students和studentsDAO的set方法注入进来的。
<?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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
">
<context:annotation-config/>
<bean name="students" class="com.qzp.model.Students"></bean>
<bean name="studentsDAO" class="com.qzp.dao.impl.StudentsDAOImpl"></bean>
<bean name="studentsService" class="com.qzp.service.StudentsService">
<property name="s" ref="students"></property>
<property name="sDAO" ref="studentsDAO"></property>
</bean>
</beans>
附上测试方法:
public class TestStudentsService extends TestCase{
public void testSaveStudents(){
ApplicationContext cxt=new ClassPathXmlApplicationContext("applicationContext.xml");
Students s=(Students)cxt.getBean("students");
s.setSid("001");
s.setSname("qzp");
s.setAge(25);
StudentsService sService=(StudentsService)cxt.getBean("studentsService");
//使用断言,如果二者相等,通过
Assert.assertEquals(true, sService.saveStudents());
}
}
第二种注入方式:构造方法注入
在applicationContext.xml的students bean中添加如下的三个值,即可完成构造方法的注入。
如果是string和int类型可以不写,它会帮助你进行类型转换。
<?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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
">
<bean name="students" class="com.qzp.model.Students">
<constructor-arg index="0" type="java.lang.String" value="001"/>
<constructor-arg index="1" value="qzp"/>
<constructor-arg index="2" value="25"/>
</bean>
<bean name="studentsDAO" class="com.qzp.dao.impl.StudentsDAOImpl"></bean>
<bean name="studentsService" class="com.qzp.service.StudentsService">
<property name="s" ref="students"></property>
<property name="sDAO" ref="studentsDAO"></property>
</bean>
</beans>
测试代码如下,
此时就不需要在实例化bean时一个个set值进去了。(对比第一种的测试方法)
public class TestStudentsService extends TestCase{
public void testSaveStudents(){
ApplicationContext cxt=new ClassPathXmlApplicationContext("applicationContext.xml");
StudentsService sService=(StudentsService)cxt.getBean("studentsService");
//使用断言,如果二者相等,通过
Assert.assertEquals(true, sService.saveStudents());
}
}
第三种 使用注解注入
好处不比写一堆长长的applicationContext.xml文件
1.修改applicationContext.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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
">
<!-- 加入对注解的支持 -->
<context:annotation-config/>
<bean name="students" class="com.qzp.model.Students"></bean>
<bean name="studentsDAO" class="com.qzp.dao.impl.StudentsDAOImpl"></bean>
<bean name="studentsService" class="com.qzp.service.StudentsService"></bean>
</beans>
2.修改studentsService类
其中有两种方法一种是@Resource,另一种是@Autowired
使用@Resource(name="students"),通过name来指定对应的bean的名称。
public class StudentsService {
//name对应applicationContext中bean的name
@Resource(name="studentsDAO")
private StudentsDAO sDAO;
@Resource(name="students")
private Students s;
public StudentsDAO getsDAO() {
return sDAO;
}
public void setsDAO(StudentsDAO sDAO) {
this.sDAO = sDAO;
}
public Students getS() {
return s;
}
public void setS(Students s) {
this.s = s;
}
//保存学生
public boolean saveStudents(){
if(sDAO.saveStudents(s))
{
return true;
}
else
{
return false;
}
}
}
或者使用@Autowired,他会按照类型来匹配使用@Qualfer("students")来指定bean的名称
public class StudentsService {
//name对应applicationContext中bean的name
//@Resource(name="studentsDAO")
//按照类型来匹配,因为它们的类型不同,
@Autowired
@Qualifier("studentsDAO")
private StudentsDAO sDAO;
//@Resource(name="students")
@Autowired
@Qualifier("students")
private Students s;
public StudentsDAO getsDAO() {
return sDAO;
}
public void setsDAO(StudentsDAO sDAO) {
this.sDAO = sDAO;
}
public Students getS() {
return s;
}
public void setS(Students s) {
this.s = s;
}
//保存学生
public boolean saveStudents(){
if(sDAO.saveStudents(s))
{
return true;
}
else
{
return false;
}
}
}
3.测试
public class TestStudentsService extends TestCase{
public void testSaveStudents(){
ApplicationContext cxt=new ClassPathXmlApplicationContext("applicationContext.xml");
StudentsService sService=(StudentsService)cxt.getBean("studentsService");
//使用断言,如果二者相等,通过
Assert.assertEquals(true, sService.saveStudents());
}
}
测试成功。