SpringBoot編寫自定義配置信息
阿新 • • 發佈:2019-04-11
efi onf config ext actor frame sco int turn
⒈編寫自定義配置類
1.瀏覽器配置
1 package cn.coreqi.security.properties; 2 3 public class BrowserProperties { 4 5 private String loginPage = "coreqi-signIn.html"; //登錄頁面 6 7 private LoginType loginType = LoginType.JSON; 8 9 10 public LoginType getLoginType() { 11 returnloginType; 12 } 13 14 public void setLoginType(LoginType loginType) { 15 this.loginType = loginType; 16 } 17 18 public String getLoginPage() { 19 return loginPage; 20 } 21 22 public void setLoginPage(String loginPage) { 23 this.loginPage = loginPage;24 } 25 }
2.安全配置中包含了瀏覽器配置
1 package cn.coreqi.security.properties; 2 3 import org.springframework.boot.context.properties.ConfigurationProperties; 4 5 @ConfigurationProperties(prefix = "coreqi.security") 6 public class SecurityProperties { 7 private BrowserProperties browser = newBrowserProperties(); 8 9 public BrowserProperties getBrowser() { 10 return browser; 11 } 12 13 public void setBrowser(BrowserProperties browser) { 14 this.browser = browser; 15 } 16 }
⒉在配置文件中配置
1 coreqi.security.browser.loginPage=/coreqi-signIn.html
⒊開啟自定義配置,並在代碼中引用
1 package cn.coreqi.security.config; 2 3 import cn.coreqi.security.properties.SecurityProperties; 4 import org.springframework.beans.factory.annotation.Autowired; 5 import org.springframework.boot.context.properties.ConfigurationProperties; 6 import org.springframework.boot.context.properties.EnableConfigurationProperties; 7 import org.springframework.context.annotation.Configuration; 8 9 @Configuration 10 @EnableConfigurationProperties(value = {SecurityProperties.class}) 11 public class SecurityPropertiesConfig { 12 @Autowired 13 private SecurityProperties securityProperties; 14 public void test(){ 15 System.out.println(securityProperties.getBrowser().getLoginPage()); 16 } 17 }
SpringBoot編寫自定義配置信息