java枚舉如何使用spring的@value注入屬性
java枚舉使用spring的@value注入屬性
背景:
在spring中使用@value注解來達到動態(tài)配置線上和預發(fā)環(huán)境的參數(shù),在普通類中可以隨意使用@value實現(xiàn),java枚舉enum無法注入,
怎么解決這個問題?
public enum SpringValueEnum { TEACHER(0, "我是老師"), STUDENT(1, "我是學生") { @Override public String getDesc() { return PeopleEnumContainer.name; } }; private Integer code; private String desc; SpringValueEnum(Integer code, String desc) { this.code = code; this.desc = desc; } public Integer getCode() { return code; } public void setCode(Integer code) { this.code = code; } public String getDesc() { return desc; } public void setDesc(String desc) { this.desc = desc; } @Component static class PeopleEnumContainer { private static String name; @Value("${people.student.desc}") public void init(String name) { PeopleEnumContainer.name = name; } } }
如上,使用內部類注入,重寫枚舉屬性get方法來達到動態(tài)替換的目的。
Spring @Value屬性注入使用總結
@Value注入
不通過配置文件的注入屬性的情況
通過@Value將外部的值動態(tài)注入到Bean中,使用的情況有:
- 注入普通字符串
- 注入操作系統(tǒng)屬性
- 注入表達式結果
- 注入其他Bean屬性:注入beanInject對象的屬性another
- 注入文件資源
- 注入URL資源
@Value("normal") private String normal; // 注入普通字符串 @Value("#{systemProperties['os.name']}") private String systemPropertiesName; // 注入操作系統(tǒng)屬性 @Value("#{ T(java.lang.Math).random() * 100.0 }") private double randomNumber; //注入表達式結果 @Value("#{beanInject.another}") private String fromAnotherBean; // 注入其他Bean屬性:注入beanInject對象的屬性another,類具體定義見下面 @Value("classpath:com/hry/spring/configinject/config.txt") private Resource resourceFile; // 注入文件資源 @Value("http://www.baidu.com") private Resource testUrl; // 注入URL資源
注入其他Bean屬性:注入beanInject對象的屬性another
@Component public class BeanInject { @Value("其他Bean的屬性") private String another; public String getAnother() { return another; } public void setAnother(String another) { this.another = another; } }
注入文件資源:com/hry/spring/configinject/config.txt
test configuration file
測試類:
@SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } @RunWith(SpringRunner.class) @SpringBootTest(classes=Application.class) public class ConfiginjectApplicationTest { @Autowired private BaseValueInject baseValueInject; @Test public void baseValueInject(){ System.out.println(baseValueInject.toString()); } }
運行測試類
normal=normal systemPropertiesName=Windows 10 randomNumber=35.10603794922444 fromAnotherBean=其他Bean的屬性 resourceFile=test configuration file testUrl=<html>...<title>百度一下,你就知道</title>...略</html>
通過配置文件的注入屬性的情況
通過@Value將外部配置文件的值動態(tài)注入到Bean中。
配置文件主要有兩類:
- application.properties。application.properties在spring boot啟動時默認加載此文件
- 自定義屬性文件。自定義屬性文件通過@PropertySource加載。@PropertySource可以同時加載多個文件,也可以加載單個文件。如果相同第一個屬性文件和第二屬性文件存在相同key,則最后一個屬性文件里的key啟作用。加載文件的路徑也可以配置變量,如下文的${anotherfile.configinject},此值定義在第一個屬性文件config.properties
第一個屬性文件config.properties內容如下:
${anotherfile.configinject}作為第二個屬性文件加載路徑的變量值
book.name=bookName anotherfile.configinject=placeholder
第二個屬性文件config_placeholder.properties內容如下:
book.name.placeholder=bookNamePlaceholder
下面通過@Value(“${app.name}”)語法將屬性文件的值注入bean屬性值,詳細代碼見:
@Component // 引入外部配置文件組:${app.configinject}的值來自config.properties。 // 如果相同 @PropertySource({"classpath:com/hry/spring/configinject/config.properties", "classpath:com/hry/spring/configinject/config_${anotherfile.configinject}.properties"}) public class ConfigurationFileInject{ @Value("${app.name}") private String appName; // 這里的值來自application.properties,spring boot啟動時默認加載此文件 @Value("${book.name}") private String bookName; // 注入第一個配置外部文件屬性 @Value("${book.name.placeholder}") private String bookNamePlaceholder; // 注入第二個配置外部文件屬性 @Autowired private Environment env; // 注入環(huán)境變量對象,存儲注入的屬性值 public String toString(){ StringBuilder sb = new StringBuilder(); sb.append("bookName=").append(bookName).append("\r\n") .append("bookNamePlaceholder=").append(bookNamePlaceholder).append("\r\n") .append("appName=").append(appName).append("\r\n") .append("env=").append(env).append("\r\n") // 從eniroment中獲取屬性值 .append("env=").append(env.getProperty("book.name.placeholder")).append("\r\n"); return sb.toString(); } }
測試代碼:
Application.java同上文
@RunWith(SpringRunner.class) @SpringBootTest(classes=Application.class) public class ConfiginjectApplicationTest { @Autowired private ConfigurationFileInject configurationFileInject; @Test public void configurationFileInject(){ System.out.println(configurationFileInject.toString()); } }
測試運行結果
bookName=bookName
bookNamePlaceholder=bookNamePlaceholder
appName=appName
env=StandardEnvironment {activeProfiles=[], defaultProfiles=[default], propertySources=[Inlined Test Properties,systemProperties,systemEnvironment,random,applicationConfig: [classpath:/application.properties],class path resource [com/hry/spring/configinject/config_placeholder.properties],class path resource [com/hry/spring/configinject/config.properties]]}
env=bookNamePlaceholder
總結
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
詳解mall整合SpringBoot+MyBatis搭建基本骨架
這篇文章主要介紹了詳解mall整合SpringBoot+MyBatis搭建基本骨架,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-08-08Mybatis中 mapper-locations和@MapperScan的作用
這篇文章主要介紹了Mybatis中 mapper-locations和@MapperScan的作用,mybatis.mapper-locations在SpringBoot配置文件中使用,作用是掃描Mapper接口對應的XML文件,需要的朋友可以參考下2023-05-05