1.打包maven項目
1. 選中Java項目工程名稱,在菜單中選擇 File->project structure... (快捷鍵Ctrl+Alt+Shift+S)。

2.在彈出的窗口中左側選中"Artifacts",點擊"+"選擇jar,然后選擇"from modules with dependencies"。

3.在配置窗口中配置"Main Class"。選擇“Main Class”后配置“Directory for META-INF/MAINFEST.MF”,本文中選擇的項目根目錄,配置完成后如下圖所示,點擊OK進入下一步。

4.在彈出的窗體中選中"Build On make "(make 項目的時候會自動輸出jar)

5.以上的步驟就完成了編譯時生成Jar包的配置,然后在菜單中選擇Build->make project 。

6.build success 后,去該項目的out文件夾內查找本項目的jar包
OK,jar打好,下面才是重頭戲:
首先說下這個jar包的用處:自動獲取當前項目的屬性值(僅是測試,功能、業務性不強)

接下來,如何實現呢? 這里要了解spring boot條件注解這個概念,有如下注解形式:
| @ConditionalOnBean | 當容器里有指定的Bean的條件下 |
| @ConditionalOnClass | 當類路徑下有指定的class的條件下 |
| @ConditionalOnExpression | 基於SpEL表達式作為判斷條件 |
| @ConditionalOnJava | 基於jvm版本作為判斷條件 |
| @ConditionalOnJndi | 在JNDI存在的條件下查找指定的位置 |
| @ConditionalOnMissingBean | 當容器里沒有指定Bean的情況下 |
| @ConditionalOnMissingClass | 當類路徑下沒有指定的類的情況下 |
| @ConditionalOnNotWebApplication | 當前項目不是web項目的情況下 |
| @ConditionalOnProperty | 指定的屬性是否有指定的值 |
| @ConditionalOnResource | 類路徑下是否有指定的資源 |
| @ConditionalOnSingleCandidate | 當指定的Bean在容器中只有一個,或者雖然有多個但是指定首選的Bean |
| @ConditionalOnWebApplication | 當前項目是web項目的條件下 |
接下來,用標紅的注解,做個實例吧
首先是jar源的情況:基於spring boot框架,實現類型安全讀取屬性值的功能,比較簡單,主要是整個流程能否順利走通,最終看到想要的效果,這就要
智者見智仁者見仁了,不廢話,燥起來。

基本的組織結構,揀重點 點一下
HelloServiceAutoConfiguration
1 package com.wm.auto; 2 3 import org.springframework.beans.factory.annotation.Autowired; 4 import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; 5 import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; 6 import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; 7 import org.springframework.boot.context.properties.EnableConfigurationProperties; 8 import org.springframework.context.annotation.Bean; 9 import org.springframework.context.annotation.Configuration; 10 11 12 @Configuration 13 @EnableConfigurationProperties(HelloServiceProperties.class) 14 @ConditionalOnClass(HelloService.class) 15 @ConditionalOnProperty(prefix = "hello",value ="enabled",matchIfMissing = true) 16 public class HelloServiceAutoConfiguration { 17 @Autowired 18 private HelloServiceProperties helloServiceProperties; 19 20 @Bean 21 @ConditionalOnMissingBean(HelloService.class) 22 public HelloService helloService(){ 23 HelloService helloService=new HelloService(); 24 helloService.setMsg(helloServiceProperties.getMsg()); 25 return helloService; 26 } 27 }
此處上面的判斷條件,判斷HelloService Bean是否存在,若不存在,就創建一個。
對了,還有如何做到類型安全讀取配置文件的屬性值的
HelloServiceProperties
1 package com.wm.auto; 2 3 import org.springframework.boot.context.properties.ConfigurationProperties; 4 5 6 @ConfigurationProperties(prefix="hello") 7 public class HelloServiceProperties { 8 private static final String MSG="world"; 9 private String msg=MSG; 10 11 public String getMsg() { 12 return msg; 13 } 14 15 public void setMsg(String msg) { 16 this.msg = msg; 17 } 18 19 }
spring.factories 這個是個重點,是告訴引用此jar包的項目,聲明了有哪些自動配置,若有多個配置,需用“,”隔開,此處的“\”是為了換行后仍然能讀到屬性。
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.wm.auto.HelloServiceAutoConfiguration
接下來就可以打包了,這個過程中,遇到了個問題:使用mvn install 打包到maven的本地倉庫后,引用該jar的項目是由gradle構建的,用了若干方法始終無法被正常使用,
若某位大神 遇到過,並解決了,清指出問題的原因,不勝感激。
最后就是引用jar包的項目介紹:

1 package com.boot; 2 3 import com.boot.properties.AuthorSetting; 4 import com.wm.auto.HelloService; 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.boot.SpringApplication; 7 import org.springframework.boot.autoconfigure.SpringBootApplication; 8 import org.springframework.web.bind.annotation.RequestMapping; 9 import org.springframework.web.bind.annotation.RestController; 10 11 @RestController 12 @SpringBootApplication 13 public class Application { 14 15 @Autowired 16 public AuthorSetting authorSetting; 17 18 @Autowired 19 public HelloService helloService; 20 21 @RequestMapping("/") 22 public String index() { 23 return authorSetting.getName() + authorSetting.getAge() + "Hello Spring Boot"; 24 } 25 26 @RequestMapping("/auto-info") 27 public String autoString() { 28 return helloService.getMsg(); 29 } 30 31 public static void main(String[] args) { 32 SpringApplication.run(Application.class, args); 33 } 34 }
至此 完成,看效果

