目录
- 统一测试类与config类
- 1. 启动类注入Bean
- 2.启动类扫描@ComponentScan
- 3.启动类@EnableConfigurationProperties
- 4.启动类@Import
统一测试类与config类
yml
student: red: dou
config
@Configuration注解 与 @Component注解都可以。
@Data @Configuration @ConfigurationProperties(prefix = "student") // 文件上传 配置前缀file.oss public class MinIOConfigProperties implements Serializable { private String red; }
@Data @Component @ConfigurationProperties(prefix = "student") // 文件上传 配置前缀file.oss public class MinIOConfigProperties implements Serializable { private String red; }
test
@SpringBootTest class SpringBoojavascripttTest01ApplicationTests { @Autowired private MinIOConfigProperties minIOConfhttp://www.devze.comigProperties; @Test void contextLoads() { // 获取AccessKey值 String accessKey = minIOConfigProperties.getRed(); System.out.println(accessKey); } }
1. 启动类注入Bean
@SpringBootApplication public class SpringBootTest01Application { public static void main(String[] args) { SpringApplication.run(SpringBootTest01Application.class, args); } @Bean public MinIOConfigProperties minIOConfigProperties() { return new MinIOConfigProperties(); } }
2.启动类扫描@ComponentScan
@ComponentScan(basePackages = "com.ma.config") @SpringBootApplication public class SpjsringBootTest01Application { public static void main(String[] args) { SpringApplication.run(SpringBootTest01Application.class, args); } }
3.启动类@EnableConfigurationProperties
@SpringBootApplication js@EnableConfigurationProperties(MinIOConfigProperties.class) public class SpringBoohttp://www.devze.comtTest01Application { public static void main(String[] args) { SpringApplication.run(SpringBootTest01Application.class, args); } }
4.启动类@Import
@SpringBootApplication @Import(MinIOConfigProperties.class) public class SpringBootTest01Application { public static void main(String[] args) { SpringApplication.run(SpringBootTest01Application.class, args); } }
到此这篇关于SpringBoot注入Bean的四种方式总结的文章就介绍到这了,更多相关SpringBoot注入Bean内容请搜索编程客栈(www.devze.com)以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程客栈(www.devze.com)!
精彩评论