@ConfigurationProperties註解的使用
用處
根據類型校驗和管理application中的beancss
舉例使用
application.properties的內容java
test.name=default
test.age=19
test.friends[0]=Ben
test.friends[1]=Alice
test.utils[0]=DateUtils
test.utils[1]=TimeUtils
新建TestProperties用來接收配置的內容spring
package com.yxj.spring.properties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import java.util.Arrays;
import java.util.List;
/**
* @ProjectName: springBootDemo
* @Package: com.yxj.spring.properties
* @Description: @ConfigurationProperties(prefix="test"),該註解會自動,將配置文件前綴是test的各項,加載至該bean中
* @Author: 阿杰
* @CreateDate: 2019/1/23 20:00
* @UpdateUser: 暫無
* @UpdateDate: 2019/1/23 20:00
* @UpdateRemark: The modified content
* @Version: 1.0
*/
@ConfigurationProperties(prefix="test")
public class TestProperties {
private String age;
private String name;
private String[] friends;
private List<String> utils;
public String[] getFriends() {
return friends;
}
public TestProperties setFriends(String[] friends) {
this.friends = friends;
return this;
}
public String getAge() {
return age;
}
public TestProperties setAge(String age) {
this.age = age;
return this;
}
public String getName() {
return name;
}
public TestProperties setName(String name) {
this.name = name;
return this;
}
public List<String> getUtils() {
return utils;
}
public TestProperties setUtils(List<String> utils) {
this.utils = utils;
return this;
}
@Override
public String toString() {
return "TestProperties{" +
"age='" + age + '\'' +
", name='" + name + '\'' +
", friends=" + Arrays.toString(friends) +
", utils=" + utils +
'}';
}
}
springboot啓動類sql
package com.yxj.spring;
import com.yxj.spring.properties.TestProperties;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Import;
import java.util.List;
/**
* @ProjectName: springBootDemo
* @Package: com.yxj.spring
* @Description:
* @Author: 阿杰
* @CreateDate: 2019/1/18 20:18
* @UpdateUser: 暫無
* @UpdateDate: 2019/1/18 20:18
* @UpdateRemark: The modified content
* @Version: 1.0
*/
@Import(TestProperties.class)
@SpringBootApplication
public class SpringBootTestRun {
/**
* getBeansOfType(MakeApp.class)會裝配bean類型是MakeApp的全部實例
* @param args
*/
public static void main(String[] args) {
ConfigurableApplicationContext run = SpringApplication.run(SpringBootTestRun.class, args);
System.out.println("------------------分割線------------------");
System.out.println(run.getBean(TestProperties.class));
}
}
測試結果數組
------------------分割線------------------
TestProperties{age='19', name='default', friends=[Ben, Alice], utils=[DateUtils, TimeUtils]}
注意:
@ConfigurationProperties(prefix="test"),該註解會自動,將配置文件前綴是test的各項,加載至該bean中springboot
須要給定各個屬性值的get,set方法微信
名字須要與配置項的key值對應app
能夠加載數組,集合ide
須要@Component註解或者其餘方式將該類交由spring管理svg
@EnableAutoConfiguration註解的使用
解析
1.springBoot啓動類會使用@SpringBootApplication
2.點進入源代碼發現改註解是一個複合註解,由好幾個註解共同組合而成
@SpringBootConfiguration標註當前類是配置類,並會將當前類內聲明的一個或多個以@Bean註解標記的方法的實例歸入到spring容器中,而且實例名就是方法名。
@EnableAutoConfiguration,自動配置註解,自動載入應用程序所需的全部Bean
@ComponentScan:默認掃描@SpringBootApplication所在類的同級目錄以及它的子目錄。這也就是springBoot啓動類創建在項目根目錄的緣由
嘗試
package com.yxj.spring;
import com.yxj.spring.properties.TestProperties;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Import;
import java.util.List;
/**
* @ProjectName: springBootDemo
* @Package: com.yxj.spring
* @Description:
* @Author: 阿杰
* @CreateDate: 2019/1/18 20:18
* @UpdateUser: 暫無
* @UpdateDate: 2019/1/18 20:18
* @UpdateRemark: The modified content
* @Version: 1.0
*/
@Import(TestProperties.class)
@SpringBootApplication
public class SpringBootTestRun {
public static void main(String[] args) {
ConfigurableApplicationContext run = SpringApplication.run(SpringBootTestRun.class, args);
System.out.println("------------------分割線------------------");
System.out.println(run.getBean(TestProperties.class));
}
}
輸出結果
------------------分割線------------------
TestProperties{age='19', name='default', friends=[Ben, Alice], utils=[DateUtils, TimeUtils]}
總結
將@SpringBootApplication替換爲@EnableAutoConfiguration,仍是能正常輸出spring管理的bean對象
@EnableAutoConfiguration註解導入了AutoConfigurationImportSelector類,最終的接口類是ImportSelector
源代碼註釋解釋了,ImportSelector 該接口的方法的返回值都會被歸入到spring容器管理中
ImportSelector的具體使用與自定義,能夠查看大佬博客https://blog.csdn.net/weixin_34452850/article/details/82883033
springBoot事件監聽
步驟
自定義事件,通常是繼承ApplicationEvent抽象類
Spring的事件監聽器接口,全部的監聽器都實現了ApplicationListener接口
發佈事件
代碼實現
定義自定義事件
package com.yxj.spring.monitor;
import org.springframework.context.ApplicationEvent;
/**
* @ProjectName: springBootDemo
* @Package: com.yxj.spring.monitor
* @Description: 定義事件
* @Author: 阿杰
* @CreateDate: 2019/1/23 22:16
* @UpdateUser: 暫無
* @UpdateDate: 2019/1/23 22:16
* @UpdateRemark: The modified content
* @Version: 1.0
*/
public class MyEvent extends ApplicationEvent {
/**
* Create a new ApplicationEvent.
*
* @param source the object on which the event initially occurred (never {@code null})
*/
public MyEvent(String source) {
super(source);
}
}
定義事件監聽器
package com.yxj.spring.monitor;
import org.springframework.context.ApplicationListener;
/**
* @ProjectName: springBootDemo
* @Package: com.yxj.spring.monitor
* @Description: 定義事件監聽器
* @Author: 阿杰
* @CreateDate: 2019/1/23 22:18
* @UpdateUser: 暫無
* @UpdateDate: 2019/1/23 22:18
* @UpdateRemark: The modified content
* @Version: 1.0
*/
public class MyApplicationListener implements ApplicationListener<MyEvent> {
@Override
public void onApplicationEvent(MyEvent event) {
System.out.println("接收到事件:"+event.getClass());
}
}
springBoot啓動類
package com.yxj.spring;
import com.yxj.spring.monitor.MyApplicationListener;
import com.yxj.spring.monitor.MyEvent;
import com.yxj.spring.properties.TestProperties;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationListener;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Import;
import java.util.List;
/**
* @ProjectName: springBootDemo
* @Package: com.yxj.spring
* @Description:
* @Author: 阿杰
* @CreateDate: 2019/1/18 20:18
* @UpdateUser: 暫無
* @UpdateDate: 2019/1/18 20:18
* @UpdateRemark: The modified content
* @Version: 1.0
*/
@Import(TestProperties.class)
@SpringBootApplication
public class SpringBootTestRun {
public static void main(String[] args) {
ApplicationListener myApplicationListener = new MyApplicationListener();
MyEvent myEvent = new MyEvent("");
SpringApplication springBootTestRun = new SpringApplication(SpringBootTestRun.class);
springBootTestRun.addListeners(myApplicationListener);
ConfigurableApplicationContext run = springBootTestRun.run(args);
run.publishEvent(myEvent);
run.close();
}
}
測試結果
接收到事件:class com.yxj.spring.monitor.MyEvent
重點
將監聽器添加至spring容器管理幾種方式
1.springBootTestRun.addListeners(myApplicationListener);
2.在類上面加入@Component註解,@Service等註解
3.在application.properties添加參數context.listener.classes=監聽器類的項目全路徑
4.經過@Eventlistener註解,方法參數爲監聽的類ApplicationEvent或者其子類,一些自定義的監聽器,或者springBoot系統啓動時加載初始化信息等均可以經過ApplicationEvent+改變參數來實現監聽
若是感受文章對你有幫助,歡迎轉發點贊
下一篇文章介紹如何使用SpringBoot實現支付寶掃碼支付 APP支付 H5支付
掃描下方二維碼 公衆號回覆 資源 資料
送你們兩套資源
若有問題掃描下方二維碼加羣
Springboot企業級掃碼點餐實戰課程 回覆 資源
2.Mysql從入門到精通 回覆 資料
長按識別二維碼,瞭解更多
掃描二維碼加入技術交流羣!
本文分享自微信公衆號 - 亂敲代碼(lqcoder)。
若有侵權,請聯繫 support@oschina.cn 刪除。
本文參與「OSC源創計劃」,歡迎正在閱讀的你也加入,一塊兒分享。