Spring学习之Bean的装配多种方法
Spring中的Bean是应用程序的基本构建块之一,而Bean装配指的是将各个Bean组合在一起以创建一个完整的应用程序。在Spring中,有多种方法可以对Bean进行装配。
1. 自动装配
自动装配是Spring中最简单、最便利的装配方式之一。Spring会自动地为一些Bean属性寻找合适的值,并将它们注入到Bean中。自动装配有三种方式:按名称装配、按类型装配、按构造方法装配。
1.1 按名称装配
按名称进行自动装配时,Spring会自动地将与Bean属性同名的Bean注入到属性中。示例如下:
<bean name="student" class="com.example.Student">
...
</bean>
<bean name="clazz" class="com.example.Clazz">
<property name="student" ref="student"/>
...
</bean>
上面的例子中,Clazz
中的student
属性与Student
的名称相同,因此Spring会自动地将Student
注入到Clazz
的student
属性中。
1.2 按类型装配
按类型进行自动装配时,Spring会自动地寻找与Bean属性类型相同的Bean,并将它注入到属性中。示例如下:
<bean name="student" class="com.example.Student">
...
</bean>
<bean name="clazz" class="com.example.Clazz">
<property name="student" type="com.example.Student"/>
...
</bean>
上面的例子中,Clazz
中的student
属性的类型与Student
相同,因此Spring会自动地将Student
注入到Clazz
的student
属性中。
1.3 按构造方法装配
按构造方法进行自动装配时,Spring会自动地根据构造方法的参数类型和名称自动注入相应的Bean。示例如下:
<bean name="student" class="com.example.Student">
...
</bean>
<bean name="clazz" class="com.example.Clazz">
<constructor-arg name="student" ref="student"/>
...
</bean>
上面的例子中,Clazz
中的构造方法参数为student
,与Student
的名称相同,因此Spring会自动地将Student
注入到Clazz
的构造方法中。
2. 基于注解的装配
使用基于注解的方式装配Bean的好处是,代码更加简洁,可读性更好。Spring提供了多种注解用于装配Bean。
2.1 @Autowired注解
使用@Autowired
注解可以自动装配Bean,示例如下:
@Component
public class Clazz {
@Autowired
private Student student;
...
}
上面的例子中,使用@Autowired
注解注入Student
对象。
2.2 @Qualifier注解
在使用@Autowired
注解时,当存在多个同类型的Bean时,Spring无法确定注入哪个Bean。此时可以使用@Qualifier
注解指定注入哪个Bean,示例如下:
@Component
public class Clazz {
@Autowired
@Qualifier("goodStudent")
private Student student;
...
}
上面的例子中,使用@Qualifier
注解指定注入goodStudent
。
3. Java配置方式装配
Java配置方式装配Bean的优点是可以避免使用XML文件进行配置,更加灵活,可读性更好,示例如下:
@Configuration
public class AppConfig {
@Bean
public Student student() {
return new Student();
}
@Bean
public Clazz clazz() {
return new Clazz(student());
}
}
上面的例子中,使用@Configuration
注解表明这是一个Java配置类,使用@Bean
注解定义Bean。
示例一
简单实现一个Spring Web应用程序,使用自动装配实现Bean的注入。
首先,在pom.xml文件中添加如下依赖:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.2.6.RELEASE</version>
</dependency>
然后,定义两个Bean:
public class Student {
private String name;
private String id;
// 省略Getter和Setter方法
}
@Service
public class StudentService {
private List<Student> studentList = new ArrayList<>();
public void addStudent(Student student) {
studentList.add(student);
}
public List<Student> getStudentList() {
return studentList;
}
}
在StudentService
中使用@Service
注解表示这是一个服务类。
接下来,在Controller中使用自动装配将StudentService
注入:
@RestController
public class StudentController {
@Autowired
private StudentService studentService;
@RequestMapping(value = "/addStudent", method = RequestMethod.POST)
public void addStudent(Student student) {
studentService.addStudent(student);
}
@RequestMapping(value = "/getStudentList", method = RequestMethod.GET)
public List<Student> getStudentList() {
return studentService.getStudentList();
}
}
上面的代码中,使用了@RestController
注解表示这是一个控制器类。
示例二
实现一个Spring Web应用程序,使用基于注解的装配方式实现Bean的注入。
首先,在pom.xml文件中添加如下依赖:
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.2.6.RELEASE</version>
</dependency>
定义如下Bean:
public class Student {
private String name;
private String id;
// 省略Getter和Setter方法
}
@Component
public class StudentService {
private List<Student> studentList = new ArrayList<>();
public void addStudent(Student student) {
studentList.add(student);
}
public List<Student> getStudentList() {
return studentList;
}
}
在Service类中使用@Component
注解表示这是一个组件类。
然后,在Controller中使用基于注解的装配方式将StudentService
注入:
@RestController
public class StudentController {
@Autowired
private StudentService studentService;
@RequestMapping(value = "/addStudent", method = RequestMethod.POST)
public void addStudent(Student student) {
studentService.addStudent(student);
}
@RequestMapping(value = "/getStudentList", method = RequestMethod.GET)
public List<Student> getStudentList() {
return studentService.getStudentList();
}
}
上面的代码中,使用了@RestController
注解表示这是一个控制器类,使用了@Autowired
注解和@Component
注解实现了Bean的自动注入。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Spring学习之Bean的装配多种方法 - Python技术站