关于Spring的属性注入一共有两种方式,分别为XML方式与注解方式,其中XML方式又分为Set注入与构造方法注入,注解方式分为java注解与Spring注解。根据需要新建java project,项目结构与源代码如下:(可以通过本文结尾提供的链接下载
Students.java
package com.spr.vo;
public class Students {
private String sid;
private String sname;
private int sage;
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 getSage() {
return sage;
}
public void setSage(int sage) {
this.sage = sage;
}
}
StudentsDAO.java
package com.spr.dao;
import com.spr.vo.Students;
public interface StudentsDAO {
public boolean saveStudents(Students s);
}
StudentsDAOImpl.java
package com.spr.dao;
import com.spr.vo.Students;
public class StudentsDAOImpl implements StudentsDAO{
@Override
public boolean saveStudents(Students students) {
// TODO Auto-generated method stub
//在此模拟数据存入数据库
if(students != null){
System.out.println("学号:"+students.getSid());
System.out.println("姓名:"+students.getSname());
System.out.println("年龄:"+students.getSage());
System.out.println("数据保存成功!");
return true;
}
else{
return false;
}
}
}
StudentsService.java
package com.spr.service;
public interface StudentsService {
public boolean saveStudents();
}
StudentsServiceImpl.java
package com.spr.service;
import com.spr.dao.StudentsDAO;
import com.spr.vo.Students;
public class StudentsServiceImpl implements StudentsService{
private StudentsDAO studentsDAO;
private Students students;
@Override
//保存学生(调用DAO层方法)
public boolean saveStudents() {
// TODO Auto-generated method stub
if(studentsDAO.saveStudents(students)){
return true;
}
else{
return false;
}
}
}
TestSaveStudents.java
package com.spr.test;
public class TestSaveStudents {
public static void main(String[] args){
//稍后完善
}
}
其中,在StudentsServiceImpl.java中,仅仅拥有studentsDAO与students两个属性,但是并没有对其实例化,如果直接引用,在运行程序时会抛出空指针异常,所有我们要用Spring为其注入。
一、Set属性注入:
首先在StudentsServiceImpl.java中生成属性的set/get方法,代码修改后为:
StudentsServiceImpl.java
package com.spr.service;
import com.spr.dao.StudentsDAO;
import com.spr.vo.Students;
public class StudentsServiceImpl implements StudentsService{
private StudentsDAO studentsDAO;
private Students students;
public StudentsServiceImpl(){
}
public StudentsServiceImpl(StudentsDAO studentsDAO){
this.studentsDAO = studentsDAO;
}
@Override
//保存学生(调用DAO层方法)
public boolean saveStudents() {
// TODO Auto-generated method stub
if(studentsDAO.saveStudents(students)){
return true;
}
else{
return false;
}
}
//生成的Set/get方法
public StudentsDAO getStudentsDAO() {
return studentsDAO;
}
public void setStudentsDAO(StudentsDAO studentsDAO) {
this.studentsDAO = studentsDAO;
}
public Students getStudents() {
return students;
}
public void setStudents(Students students) {
this.students = students;
}
}
编写applicationContext.xml文件:
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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
<bean name="students" class="com.spr.vo.Students"></bean>
<bean name="studentsDAO" class="com.spr.dao.StudentsDAOImpl"></bean>
<bean name="studentsService" class="com.spr.service.StudentsServiceImpl">
<!-- 1、Setter属性注入 -->
<!-- <property name="属性名称" ref="bean引用名称"></property> -->
<property name="studentsDAO" ref="studentsDAO"></property>
<property name="students" ref="students"></property>
<!-- 2、构造方法注入 -->
<!-- <constructor-arg ref="studentsDAO"></constructor-arg> -->
</bean>
</beans>
修改TestSaveStudents.java
package com.spr.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.spr.service.StudentsServiceImpl;
import com.spr.vo.Students;
public class TestSaveStudents {
public static void main(String[] args){
//稍后完善
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
Students s = (Students) ctx.getBean("students");
s.setSid("L003");
s.setSname("张三");
s.setSage(20);
StudentsServiceImpl serviceImpl = (StudentsServiceImpl) ctx.getBean("studentsService");
if(serviceImpl.saveStudents()){
System.out.println("操作成功");
}
else{
System.out.println("操作失败");
}
}
}
运行后的结果:
二、构造方法注入:
首先要在需要注入属性的类中实现一个含有属性参数的构造方法(注意要保留没有参数的构造方法):
StudentsServiceImpl.java
package com.spr.service;
import com.spr.dao.StudentsDAO;
import com.spr.vo.Students;
public class StudentsServiceImpl implements StudentsService{
private StudentsDAO studentsDAO;
private Students students;
//保留不含参数的构造方法
public StudentsServiceImpl(){
}
//含有属性参数的构造方法
public StudentsServiceImpl(StudentsDAO studentsDAO,Students students){
this.studentsDAO = studentsDAO;
this.students = students;
}
@Override
//保存学生(调用DAO层方法)
public boolean saveStudents() {
// TODO Auto-generated method stub
if(studentsDAO.saveStudents(students)){
return true;
}
else{
return false;
}
}
}
之后配置applicationContext.xml文件:
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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
<bean name="students" class="com.spr.vo.Students"></bean>
<bean name="studentsDAO" class="com.spr.dao.StudentsDAOImpl"></bean>
<bean name="studentsService" class="com.spr.service.StudentsServiceImpl">
<!-- 1、Setter属性注入 -->
<!-- <property name="属性名称" ref="bean引用名称"></property> -->
<!-- <property name="studentsDAO" ref="studentsDAO"></property> -->
<!-- <property name="students" ref="students"></property> -->
<!-- 2、构造方法注入 -->
<!-- <constructor-arg ref="bean引用名称" index="属性索引"></constructor-arg> -->
<constructor-arg ref="studentsDAO" index="0"></constructor-arg>
<constructor-arg ref="students" index="1"></constructor-arg>
</bean>
</beans>
运行结果与set注入方式一样。
相对于xml文件的注入方法,annotation方法注入就十分的方便。
首先先修改applicationContext.xml文件,加入下面代码:
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/>
修改后的applicationContext.xml文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns:context="http://www.springframework.org/schema/context"
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:annotation-config/>
<bean name="students" class="com.spr.vo.Students"></bean>
<bean name="studentsDAO" class="com.spr.dao.StudentsDAOImpl"></bean>
<bean name="studentsService" class="com.spr.service.StudentsServiceImpl">
<!-- 1、Setter属性注入 -->
<!-- <property name="属性名称" ref="bean引用名称"></property> -->
<!-- <property name="studentsDAO" ref="studentsDAO"></property> -->
<!-- <property name="students" ref="students"></property> -->
<!-- 2、构造方法注入 -->
<!-- <constructor-arg ref="bean引用名称" index="属性索引"></constructor-arg> -->
<!-- <constructor-arg ref="studentsDAO" index="0"></constructor-arg> -->
<!-- <constructor-arg ref="students" index="1"></constructor-arg> -->
</bean>
</beans>
在StudentsServiceImpl.java加入java注解
StudentsServiceImpl.java
package com.spr.service;
import javax.annotation.Resource;
import com.spr.dao.StudentsDAO;
import com.spr.vo.Students;
public class StudentsServiceImpl implements StudentsService{
@Resource(name="studentsDAO")
private StudentsDAO studentsDAO;
@Resource(name="students")
private Students students;
@Override
//保存学生(调用DAO层方法)
public boolean saveStudents() {
// TODO Auto-generated method stub
if(studentsDAO.saveStudents(students)){
return true;
}
else{
return false;
}
}
}
运行TestSaveStudents.java,运行效果一样。
四、Spring注解注入
Spring注解注入与java注解注入的applicationContext.xml配置一样只不过是在注解上有所不同,如下:
StudentsServiceImpl.java
package com.spr.service;
import javax.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import com.spr.dao.StudentsDAO;
import com.spr.vo.Students;
public class StudentsServiceImpl implements StudentsService{
@Autowired
@Qualifier("studentsDAO")
private StudentsDAO studentsDAO;
@Autowired
@Qualifier("students")
private Students students;
@Override
//保存学生(调用DAO层方法)
public boolean saveStudents() {
// TODO Auto-generated method stub
if(studentsDAO.saveStudents(students)){
return true;
}
else{
return false;
}
}
}
但是运行效果是一样的