Spring PropertyPlaceholderConfigurer獲取屬性檔案值
PropertyPlaceholderConfigurer是個bean工廠後置處理器的實現,也就是 BeanFactoryPostProcessor介面的一個實現。PropertyPlaceholderConfigurer可以將上下文(配置文 件)中的屬性值放在另一個單獨的標準java Properties檔案中去。在XML檔案中用${key}替換指定的properties檔案中的值。這樣的話,只需要對properties檔案進 行修改,而不用對xml配置檔案進行修改。
屬性檔案
以app.properties為例:
配置key-value:
filePath = /Users/loukai /easylife/files/
自定義屬性載入工具類
該類必須要繼承 PropertyPlaceholderConfigurer類(實際是對PropertyPlaceholderConfigurer類的一個擴充套件)
public class AppConfigUtil extends PropertyPlaceholderConfigurer{
private static Map<String, Object> ctxPropertiesMap;
/**
* 重寫processProperties方法
*/
@Override
protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props) throws BeansException {
super.processProperties(beanFactoryToProcess, props);
ctxPropertiesMap = new HashMap<String, Object>();
for (Object key : props.keySet()) {
String keyStr = key.toString();
String value = props.getProperty(keyStr);
ctxPropertiesMap.put(keyStr, value);
}
}
public static Object getContextProperty(String name) {
return ctxPropertiesMap.get(name);
}
public static Object setContextProperty(String name,Object value) {
return ctxPropertiesMap.put(name, value);
}
public static Map<String, Object> getContextPropertyMap() {
return ctxPropertiesMap;
}
}
applicationContext.xml配置
往spring容器中注入bean
<bean id="appConfigUtil" class="com.easylife.core.util.AppConfigUtil">
<property name="locations">
<list>
<value>classpath*:app.properties</value>
</list>
</property>
</bean>
使用
AppConfigUtil.getContextProperty("filePath");
其他相關
相關推薦
Spring PropertyPlaceholderConfigurer獲取屬性檔案值
PropertyPlaceholderConfigurer是個bean工廠後置處理器的實現,也就是 BeanFactoryPostProcessor介面的一個實現。PropertyPlaceholderConfigurer可以將上下文(配置文 件)中的屬性
Spring Boot入門(二):獲取配置檔案值
本篇部落格主要講解下在Spring Boot中如何獲取配置檔案的值。 1. 使用yaml配置檔案 Spring Boot預設生成的配置檔案為application.properties,不過它也支援yaml語言的配置檔案, 兩者之間的差別並不是很大,只是yaml語言的配置檔案層次結構更明顯,可讀性更強,因此目
SpringBoot用@ConfigurationProperties獲取配置檔案值
SpringBoot的配置檔案有yml和properties兩種,看一些文章說yml以資料為中心,比較好。個人覺得properties更好用,所以這裡以properties格式為例來說。 我們都知道@Value 註解可以從配置檔案讀取一個配置,如果只是配置某個值,比如 某一個域名,配置為
spring boot獲取屬性檔案內容
spring boot屬性檔案有兩者:1、以.properties為字尾名; 2、以.yml為字尾名 @ConfigurationProperties(prefix = "ebay",locations = "classpath:business/ebay.propert
Spring MVC(十三)--保存並獲取屬性參數
信息 sta ams val esp 路徑 也會 user 方法 這裏的屬性參數主要是指通過request、session、cookie等設置的屬性,有時候我們需要將一些請求的參數保存到HTTP的request或者session對象中去,在控制器中也會進行設置和獲取操作,s
Spring(20) 獲取別的bean的屬性
嗯。。。就是在配置檔案中一個bean把別人的屬性拿過來用 通過將class指定為PropertyFactoryBean來實現獲取其他bean的屬性 在程式碼裡邊說吧,總而言之呢就是把class指定為ProperFactory,然後再指定是要獲取那個bean的哪個屬性package
Spring @Value注入屬性獲取不到值
今天在Spring boot中使用@Value發現獲取不到值,但在另一個類中@Value註解確認獲取到,我的程式碼如下: @Component public class EsClientWithSG { private TransportClien
spring aop 獲取攔截類中的屬性的值
最近搭建一個ssh的通用架子時,想做個通用日誌的攔截方法,可以利用反射獲取攔截類中的屬性 不多說,上程式碼 日誌類 public class MyLog { public void doBefore(JoinPoint jp) throws SecurityExcepti
spring mvc controller中獲取屬性檔案(properties)的值
1.將properties檔案位置加到spring mvc的配置的檔案(spring-mvc.xml)中,如下 <bean id="placeholderConfig" class="org.springframework.beans.facto
Spring Boot通過application.yml配置文件獲取屬性及類信息
boot 實體類 ping override ria pro tid @value attr 實體類信息**@ConfigurationProperties(prefix="mycar")**br/>直接獲取單個屬性@Value("${mycar.carNo}")
Spring中使用屬性文件properties的兩種方式
文件 location 郵件發送 class mave red onf 路徑 文件內容 實際項目中,通常將可配置的參數放到屬性文件中,例如數據庫連接信息、redis連接信息等,便於統一管理。然後通過IoC框架spring將其加載到上下文中,使得程序可以直接使用。 創建mys
JQuery獲取屬性值
zhang 文本 引號 rop 屬性 false vid 標簽 als $("#divid").text(); //獲取id為divid下的文本內容 $("#divid").attr("name"); //獲取id為divid標簽的name的值 $("#divid").
一個獲取獲取文檔節點的問題
段落 chan logs function html tag color [0 inner 我想獲取一下p段落,通過JS代碼更改其中的內容,代碼如下: 1 <p>第一個段落</P> 2 <button onclick=" changeP()
Spring經常使用屬性的註入及屬性編輯器
edt 註入 nim jsm fin v2x avt ket jsf 對於對象的註入,我們使用ref方式,能夠指定註入的對象。以下看下對於基本類型的註入。以及當spring無法轉換基本類型進行註入時,怎樣編寫一個相似轉換器的東西來完畢註
spring mvc獲取絕對路徑的幾種方法
col 文件 結果 ret 項目 tps span ppr etc 1.首先如果是在一個controller方法中,則很簡單,直接用下面語句。 1 @RequestMapping("categoryHome") 2 public ModelAndVie
SCRIPT5007:無法獲取屬性“show”的值,對象為null或沒有定義
dojo show post nbsp gb2 data scrip con style 1、錯誤描寫敘述 SCRIPT5007:無法獲取屬性“show”的值,對象為null或沒有定義 dojo.js,行15.字符11808 2、錯誤原因
jQuery獲取屬性
html prop pre class 對象 jquer light 常用 jquery對象 jQuery在獲取jQuery對象的屬性時,出現attr()獲取不到的情況,此時,請使用prop()獲取 如下為經常用到的: var oHtml=$(this).pr
從package.json中獲取屬性
pack tps .json over href from version span package var pjson = require(‘./package.json‘); console.log(pjson.version); 詳見:https://stack
Spring中獲取Session的方法匯總
ttr public urn 過去 ets red ole list details Spring: web.xml <listener> <listener-class>org.springframework.web.context.
spring boot?Swagger2文檔構建及單元測試
int 5.6 n) post 參數 odin mat ace 關註 首先,回顧並詳細說明一下在快速入門中使用的@Controller、@RestController、@RequestMapping註解。如果您對Spring MVC不熟悉並且還沒有嘗試過快速入門案例,建議先