一、property-placeholderhelp
Property-placeholder是Spring框架提供的一種用於替換XML中屬性佔位符的機制。它可以讀取Properties文件,並且在XML文件中取出相應的屬性值,並將其替換為佔位符,該機制對於模塊化配置、測試、靈活性以及重用方面都具有很大的優勢。
在Spring的應用上下文中,通過PropertyPlaceholderConfigurer可以很方便的讀取Properties文件,並用${}的語法來引用讀取的屬性值,當然在引用值為空的時候,可以設置默認值。例如:
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${jdbc.driverClassName}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean>
在上述的代碼中,${jdbc.driverClassName}、${jdbc.url}、${jdbc.username} 和 ${jdbc.password} 都是PropertyPlaceholderConfigurer讀取Properties文件中所定義的屬性名。
二、property-placeholderhelper
除了通過PropertyPlaceholderConfigurer來讀取Properties文件之外,Spring 4.3版本以後,還提供了一個更加強大的屬性載入機制 — PropertySourcesPlaceholderConfigurer。PropertySourcesPlaceholderConfigurer繼承自PropertyPlaceholderConfigurer,並且更加靈活,支持從多個地方讀取屬性文件,包括系統屬性、Java配置類、環境變數等等。實際上,它也是Spring @PropertySources註解使用的核心類。
在Java配置類中配置示例:
@Configuration @ComponentScan @PropertySources(value = { @PropertySource("classpath:application.properties"), @PropertySource("file:/opt/config/application.properties") }) public class AppConfig { @Autowired Environment env; @Bean public DataSource dataSource() { BasicDataSource dataSource = new BasicDataSource(); dataSource.setDriverClassName(env.getProperty("jdbc.driverClassName")); dataSource.setUrl(env.getProperty("jdbc.url")); dataSource.setUsername(env.getProperty("jdbc.username")); dataSource.setPassword(env.getProperty("jdbc.password")); return dataSource; } }
使用PropertySourcesPlaceholderConfigurer可以在Java配置類中從多個地方載入屬性,並且與其他的Spring組件無縫集成。
三、property-placeholder熱載入
在大多數情況下,我們不會在運行時更改屬性文件中的值,但是,在一些特殊的情況下,我們可能需要熱修改Properties文件的值,並且更新Spring容器中的Bean。Spring框架自身並沒有提供熱修改屬性文件的功能,但是我們可以自己實現一個熱載入的機制。
我們可以在程序中監控Properties文件的修改,並且在文件發生變更時,重新讀取Properties文件,並且更新Spring中的Bean。示例代碼如下:
@Configuration @PropertySources(value = {@PropertySource("classpath:config.properties")}) public class AppConfig { @Autowired Environment env; @Bean public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() { PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer(); configurer.setLocation(new ClassPathResource("config.properties")); configurer.setIgnoreResourceNotFound(false); configurer.setIgnoreUnresolvablePlaceholders(false); configurer.setProperties(CommonUtils.loadProperties(configurer.getLocation())); return configurer; } /** * 監聽文件修改 */ @PostConstruct public void init() { String location = env.getProperty("spring.config.location"); if (location != null) { File file = new File(location); if (!file.exists()) { throw new FileNotFoundException("The config file does not exist: " + file.toString()); } if (location.endsWith(".properties")) { new PropertiesFileListener(file, this::reloadProperties).watch(); } } } /** * 重新載入Properties文件 * @param properties Properties */ public void reloadProperties(Properties properties) { PropertySourcesPlaceholderConfigurer configurer = propertySourcesPlaceholderConfigurer(); configurer.setProperties(properties); } .......... }
四、PropertyPlaceholderConfigurer
PropertyPlaceholderConfigurer是Spring框架中的一個核心組件,它是Properties文件中的屬性值佔位符解析器。它提供了多種方式來解析Properties文件中的佔位符,例如,可以使用系統環境變數、標準JRE屬性、命令行參數、JNDI等等方式。通過PropertyPlaceholderConfigurer,我們可以輕鬆地將XML配置文件和Properties文件進行解耦合,從而實現靈活性和可維護性。例如:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:config.properties</value> </list> </property> </bean>
在上述代碼中,我們將Properties文件的配置放置到了Spring的Application Context對象中,並且可以通過佔位符來引用Properties文件中的參數。
五、結合註解使用
除了XML配置之外,Property-placeholder在註解中也得到了廣泛的應用,Spring提供了@Value註解來讀取Properties文件中的配置參數,用法如下:
@Value("${jdbc.driverClassName}") private String driverClassName; @Value("${jdbc.url}") private String url; @Value("${jdbc.username}") private String username; @Value("${jdbc.password}") private String password;
使用@Value註解讀取Properties文件中的配置參數,可以避免在Java代碼中硬編碼參數值,也可以讓配置從不同的環境中進行分離。
總結
Property-placeholder機制在Spring框架中得到了廣泛地應用,在不同的開發場景中起到了不可替代的作用。通過本文的介紹,我們知道,Spring提供了多種載入Properties文件的方式,從XML配置、Java配置到註解方式,我們可以根據實際的開發情況來選擇使用哪種方式。同時,在熱載入方面,我們需要根據實際需求進行調整,以達到最佳的實踐效果。
原創文章,作者:QMRW,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/138678.html