本文共 2068 字,大约阅读时间需要 6 分钟。
test-spring-boot-autoconfigure
org.springframework.boot spring-boot-starter-parent 1.5.2.RELEASE org.springframework.boot spring-boot-starter org.projectlombok lombok
配置文件类
@ConfigurationProperties(prefix = "test.hello")public class HelloProperties { private String suffix; public String getSuffix() { return suffix; } public void setSuffix(String suffix) { this.suffix = suffix; }}
服务类
public class HelloService { HelloProperties helloProperties; public String sayHello(String name) { return "Hello " + name + "," + helloProperties.getSuffix(); } public HelloProperties getHelloProperties() { return helloProperties; } public void setHelloProperties(HelloProperties helloProperties) { this.helloProperties = helloProperties; }}
注册类
/** * web应用才生效 */@ConditionalOnWebApplication/** * 让属性文件生效 */@EnableConfigurationProperties(HelloProperties.class)/*** * 声明是一个配置类 */@Slf4j@Configurationpublic class HelloServiceAutoConfiguration { @Autowired private HelloProperties helloProperties; @Bean public HelloService helloService() { log.info("开始注册HelloService Bean"); HelloService helloService = new HelloService(); helloService.setHelloProperties(helloProperties); return helloService; }}
test-spring-boot-starter
com.test test-spring-boot-autoconfigure 1.0-SNAPSHOT
配置类
测试在SpringBoot项目中,pom.xml引入自动配置项目
com.test test-spring-boot-starter 1.0-SNAPSHOT
配置
test.hello.suffix=早上好
注入HelloService,即可使用。
@Autowired private HelloService helloService;
参考 https://www.cnblogs.com/niumoo/p/11775009.html