Spring Bean(Mis-)配置

时间:2011-10-17 13:42:43

标签: java xml spring dependency-injection

我是Spring的新手,正在努力实现两个目标:

  1. 我想将应用程序的配置数据(服务器IP,凭据等)保存在特定于部署的属性文件中(`environment.properties`)
  2. 我想使用Spring将此配置数据依赖注入我的应用程序
  3. 首先我们有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?无论如何要绕过这个?

    提前致谢!

3 个答案:

答案 0 :(得分:3)

你得到的实际错误是由于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>

答案 1 :(得分:1)

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

  1. 使用ref属性引用另一个bean
  2. 使用值 - 传递简单值。请注意,spring附带一组Convertors,它可以将字符串转换为数字/布尔等...
  3. 使用嵌套bean标记或仅传递值。 在你的情况下,你根本没有传递价值。您可以将其更改为:
  4. &lt; constructor-arg value =“”/&gt;

    它会很好

答案 2 :(得分:0)

这里有多个问题:

  • <constructor-arg> element must specify a ref or value这是因为在你的bean定义中你有一个emptu constructor-arg标签。由于该标记必须具有“value”或“ref”属性,xml文件才会被schma无效:

             

删除第二个构造函数-arg标记。

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

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