Java 弹簧4.1.6-负载;。物业;并用它初始化一个bean
我是SpringMVC的新手,正在做一些测试。我试图找到一些关于这个问题的答案,但大多数都参考了Spring3.11,我使用的是最新版本:4.1.6 我想在应用程序启动时加载一个“.properties”文件,并使用其中的信息创建一个bean,以便在应用程序的所有上下文中访问它 到目前为止,我试图在servlet context.xml中加载该文件Java 弹簧4.1.6-负载;。物业;并用它初始化一个bean,java,spring,managed-bean,properties-file,Java,Spring,Managed Bean,Properties File,我是SpringMVC的新手,正在做一些测试。我试图找到一些关于这个问题的答案,但大多数都参考了Spring3.11,我使用的是最新版本:4.1.6 我想在应用程序启动时加载一个“.properties”文件,并使用其中的信息创建一个bean,以便在应用程序的所有上下文中访问它 到目前为止,我试图在servlet context.xml中加载该文件 <?xml version="1.0" encoding="UTF-8"?> <beans:beans xmlns="http:/
<?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>
我的“.properties”文件:
我认为引用是可以的,因为它在启动服务器时给我带来了麻烦,说它找不到“name”的属性,但我做了错误的注释,然后我修复了它
我想要的是初始化该bean,并使其在DB类中具有如下属性:
@ManagedAttribute
private ResourcesDB resources;
...
public void foo() {
String dbName = resources.getName();
}
当我尝试时,资源为空。我做错了什么
-----更新----好的,我可以用给出的答案来解决这个问题。首先,我更正了@Value像(“${DB.NAME}”)一样,并向服务注释@service(Value=“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(${DB.NAME}”)
您可能还需要将以下内容添加到XML配置中:
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:resources/Resources.properties" />
</bean>
位置上的值可能会有所不同,不确定您是如何构造和构建工件的。您的代码中有两个缺陷
@Value
表达式错误
必须与bean位于相同的上下文中@Value
时,您必须使用占位符,默认情况下${property name}
您只是在使用一个名称。因此,请更新注释以反映这一点。即@Value(${DB.NAME}
接下来,您已经在由DispatcherServlet
加载的上下文中定义了
,而您的bean是由ContextLoaderListener
加载的。属性占位符bean是一个BeanFactoryPostProcessor
,它将只对在相同上下文中加载的bean定义进行操作。基本上,您的bean定义是在父上下文中,占位符在子上下文中
要修复此问题,请将
移动到定义bean的同一上下文中
使用
@Autowired
或@Inject
代替JSF注释的@ManagedAttribute
。如果没有
,请添加
查找PropertyPlaceHolderConfigure,您应该使用“${db.user}”“您应该查看Spring Boot和@ConfigurationProperties
。我刚开始就尝试了,但没有成功。无论如何,我会再试一次,但当我这样做时:@ManagedAttribute private ResourcesDB resource;给了我编译错误:注释“@ManagedAttribute”此位置不允许使用。嗯,我确定这就是问题所在。更新了答案。您的bean定义与
相同。我用解决方案更新了问题,部分原因是这样的,所以非常感谢。只是,在Spring 4.1.6中,bean的定义似乎是“”。再次感谢。@AndrésMarotta不,这并不取决于您作为根命名空间添加了什么,如果它是另一个Springbeans.xsd
,那么您需要为bean
元素(以及该xsd中的所有其他元素)添加前缀使用您选择的名称空间。这与spring无关,只不过是xml的工作方式。很抱歉回答了这么多问题,我正在工作。我按照您所说的做,在servlet-context.xml中定义了bean:
'`并编写类似(“${DB.NAME}”)的值我有一个异常:嵌套异常是java.lang.IllegalArgumentException:无法解析字符串值“${DB.NAME}”中的占位符“DB.NAME”
<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>
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:resources/Resources.properties" />
</bean>