javascript
Spring Boot笔记-自动配置(Spring Boot封装成jar被其他项目引用)
特點:
這里也就是自己寫個Service注冊到別人的SpringBoot項目中,然后別人來調用這個Service,這個Service,先讀取自己的application.properties,再讀取引入這個項目的application.properties,這里是覆蓋式的。
?
程序運行如下:
CallJar調用了TestToolJar的一個Service。
運行截圖如下:
這里的host,和port是在CallJar的application.properties中配置的:
?
封裝TestToolJar,讓其他SpringBoot項目調用
TestServiceAutoConfiguration.java接收application.properties的配置數據:
@ConfigurationProperties(prefix = "test-config") public class TestServiceProperties {private String host;private int port;public String getHost() {return host;}public void setHost(String host) {this.host = host;}public int getPort() {return port;}public void setPort(int port) {this.port = port;} }這里maven要添加:
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-configuration-processor</artifactId><optional>true</optional> </dependency>TestService.java要封裝的service
public class TestService {private String host;private Integer port;public TestService(String host, Integer port) {this.host = host;this.port = port;}public void printAll(){System.out.println("host: " + this.host + " , port:" + port);} }這里要注意,這種方法不用去@Service,在調用這個jar包的spring boot進行調用操作。
TestServiceAutoConfiguration.java
@Configuration @ConditionalOnClass(TestService.class) @ConditionalOnProperty(name = "test-config.enabled", havingValue = "true", matchIfMissing = true) @EnableConfigurationProperties(TestServiceProperties.class) public class TestServiceAutoConfiguration {@Bean@ConditionalOnMissingBeanpublic TestService testService(TestServiceProperties testServiceProperties){return new TestService(testServiceProperties.getHost(), testServiceProperties.getPort());} }自動注入配置
@ConditionalOnClass,存在TestService.class才裝配;
@ConditionalOnProperty,配置文件存在test-config.enable=true才啟動,允許此配置不存在
@ConditionalOnMissingBea,沒有TestServiceAutoConfiguration這個類才進行裝配。
下面是EnableTestService.java
這個類是給調用者提供的,調用者使用此注解,將spring boot的工具jar包,加載到自己的項目中
@Inherited @Documented @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Import(TestServiceAutoConfiguration.class) public @interface EnableTestService { }再去掉maven中的打包插件:
打包:
隨后是將jar導入到倉庫:
mvn install:install-file -Dfile=TestToolJar-1.0.jar -DgroupId=cn.it1995 -DartifactId=testToolJar -Dversion=1.0.1 -Dpackaging=jar下面是調用者
首先是maven引入jar包:
<dependency><groupId>cn.it1995</groupId><artifactId>testToolJar</artifactId><version>1.0.1</version> </dependency>在application.properties中進行配置:
test-config.enable=true test-config.host=114.114.114.114 test-config.port=9999調用如下:
@SpringBootApplication @EnableTestService public class CallJarMain implements CommandLineRunner {@AutowiredTestService testService;public static void main(String[] args) {SpringApplication.run(CallJarMain.class, args);}@Overridepublic void run(String... args) throws Exception {testService.printAll();} }@EanleTestService,回去調用打包成jar的spring boot工具包中的EableTestService.java,如果application.properties中test-config.enable=true時,就會去注冊TestService,否則就不會進行注冊。
運行截圖如下:
項目打包下載地址:
https://github.com/fengfanchen/Java/tree/master/SpringBootToolJarTest
總結
以上是生活随笔為你收集整理的Spring Boot笔记-自动配置(Spring Boot封装成jar被其他项目引用)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spring Boot文档阅读笔记-构建
- 下一篇: Spring Boot文档阅读笔记-ex