四、Spring IoC实践和应用(三种配置方式总结)

本章概要

  • 三种配置方式总结
    • XML方式配置总结
    • XML+注解方式配置总结
    • 完全注解方式配置总结
  • 整合Spring5-Test5搭建测试环境

4.5 三种配置方式总结

4.5.1 XML方式配置总结
  1. 所有内容写到xml格式配置文件中
  2. 声明bean通过<bean标签
  3. <bean标签包含基本信息(id,class)和属性信息 <property name value / ref
  4. 引入外部的properties文件可以通过<context:property-placeholder
  5. IoC具体容器实现选择ClassPathXmlApplicationContext对象
4.5.2 XML+注解方式配置总结
  1. 注解负责标记IoC的类和进行属性装配
  2. xml文件依然需要,需要通过<context:component-scan标签指定注解范围
  3. 标记IoC注解:@Component,@Service,@Controller,@Repository
  4. 标记DI注解:@Autowired @Qualifier @Resource @Value
  5. IoC具体容器实现选择ClassPathXmlApplicationContext对象
4.5.3 完全注解方式配置总结
  1. 完全注解方式指的是去掉xml文件,使用配置类 + 注解实现
  2. xml文件替换成使用@Configuration注解标记的类
  3. 标记IoC注解:@Component,@Service,@Controller,@Repository
  4. 标记DI注解:@Autowired @Qualifier @Resource @Value
  5. <context:component-scan标签指定注解范围使用@ComponentScan(basePackages = {“com.atguigu.components”})替代
  6. <context:property-placeholder引入外部配置文件使用@PropertySource({“classpath:application.properties”,“classpath:jdbc.properties”})替代
  7. <bean 标签使用@Bean注解和方法实现
  8. IoC具体容器实现选择AnnotationConfigApplicationContext对象

4.6 整合Spring5-Test5搭建测试环境

  1. 整合测试环境作用
  • 好处1:不需要自己创建IOC容器对象了
  • 好处2:任何需要的bean都可以在测试类中直接享受自动装配
  1. 导入相关依赖
<!--junit5测试-->
<dependency>
  <groupId>org.junit.jupiter</groupId>
  <artifactId>junit-jupiter-api</artifactId>
  <version>5.3.1</version>
</dependency>
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-test</artifactId>
  <version>6.0.6</version>
  <scope>test</scope>
</dependency>
  1. 整合测试注解使用
//@SpringJUnitConfig(locations = {"classpath:spring-context.xml"})  //指定配置文件xml
@SpringJUnitConfig(value = {
   BeanConfig.class})  //指定配置类
public class Junit5IntegrationTest {
   

    @Autowired
    private User user;

    @Test
    public void testJunit5() {
   
        System.out.println(user);
    }
}

相关推荐

  1. 、Spring IoC实践应用配置方式总结

    2023-12-31 11:28:03       32 阅读
  2. spring配置方式总结

    2023-12-31 11:28:03       20 阅读
  3. SQL实现模糊查询的方法总结

    2023-12-31 11:28:03       27 阅读
  4. Mysql的索引实现方式

    2023-12-31 11:28:03       14 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-31 11:28:03       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-31 11:28:03       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-31 11:28:03       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-31 11:28:03       18 阅读

热门阅读

  1. XXL-JOB学习笔记-基于代码实现新建、修改任务

    2023-12-31 11:28:03       32 阅读
  2. 12、defer

    2023-12-31 11:28:03       33 阅读
  3. 面试要点,算法,数据结构等练习大全

    2023-12-31 11:28:03       30 阅读
  4. 设计模式之策略模式

    2023-12-31 11:28:03       29 阅读
  5. Redis 的常用命令

    2023-12-31 11:28:03       33 阅读
  6. RK3566 ANDROID 11 平台上适配移远EC200A

    2023-12-31 11:28:03       42 阅读
  7. git分支管理

    2023-12-31 11:28:03       34 阅读