spring装配时候配置的自动注入

配置文件

 

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;

}  

   

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值