一、maven依赖
<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>
二、开启注解支持
在spring配置文件spring-conf.xml中,开启注解驱动支持,即加上<context:annotation-config/>
作用是,向spring容器(spring5.1.9)注册了5个bean,此5个bean的id为:
- org.springframework.context.annotation.internalConfigurationAnnotationProcessor
- org.springframework.context.annotation.internalAutowiredAnnotationProcessor
- org.springframework.context.annotation.internalCommonAnnotationProcessor
- org.springframework.context.event.internalEventListenerProcessor
- org.springframework.context.event.internalEventListenerFactory
如果想使用@Autowired注解,那么就必须事先在 Spring 容器中声明 AutowiredAnnotationBeanPostProcessor Bean。则
在spring-conf.xml配置文件中加Student Bean和Dao Bean
<?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">
<context:annotation-config/>
<bean id="student" class="com.example.entity.Student">
<property name="id" value="123456"/>
<property name="name" value="zhangsan"/>
</bean>
<bean id="studentDao" class="com.example.dao.StudentDaoImpl">
<!-- 下面一句注释掉,使用注解时,不需要这句注入 -->
<!--<property name="student" ref="student"/>-->
</bean>
</beans>
三、编写类
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.DAO层
public interface IStudentDao {
// 得到学生的信息
String getStuInfo();
}
四、@Autowired
@Autowired(required=true)
构造器、字段、方法
此注解是使bean自动装配,代替xml的装配。
@Autowired根据参数类型(ByType)自动装配,如果有多个同个类型的bean,根据名称装配(成员变量名作为id)。
@Autowired(required = false),这等于告诉 Spring,在找不到匹配 Bean 时也不报错。默认required = true,找不到匹配的Bean时会报错。
在Bean的字段上添加@AutoWired注解:
public class StudentDaoImpl implements IStudentDao{
@Autowired
private Student student;
// 得到学生的信息
@Override
public String getStuInfo() {
return student.toString();
}
// 可不写set、get,因其用字段注入,不是从set方法注入
}
五、@Qualifier
@Qualifier(value = "限定标识符")
字段、方法、参数
通过@Qualifier注解指定名称,确定一个Bean。此时只能使用指定id,通过ByName查找Bean。
在Bean的字段上添加@Qualifier注解:
public class StudentDaoImpl implements IStudentDao{
// 指定id为student的bean
@Autowired
@Qualifier("student")
private Student student;
// 得到学生的信息
@Override
public String getStuInfo() {
return student.toString();
}
}
六、@Resource
@Resource:自动装配,默认根据名称(ByName)装配,如果找不到根据参数类型(ByType)装配
@Resource(name="name")
字段或setter方法
在Bean的字段上添加@Resource注解:
public class StudentDaoImpl implements IStudentDao {
@Resource(name="student")
private Student student;
@Override
public String getStuInfo() {
return student.toString();
}
}
@Autowired是Spring的注解,@Resource是J2EE的注解
七、测试
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);
}
}