Spring Bean(错误)配置

发布于 2024-12-10 11:59:19 字数 2504 浏览 0 评论 0原文

我是 Spring 新手,正在尝试实现两个目标:

  1. 我想将应用程序的配置数据(服务器 IP、凭据等)保存在特定于部署的属性文件(`environment.properties`)中
  2. 我想使用 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:

  1. I want to keep my application's config data (server IPs, credentials, etc.) in a deployment-specific properties file (`environment.properties`)
  2. 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 Strings, or is it not possible to use Spring the way I am? Anyway to circumvent this?

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

嘿看小鸭子会跑 2024-12-17 11:59:19

您得到的实际错误是由于 ftpUser bean 的 constructor-arg 元素为空。删除重复项

<constructor-arg></constructor-arg>

我还认为您对此的处理有点不正确。典型的方法是为 FTPClient 定义一个 bean,如下所示:

<bean id="ftpClient" class="your.package.FTPClient">
   <constructor-arg value="${ftpServer}" />
   <constructor-arg value="${ftpUser}" />
   <constructor-arg value="${ftpPassword}" />
</bean>

The actual error you get is due to having an empty constructor-arg element for the ftpUser bean. Remove the duplicate

<constructor-arg></constructor-arg>

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:

<bean id="ftpClient" class="your.package.FTPClient">
   <constructor-arg value="${ftpServer}" />
   <constructor-arg value="${ftpUser}" />
   <constructor-arg value="${ftpPassword}" />
</bean>
凤舞天涯 2024-12-17 11:59:19

问题是这实际上没有任何意义。 Spring有3种指定方式

  1. 使用 ref 属性引用另一个 bean
  2. 使用值 - 传递简单值。请注意,spring 附带了一组转换器,它可以将字符串转换为数字/布尔值等...
  3. 使用嵌套 bean 标签或仅传递值。
    就您而言,您根本不传递价值。您可以将其更改为:

一切都会好起来的

The problem is that actualy means nothing. Spring has 3 ways of specifying

  1. Using ref attribute to reference another bean
  2. Using value - passing simple value. Note that spring comes with a pack of Convertors, which can convert string to numbers/booleans etc...
  3. Using nested bean tag or just passing the value.
    In your case you don't pass value at all. You can change it to be:

<constructor-arg value=""/>

and it will be fine

雨落□心尘 2024-12-17 11:59:19

这里有多个问题:

  • ;元素必须指定一个 ref 或 value 这是因为在你的 bean 定义中你有一个 emptu constructor-arg 标签。 xml 文件只是被架构失效,因为该标记必须具有“value”或“ref”属性:

删除第二个 constructor-arg 标签。

  • 为了在 .properties 文件中进行参数化,您只需创建该文件并向其添加属性/值,然后在 spring 上下文中您对该文件进行引用,然后使用 ${propertyName} 占位符获取 xml beans 文件中某个属性的值:

    ;
    <属性名称=“位置”>
      <列表>
          <值>类路径:my.properties
      
    
    

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:

    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
      <list>
          <value>classpath:my.properties</value>
      </list>
    </property>
    

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文