今天在做練習(xí)的時(shí)候,有幾個(gè)變量在controller層寫死了,這樣是不好的,因此將它提取到外部的配置文件中,Controller直接引用即可。
代碼如下:
1. 提取變量到外部配置文件
baseDict.properties :
industryType.code=001
fromType.code=002
levelType.code=006
2. 在springmvc.xml中引入外部配置文件:
<!--注入外部配置文件-->
<context:property-placeholder location="classpath:resources/basedictcode.properties"/>
3. 在Controller層使用
@Value("${industryType.code}")
private String industryTypeCode;
@Value("${fromType.code}")
private String fromTypeCode;
@Value("${levelType.code}")
private String levelTypeCode;
這樣就可成功讀取到配置文件中定義的值了,這里要注意的是:
由于父子容器的關(guān)系迈勋,Spring容器不能讀取到springmvc容器的內(nèi)容,Springmvc可以讀取到Spring的內(nèi)容,
因此即使在application-dao/service.xml等屬于spring容器的配置文件中引入了配置文件胎撇,例如 *.properties也不能生效麸粮。
同樣配置注解掃描也是一樣,但是在springmvc.xml中就可以成功蛮放。