最近在学习spring3.2.2框架,记录一下springIOC的三种对象注入方式
1. 项目结构图
2. bean对象类 com.xdh.vo.Student
package com.xdh.vo;
public class Student {
private String name;
private String id;
private int age;
//spring会默认调用该空白的构造方法,需要保留
public Student(){
}
public Student(String id, String name, int age){
this.id = id;
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
3. 接口StudentDAO
package com.xdh.dao;
import com.xdh.vo.Student;
public interface StudentDAO {
public boolean save(Student stu);
}
4. 接口的实现类StudentDAOImpl
package com.xdh.dao.impl;
import com.xdh.dao.StudentDAO;
import com.xdh.vo.Student;
public class StudentDAOImpl implements StudentDAO {
@Override
public boolean save(Student stu) {
if(stu != null){
System.out.println("学号: " + stu.getId());
System.out.println("姓名: " + stu.getName());
System.out.println("年龄: " + stu.getAge());
return true;
}else{
return false;
}
}
}
5. 服务类StudentService
package com.xdh.service;
import javax.annotation.Resource;
import com.xdh.dao.StudentDAO;
import com.xdh.vo.Student;
public class StudentService {
@Resource(name="studentDAO")
private StudentDAO stuDAO;
@Resource(name="student")
private Student stu;
public StudentService(){
}
public StudentService(Student stu) {
this.stu = stu;
}
public Student getStu() {
return stu;
}
public void setStu(Student stu) {
this.stu = stu;
}
public StudentDAO getStuDAO() {
return stuDAO;
}
public void setStuDAO(StudentDAO stuDAO) {
this.stuDAO = stuDAO;
}
public boolean saveStudent(){
if(stuDAO.save(stu)){
return true;
}else{
return false;
}
}
}
6. spriing的配置文件applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xmlns:util="http://www.springframework.org/schema/util" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd">
<context:annotation-config/>
<bean name="student" class="com.xdh.vo.Student">
<constructor-arg index="0" value="3110402121"></constructor-arg>
<constructor-arg index="1" value="夏董晖"></constructor-arg>
<constructor-arg index="2" value="18"></constructor-arg>
</bean>
<bean name="studentDAO" class="com.xdh.dao.impl.StudentDAOImpl"/>
<bean name="studentService" class="com.xdh.service.StudentService">
<!-- <property name="stuDAO" ref="studentDAO"></property> -->
<!-- <constructor-arg ref="student"></constructor-arg> -->
</bean>
</beans>
7. 测试类Test.java
package com.xdh.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.xdh.service.StudentService;
public class Test {
private static ApplicationContext ctx;
public static void main(String[] args) {
ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
StudentService stuService = (StudentService)ctx.getBean("studentService");
stuService.saveStudent();
}
}
说明:
spring注入对象有三种方法:
1. setter注入
2. 构造方法注入
3. 注解注入(推荐使用java的注解方式)
一般推荐使用注解注入,因为这样不用经常变动applicationContext.xml配置文件。
一般使用@Resource(name = "xxx"),其中xxx代表该对象在配置文件中的bean的名字。
注解的判断对象的顺序是先按名字匹配,再按类型匹配。