【发布时间】:2015-09-20 14:15:46
【问题描述】:
我是 Spring MVC 的新手,我正在做一些测试。我试图找到有关此问题的一些答案,但其中大多数都引用了 Spring 3.11,而我使用的是最后一个版本:4.1.6。
我想在应用程序启动时加载一个“.properties”文件,并使用其中的信息创建一个 bean 以在应用程序的所有上下文中访问它。
到目前为止,我在 servlet-context.xml
中加载文件<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
...
<context:property-placeholder location="classpath*:resources/Resources.properties" />
</beans:beans>
我认为(不太确定)我在 root-context.xml 中正确声明了 bean:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- Root Context: defines shared resources visible to all other web components -->
<bean id="Resources" class="ar.com.violenciaesmentir.blog.resources.ResourcesDB"/>
</beans>
而且我也认为我正确地制作了bean,但我真的不知道注释是否正确:
package ar.com.violenciaesmentir.blog.resources;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
@Service
public class ResourcesDB {
@Value("DB.NAME")
private String name;
@Value("DB.TYPE")
private String type;
@Value("DB.USER")
private String user;
@Value("DB.PASS")
private String pass;
@Value("DB.DRIVER")
private String driver;
@Value("DB.URL")
private String url;
@Value("DB.MAXACTIVE")
private String maxActive;
@Value("DB.MAXIDLE")
private String maxIdle;
@Value("DB.MAXWAIT")
private String maxWait;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public String getPass() {
return pass;
}
public void setPass(String pass) {
this.pass = pass;
}
public String getDriver() {
return driver;
}
public void setDriver(String driver) {
this.driver = driver;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getMaxActive() {
return maxActive;
}
public void setMaxActive(String maxActive) {
this.maxActive = maxActive;
}
public String getMaxIdle() {
return maxIdle;
}
public void setMaxIdle(String maxIdle) {
this.maxIdle = maxIdle;
}
public String getMaxWait() {
return maxWait;
}
public void setMaxWait(String maxWait) {
this.maxWait = maxWait;
}
}
我的“.properties”文件:
DB.NAME = jdbc/Blog
DB.TYPE = javax.sql.DataSource
DB.USER = blog
DB.PASS = blog
DB.DRIVER = oracle.jdbc.driver.OracleDriver
DB.URL = jdbc:oracle:thin:@localhost:1521:xe
DB.MAXACTIVE = 20
DB.MAXIDLE = 5
DB.MAXWAIT = 10000
我认为引用是可以的,因为它在启动服务器时给我带来了麻烦,说它找不到“name”的属性,但是我做错了注释,然后我修复了。
我想要的是初始化该 bean 并可以在 DB 类中拥有一个属性,例如:
@ManagedAttribute
private ResourcesDB resources;
...
public void foo() {
String dbName = resources.getName();
}
当我尝试时,resources 为空。我做错了什么?
-----更新-----
好的,我可以通过给出的答案进行一些尝试和失败来解决问题。首先,我更正了 ("${DB.NAME}") 之类的 @Value,并在服务注解 @Service(value= “资源”)。
然后,我要做的唯一更改是在 servlet-context.xml 中。而不是:
<context:property-placeholder location="classpath*:resources/Resources.properties" />
我用过:
<beans:bean id="configuracion" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<beans:property name="location" value="classpath:Resources.properties"/>
</beans:bean>
并使用 @Autowire 而不是 @ManagedBean 来访问 bean。
【问题讨论】:
-
寻找PropertyPlaceholderConfigurer,你应该使用"${db.user}"
-
你应该看看 Spring Boot 和
@ConfigurationProperties。
标签: java spring managed-bean properties-file