Spring Bean(错误)配置
我是 Spring 新手,正在尝试实现两个目标:
- 我想将应用程序的配置数据(服务器 IP、凭据等)保存在特定于部署的属性文件(`environment.properties`)中
- 我想使用 Spring将此配置数据依赖项注入到我的应用程序中
首先我们有我的environment.properties文件:
ftpServer=http://example.com/ftp
ftpUser=exampleUser
ftpPassword=examplePassword
接下来是我的Spring配置文件,其中包含我使用的propertyConfigurer bean 相信我已经正确设置:
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="placeholderPrefix" value="${" />
<property name="placeholderSuffix" value="}" />
<property name="locations">
<value>classpath:environment.properties</value>
</property>
</bean>
<bean id="ftpServer" class="java.lang.String">
<constructor-arg type="java.lang.String" value="${ftpServer}"/>
</bean>
<bean id="ftpUser" class="java.lang.String">
<constructor-arg type="java.lang.String" value="${ftpUser}"/>
<constructor-arg></constructor-arg>
</bean>
<bean id="ftpPassword" class="java.lang.String">
<constructor-arg type="java.lang.String" value="${ftpPassword}"/>
</bean>
最后是我希望将特定于部署的数据注入到的源代码:
public void connectToServerAndDoSomething()
{
String strServer = oAppContext.getBean("ftpServer");
String strUser = oAppContext.getBean("ftpUser");
String strPassword = oAppContext.getBean("ftpPassword");
FTPClient oFTP = new FTPClient(strServer, strUser, strPassword);
}
我的问题:
我收到 Spring bean 验证错误我的配置文件:
SEVERE: Exception sending context initialized event to listener instance of class
org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.parsing.BeanDefinitionParsingException:
Configuration problem: Unexpected failure during bean definition parsing
Offending resource: ServletContext resource [/WEB-INF/app-config.xml] Bean 'ftpUser'; nested exception is
org.springframework.beans.factory.parsing.BeanDefinitionParsingException:
Configuration problem: <constructor-arg> element must specify a ref or value
Offending resource: ServletContext resource [/WEB-INF/app-config.xml] Bean 'ftpUser' -> Constructor-arg
由于我是 Spring 新手,我不知道从哪里开始。是不是不可能使用Spring来注入String
之类的Java类型,或者是不可能像我这样使用Spring?无论如何要规避这个吗?
提前致谢!
I'm new to Spring and am trying to accomplish two objectives:
- I want to keep my application's config data (server IPs, credentials, etc.) in a deployment-specific properties file (`environment.properties`)
- I want to use Spring for dependency injection of this config data into my application
So first we have my environment.properties
file:
ftpServer=http://example.com/ftp
ftpUser=exampleUser
ftpPassword=examplePassword
Next my Spring config file, which contains a propertyConfigurer
bean that I believe I have set up correctly:
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="placeholderPrefix" value="${" />
<property name="placeholderSuffix" value="}" />
<property name="locations">
<value>classpath:environment.properties</value>
</property>
</bean>
<bean id="ftpServer" class="java.lang.String">
<constructor-arg type="java.lang.String" value="${ftpServer}"/>
</bean>
<bean id="ftpUser" class="java.lang.String">
<constructor-arg type="java.lang.String" value="${ftpUser}"/>
<constructor-arg></constructor-arg>
</bean>
<bean id="ftpPassword" class="java.lang.String">
<constructor-arg type="java.lang.String" value="${ftpPassword}"/>
</bean>
And finally the source code where I want this deployment-specific data injected into:
public void connectToServerAndDoSomething()
{
String strServer = oAppContext.getBean("ftpServer");
String strUser = oAppContext.getBean("ftpUser");
String strPassword = oAppContext.getBean("ftpPassword");
FTPClient oFTP = new FTPClient(strServer, strUser, strPassword);
}
My question:
I'm getting a Spring bean validation error in my config file:
SEVERE: Exception sending context initialized event to listener instance of class
org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.parsing.BeanDefinitionParsingException:
Configuration problem: Unexpected failure during bean definition parsing
Offending resource: ServletContext resource [/WEB-INF/app-config.xml] Bean 'ftpUser'; nested exception is
org.springframework.beans.factory.parsing.BeanDefinitionParsingException:
Configuration problem: <constructor-arg> element must specify a ref or value
Offending resource: ServletContext resource [/WEB-INF/app-config.xml] Bean 'ftpUser' -> Constructor-arg
Since I'm new to Spring, I don't know where to start. Is it not possible to use Spring to inject Java types like String
s, or is it not possible to use Spring the way I am? Anyway to circumvent this?
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您得到的实际错误是由于 ftpUser bean 的 constructor-arg 元素为空。删除重复项
我还认为您对此的处理有点不正确。典型的方法是为 FTPClient 定义一个 bean,如下所示:
The actual error you get is due to having an empty constructor-arg element for the ftpUser bean. Remove the duplicate
I also think you are going about this a bit incorrectly. The typical way would be to define a bean for the FTPClient like this:
问题是这实际上没有任何意义。 Spring有3种指定方式
就您而言,您根本不传递价值。您可以将其更改为:
一切都会好起来的
The problem is that actualy means nothing. Spring has 3 ways of specifying
In your case you don't pass value at all. You can change it to be:
<constructor-arg value=""/>
and it will be fine
这里有多个问题:
;元素必须指定一个 ref 或 value
这是因为在你的 bean 定义中你有一个 emptu constructor-arg 标签。 xml 文件只是被架构失效,因为该标记必须具有“value”或“ref”属性:删除第二个 constructor-arg 标签。
为了在 .properties 文件中进行参数化,您只需创建该文件并向其添加属性/值,然后在 spring 上下文中您对该文件进行引用,然后使用 ${propertyName} 占位符获取 xml beans 文件中某个属性的值:
There's multiple issues here:
<constructor-arg> element must specify a ref or value
this is due to the fact that in your bean definition you have an emptu constructor-arg tag. The xml file is simply invaliated by the schma as that tag must have a "value" or "ref" attribute:Remove the 2nd constructor-arg tag.
in order to have parametrisation in a .properties file, you simply create that file and add properties/values to it, then in your spring context you make a refference to that file like, and then you use ${propertyName} placeholder to get the value of a certain property in your xml beans file: