[Spring框架]spring新注解配置、spring整合JUnit5

1. spring新注解配置

1. @Configuration#

作用:配置类,等同于bean.xml文件
获取容器时需要使用AnnotationApplicationContext(有@Configuration注解的类.class)
属性:value:指定配置类的字节码

2. @ComponentScan#

作用:用于指定 spring 在初始化容器时要扫描的包
等同于<context:component-scan base-package="com "/>

3. @Bean#

作用:该注解只能写在方法上,表明使用此方法创建一个对象,并且放入 spring 容器
属性: name:给当前@Bean 注解方法创建的对象指定一个名称(即 bean 的 id)

4. @PropertySource#

作用:指定properties文件中的配置
属性:value[]:用于指定properties 文件位置

5. @Import#

作用:导入其他配置类
属性:value[]:指定其他配置类的字节码

2. spring整合junit5

作用:省略测试类中获取容器的语句

ApplicationContext ac =
        new AnnotationConfigApplicationContext(SpringConfig.class);
UserService userService = (UserService)ac.getBean("userService");

1. 修改pom.xml#

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>5.3.18</version>
</dependency>
<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter</artifactId>
    <version>5.7.2</version>
    <scope>test</scope>
</dependency>

2. 在测试类上增加一个复合注解@SpringJUnitConfig#

posted @   Vincy9501  阅读(434)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 25岁的心里话
点击右上角即可分享
微信分享提示
主题色彩