文章目录
前言
一、手动装配实现属性注入
<bean id="studentDao" class="com.xxx.dao.impl.StudentDaoImpl"></bean>
<bean id="studentService" class="com.xxx.service.impl.StudentServiceImpl">
<!--手动装配:设值注入,将studentDao对象注入给service对象的dao属性 -->
<property name="studentDao" ref="studentDao"></property>
</bean>
缺点:当维护bean组件或bean属性增加时,需要大量工作量完成配置。
二、自动装配实现属性注入
- 基于xml配置实现自动装配
- 基于注解+xml配置实现自动装配
基于注解+配置类实现自动装配
三、基于xml配置实现自动装配
3.1 基于xml配置实现自动装配
<bean id="studentDao" class="com.xz.dao.impl.StudentDaoImpl"></bean>
<bean id="studentService" class="com.xz.service.impl.StudentServiceImpl"
autowire="byName">
</bean>
3.2 autowire属性值
属性值 | 属性值 |
---|---|
byName | 根据名称自动装配。要求:对象id标识名和对象属性的set方法名相同。 |
byType | 根据类型自动装配要求:对象的class类型和对象属性的类型相同。注意:如果spring容器中有多个与对象属性相同的类型的bean,会报错。 |
constructor | 类似于byType,区别在于走构造方法。 |
no | 不自动装配。必须手动装配进行配置 |
default | 装配方式和全局自动装配default-autowire的值一致。如果autowire和default-autowrie都为default,那么就是不自动装配 |
3.3 全局的自动装配
<beans default-autowire="byName"/> 适用于所有的bean
四、基于注解+XML配置管理 Bean实现方式
4.1 基于注解+XML配置管理 Bean实现方式
注解 | 描述 |
---|---|
@Component | 创建对象注解,没有语义默认id标识名:类名首字母小写自定义id标识名@Service("studentService") |
@Service | 放在service层类上注解 |
@Repository | 放在dao层类上注解 |
@Controller | 放在控制层Controller类上注解 |
4.2自动注入注解,等价于autowire属性
注解 | 描述 |
---|---|
@Resource | Jdk提供注解,不需要提供set方法。 先根据byName进行注入,如果没有name一样的, 再根据byType进行注入。 @Resource注解属于JDK扩展包,所以不在JDK当中,需要额外引入以下依赖: <dependency> <groupId>jakarta.annotation</groupId> <artifactId>jakarta.annotation-api</artifactId> <version>2.1.1</version> </dependency> |
@Autowired | Spring提供注解,不需要提供set方法。 根据byType进行注入。 如果想根据名称装配,需要配合@Qualifier注解一起用。 @Qualifier(value = “名称”) 根据指定的名称作为 bean 的id进行匹配注入 |
4.3实现步骤
1.Service
@Service
public class StudentServiceImpl implements StudentService {
@Autowired
//@Resource
StudentDao studentDao;
@Override
public List<Student> getStudents() {
return studentDao.selectAll();
}
}
2.Dao
@Repository
public class StudentDaoImpl implements StudentDao {
@Override
public List<Student> selectAll() {
List<Student> list=new ArrayList<>();
list.add(new Student("1001","小张",18));
list.add(new Student("1002","小李",38));
return list;
}
}
3.applicationContext.xml 配置文件
<!--开启注解扫描-->
<context:component-scan base-package="com.xxx.service.impl,com.xxx.dao.impl"/>
4.测试
/*基于注解+xml配置*/
@Test
public void test01(){
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
StudentService studentService=ac.getBean("studentServiceImpl",StudentService.class);
List<Student> list=studentService.getStudents();
System.out.println(list);
}
五、基于注解+配置类方式管理 Bean实现自动装配
5.1相关注解
注解 | 描述 |
---|---|
@Configuration | 指定一个类为配置类,可以添加配置注解,替代配置xml文件 |
@ComponentScan(basePackages = {“包”,“包”}) | 开启注解扫描,替代<context:component-scan标签实现注解扫描 |
@PropertySource(“classpath:配置文件地址”) | 加载读取配置文件,替代 <context:property-placeholder标签 |
5.2实现步骤
1.Service
@Service
public class StudentServiceImpl implements StudentService {
@Autowired
//@Resource
StudentDao studentDao;
@Override
public List<Student> getStudents() {
return studentDao.selectAll();
}
}
2.Dao
@Repository
public class StudentDaoImpl implements StudentDao {
@Override
public List<Student> selectAll() {
List<Student> list=new ArrayList<>();
list.add(new Student("1001","小张",18));
list.add(new Student("1002","小李",38));
return list;
}
}
3.创建一个SpringConfig.java的java配置类,替换掉Spring的配置文件
注意:删除applicationContext.xml配置文件
//标注当前类是配置类,替代applicationContext.xml
@Configuration
//使用@ComponentScan注解,可以配置扫描包,替代<context:component-scan标签
@ComponentScan(basePackages = {"com.xxx.dao.impl","com.xxx.service.impl"})
public class MyConfiguration {
}
4.测试
/*基于注解+配置类*/
@Test
public void test01(){
ApplicationContext ac=new AnnotationConfigApplicationContext(MyConfiguration.class);
StudentService studentService=ac.getBean("studentServiceImpl",StudentService.class);
List<Student> list=studentService.getStudents();
System.out.println(list);
}
六、Spring5+Test5搭建测试环境
6.1整合测试环境作用
好处1:不需要自己创建IOC容器对象了
好处2:任何需要的bean都可以在测试类中直接享受自动装配
6.2 导入相关依赖
<!--junit5测试-->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.3.1</version>
<scope>test</scope>
</dependency>
<!--Spring-test-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>6.0.6</version>
</dependency>
6.3整合测试注解使用
/**
* Spring5+Test5搭建测试环境
*/
//@SpringJUnitConfig(locations = {"applicationContext.xml"}) //指定配置文件
@SpringJUnitConfig(value = {MyConfiguration.class}) //指定配置类
public class TestDemo02 {
@Autowired
StudentService studentService;
@Test
public void test01(){
List<Student> list=studentService.getStudents();
System.out.println(list);
}
}
总结
XMLIoC方式问题总结:
- 注入的属性必须添加setter方法、代码结构乱!
- 配置文件和Java代码分离、编写不是很方便!
- XML配置文件解析效率低
注解+XML IoC方式问题总结
1. 自定义类可以使用注解方式,但是第三方依赖的类依然使用XML方式!
2. XML格式解析效率低!
Spring 完全注解配置(Fully Annotation-based Configuration)
是指通过 Java配置类 代码来配置 Spring 应用程序,
使用注解来替代原本在 XML 配置文件中的配置。
相对于 XML 配置,完全注解配置具有更强的类型安全性和更好的可读性。