配置文件
spring装配时候配置的自动注入
1,spring需要特俗的类加载这个目录的配置文件(需手动配置)
1.1java中用
用factorybean方式
<bean id="prop" class="org.springframework.beans.factory.config.PropertiesFactoryBean" p:location="classpath:db.properties"/>
@Value("#{prop['server.cn']}")
private String cnWebUrl;
@RequestMapping(value = "/addConObj")
public String addConObj(Model model) {
String language = CASUtil.getCustomer().getLanguage();
if ("CN".equals(language)) {
return "redirect:" +cnWebUrl + "/offer/addConObj";
}
}
db.properties
server.cn=http://10.0.1.222:8080/web
server.en=http://10.0.1.214:8080/web
1.2配置文件中用
<context:property-placeholder location="classpath:db.properties" />
这种适用于配置文件中用通配符取
2,springboot自动加载相应目录下的配置文件(自动配置好这个加载类)
@Configuration
@ConfigurationProperties
@PropertySource("classpath:paprocesser.properties") //只适用.properties,yml不行
public class ProcesserProperty extends Properties{
}
//或
@ConfigurationProperties(prefix = "foo")
@Bean
public FooComponent fooComponent() {
...
}
paprocesser.propertie
1310=inMoneyProcesser
//获取
String processerBeanName = processerProperty.getProperty("1310");
3,直接用@Value("${pa.url}")拿
@Value("${pa.url}")
private String paUrl ;
/////////////配合get,set方法供外部调用,拿配置
有springboot有引入这个属性加载类的话可以直接
@Component
public class CasProperties {
@Value("${cas.server.host.url}")
private String casServerUrl;
@Value("${cas.server.host.login_url}")
private String casServerLoginUrl;
@Value("${cas.server.host.logout_url}")
private String casServerLogoutUrl;
@Value("${app.server.host.url}")
private String appServerUrl;
@Value("${app.login.url}")
private String appLoginUrl;
@Value("${app.logout.url}")
private String appLogoutUrl;
public String getCasServerUrl() {
return casServerUrl;
}
public void setCasServerUrl(String casServerUrl) {
this.casServerUrl = casServerUrl;
}
public String getCasServerLoginUrl() {
return casServerLoginUrl;
}
public void setCasServerLoginUrl(String casServerLoginUrl) {
this.casServerLoginUrl = casServerLoginUrl;
}
public String getCasServerLogoutUrl() {
return casServerLogoutUrl;
}
public void setCasServerLogoutUrl(String casServerLogoutUrl) {
this.casServerLogoutUrl = casServerLogoutUrl;
}
public String getAppServerUrl() {
return appServerUrl;
}
public void setAppServerUrl(String appServerUrl) {
this.appServerUrl = appServerUrl;
}
public String getAppLoginUrl() {
return appLoginUrl;
}
public void setAppLoginUrl(String appLoginUrl) {
this.appLoginUrl = appLoginUrl;
}
public String getAppLogoutUrl() {
return appLogoutUrl;
}
public void setAppLogoutUrl(String appLogoutUrl) {
this.appLogoutUrl = appLogoutUrl;
}
}