Spring 4.1.6 - 加载“.properties”并用它初始化一个 bean
Spring 4.1.6 - Load ".properties" and initialize a bean with that
我是 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 并且可以在数据库中拥有一个属性 class 比如:
@ManagedAttribute
private ResourcesDB resources;
...
public void foo() {
String dbName = resources.getName();
}
当我尝试时,resources 为空。我做错了什么?
-----更新-----
好的,我可以用给出的答案做一些尝试和失败来解决问题。首先,我更正了 @Value like ("${DB.NAME}") 并在服务注释中添加了一个值@Service(值="Resources")。
然后,我要做的唯一更改是 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。
您的 @Value 语法不正确。应该是@Value("${DB.NAME}")
。
您可能还需要将此添加到您的 XML 配置中:
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:resources/Resources.properties" />
</bean>
位置上的值可能会有所不同,不确定您是如何构建和构建工件的。
您的代码中有 2 个缺陷。
- 你的
@Value
表达式有误
- 您的
<context:property-placeholder />
必须与 bean 处于相同的上下文中
当使用 @Value
时你必须使用占位符,默认情况下 ${property name}
你只是使用一个名字。因此,请更新您的注释以反映这一点。 IE。 @Value("${DB.NAME}
.
接下来,您在 DispatcherServlet
加载的上下文中定义了 <context:property-placeholder />
,而您的 bean 是由 ContextLoaderListener
加载的。 属性 占位符 bean 是一个 BeanFactoryPostProcessor
,它只会对在相同上下文中加载的 bean 定义进行操作。基本上你的 bean 定义在父上下文中,你的占位符在子上下文中。
修复移动 <context:property-placeholder />
到定义 bean 的同一上下文。
而不是 @ManagedAttribute
,这是一个 JSF 注释,使用 @Autowired
或 @Inject
。如果您没有 <context:component-scan />
添加 <context:annotation-driven />
.
我是 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 并且可以在数据库中拥有一个属性 class 比如:
@ManagedAttribute
private ResourcesDB resources;
...
public void foo() {
String dbName = resources.getName();
}
当我尝试时,resources 为空。我做错了什么?
-----更新-----
好的,我可以用给出的答案做一些尝试和失败来解决问题。首先,我更正了 @Value like ("${DB.NAME}") 并在服务注释中添加了一个值@Service(值="Resources")。
然后,我要做的唯一更改是 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。
您的 @Value 语法不正确。应该是@Value("${DB.NAME}")
。
您可能还需要将此添加到您的 XML 配置中:
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:resources/Resources.properties" />
</bean>
位置上的值可能会有所不同,不确定您是如何构建和构建工件的。
您的代码中有 2 个缺陷。
- 你的
@Value
表达式有误 - 您的
<context:property-placeholder />
必须与 bean 处于相同的上下文中
当使用 @Value
时你必须使用占位符,默认情况下 ${property name}
你只是使用一个名字。因此,请更新您的注释以反映这一点。 IE。 @Value("${DB.NAME}
.
接下来,您在 DispatcherServlet
加载的上下文中定义了 <context:property-placeholder />
,而您的 bean 是由 ContextLoaderListener
加载的。 属性 占位符 bean 是一个 BeanFactoryPostProcessor
,它只会对在相同上下文中加载的 bean 定义进行操作。基本上你的 bean 定义在父上下文中,你的占位符在子上下文中。
修复移动 <context:property-placeholder />
到定义 bean 的同一上下文。
而不是 @ManagedAttribute
,这是一个 JSF 注释,使用 @Autowired
或 @Inject
。如果您没有 <context:component-scan />
添加 <context:annotation-driven />
.