标签:ati loser tor mave 创建 ima .config 工程 自动
1,创建一个空工程
2,new一个Modules ---------------- maven (启动器) :
springboottest-spring-boot-starter
3,new一个Modules ---------------- spring(做自动配置的):
springboottest-spring-boot-starter-autoconfigurer
4,启动器pom文件中引入自动配置模块:
<!--启动器--> <dependencies> <!--引入自动配置模块--> <dependency> <groupId>com.springboottest.starter</groupId> <artifactId>springboottest-spring-boot-starter-autoconfigurer</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency> </dependencies>
5,自动配置器中,删除主主程序等不需要的内容并编写启动器:
<dependencies> <!--引入spring-boot-starter--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> </dependencies>
package com.springboottest.starter; import org.springframework.boot.context.properties.ConfigurationProperties; //绑定文件中所有以springboottest.hello 开始的配置 @ConfigurationProperties(prefix = "springboottest.hello") public class HelloProperties { private String prefix; private String suffix; public String getPrefix() { return prefix; } public void setPrefix(String prefix) { this.prefix = prefix; } public String getSuffix() { return suffix; } public void setSuffix(String suffix) { this.suffix = suffix; } }
package com.springboottest.starter; public class HelloService { HelloProperties helloProperties; public HelloProperties getHelloProperties() { return helloProperties; } public void setHelloProperties(HelloProperties helloProperties) { this.helloProperties = helloProperties; } public String sayHello(String name){ return helloProperties.getPrefix()+"-"+ name + helloProperties.getSuffix(); }; }
package com.springboottest.starter; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration @ConditionalOnWebApplication//web应用才生效 @EnableConfigurationProperties(HelloProperties.class)//让属性生效 HelloProperties helloProperties; public class HelloServiceAutoConfiguration { //让属性生效 HelloProperties helloProperties; @Autowired HelloProperties helloProperties; @Bean public HelloService helloService(){ HelloService service = new HelloService(); service.setHelloProperties(helloProperties); return service; } }
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.springboottest.starter.HelloServiceAutoConfiguration
6,新建项目,调用自定义启动器的方法
<!--引入自定义starter--> <dependency> <groupId>com.springboottest.starter</groupId> <artifactId>springboottest-spring-boot-starter</artifactId> <version>1.0-SNAPSHOT</version> </dependency>
springboottest.hello.prefix=SPRINGBOOT
springboottest.hello.suffix=HELLO WORLD
标签:ati loser tor mave 创建 ima .config 工程 自动
原文地址:https://www.cnblogs.com/MagicAsa/p/10750180.html