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;
}
}
上一篇: contents获取网页标题的代码及两者效率的稳定性问题
下一篇: 冒泡排序、快速排序,归并排序
推荐阅读
-
Linux系统实现ansible自动化安装配置httpd的方法
-
@Autowired 和 @Resource注解, 一个接口有多个实现类的时候Spring注入遇到的问题
-
【Spring】Bean的自动装配
-
Spring 自动装配,Java 配置,XML配置、混合使用
-
【6】Spring5中Bean的自动装配
-
Spring入门 IOC理论,第一个spring程序Di依赖注入,Bean的自动装配
-
spring-依赖注入(DI)&控制反转(Ioc)&Bean的三种装配方式
-
SpringBoot使用logback自定义配置时遇到的坑 --- 在 /tmp目录下自动生成spring.log文件
-
SpringBoot 源码解析 (五)----- Spring Boot的核心能力 - 自动配置源码解析
-
spring boot 系列之六:@Conditional和spring boot的自动配置