1.按平常一样编写相应类,类统一在@Configuration注解类下注入到IOC容器中。
2.在resources/META-INF/下创建spring.factories文件,并将@Configuration注解类添加在=号后面,多个使用逗号隔开:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.example.config.AutoConfigBean
3.使用mvn install 将jar包安装到maven仓库(同时会在target中生成)
4.测试:在另外工程添加依赖,创建一个单元测试对DemoService中的方法进行测试
<dependency>
<groupId>com.example</groupId>
<artifactId>demo-spring-boot-starter</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
@Test
void testDemo(){
demoService.printDemoProperties();
}
正常是通过 @Configuration的方式将bean管理起来的,再将这个文件配置到spring.factories中,工程启动时就会自动加载到容器中
直接在类上使用注解@Component,然后在启动类上加上扫描包,已验证,是可以的。但也有不足,需要使用方指定扫描 @SpringBootApplication @ComponentScan(basePackages = {"org.test1","org.test2"})
新方案: 在@Configuration类上加上@ComponentScan,就可以实现: 1、自定义扫描路径下边带有@Controller,@Service,@Repository,@Component注解加入spring容器 2、通过includeFilters加入扫描路径下没有以上注解的类加入spring容器 3、通过excludeFilters过滤出不用加入spring容器的类 4、自定义增加了@Component注解的注解方式
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。