1. maven依赖
在src目录下新建spring-conf.xml文件(文件名称可自己定,也不用必须放到src下,程序编译后,保证文件在classpath下就行)。
创建是maven项目,则需在pom.xml中添加spring依赖,如下:
<properties>
<spring.version>5.1.9.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
2. 编写类
2.1. 实体类
public class Student {
private int id;
private String name;
public Student() {
}
public Student(int id, String name) {
this.id = id;
this.name = name;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + ''' +
'}';
}
// set、get方法
}
2.2 DAO层
IStudentDao定义一个得到学生信息的方法
public interface IStudentDao {
// 得到学生的信息
String getStuInfo();
}
StudentDaoImpl实现IStudentDao接口,StudentDaoImpl类用到Student类
public class StudentDaoImpl implements IStudentDao {
private Student student;
public StudentDaoImpl() {
}
public StudentDaoImpl(Student student) {
this.student = student;
}
@Override
public String getStuInfo() {
return student.toString();
}
public Student getStudent() {
return student;
}
public void setStudent(Student student) {
this.student = student;
}
}
3. 编写配置文件spring-conf.xml
3.1 set方法注入
property标签,name值对应StudentDaoImpl里的setStudent()方法名字中的student(spring通过反射,调用的是set方法,不是直接赋值成员变量的),ref指向 student bean,ref的值为配置student bean的id。
使用set方法注入,会先执行无参构造器,再执行set方法
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd">
<bean id="student" class="com.example.entity.Student">
<property name="id" value="10"/>
<property name="name" value="zhangsan"/>
</bean>
<bean id="studentDao" class="com.example.dao.StudentDaoImpl">
<property name="student" ref="student"/>
</bean>
</beans>
3.2 构造器注入
constructor-arg标签,调用构造器为成员变量赋值
使用构造器注入,执行有参构造器
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd">
<bean id="student" class="com.example.entity.Student">
<constructor-arg name="id" value="10"/>
<constructor-arg name="name" value="zhangsan"/>
</bean>
<bean id="studentDao" class="com.example.dao.StudentDaoImpl">
<constructor-arg name="student" ref="student"/>
</bean>
</beans>
3.3 手动装配与自动装配
不自动装配(property手动赋值):
<bean autowire="default"> <bean autowire="no">
自动装配(自动赋值,仅对自定义类型的成员变量有效):
<bean autowire="byName">:
以的setXxx方法的名字(xxx)作为id找个这个组件,为其赋值;如果没找到就装配null
<bean autowire="byType">:
以成员变量的类型找组件,找到多个则会报错,如果没找到就装配null
<bean autowire="constructor" >:
先按照有参构造器参数的类型进行装配,成功就默认赋值,没有就直接组件装配null,如果按照类型找到多个,参数的名作为id继续装配,找不到就null,不会报错
4. 测试
public class Main {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-conf.xml");
IStudentDao studentDao = (IStudentDao) context.getBean("studentDao");
String stuInfo = studentDao.getStuInfo();
System.out.println(stuInfo);
}
}
受教了,给博主递茶( ̄ˇ ̄)