如何在applicationContext.xml中读取bean的值?

时间:2014-02-14 18:24:02

标签: java xml spring

我需要在applicationContext.xml

中设置参数值接口IP地址

我从属性文件中读取此设置,并以这种方式使用它:

<bean id="hazelcastInterface" class="com.hazelcast.config.InterfacesConfig">
        <property name="interfaces">
            <list>
                <value>${interface.ip_address}</value>
            </list>
        </property>
        <property name="enabled" value="true" />
    </bean>

现在我需要从命令行参数中获取此值。我使用Apache Commons CLI解析器,解析参数并从中创建自己的bean commandLineConf并将其设置为ApplicationContext。

ExternalBeanReferneceFactoryBean.setInstance("commandLineConf", conf);
beanFactory.registerBeanDefinition(
    "commandLineConf",
    BeanDefinitionBuilder.rootBeanDefinition(
        ExternalBeanReferneceFactoryBean.class)
        .getBeanDefinition());

GenericApplicationContext rootAppContext = new GenericApplicationContext(
    beanFactory);
rootAppContext.refresh();

但我不知道如何在applicationContext.xml中从这个bean获取值。我尝试了很多方法,例如但它对我不起作用。

<bean id="hazelcastInterface" class="com.hazelcast.config.InterfacesConfig">
        <property name="interfaces">
            <list>
                <value>#{commandLineConf.ipAddress}</value>
            </list>
        </property>
        <property name="enabled" value="true" />
    </bean>

我做错了什么?

1 个答案:

答案 0 :(得分:1)

我用适当的类测试了你的xml应用程序上下文,我从这个主程序中获得了预期的ipAddress:

public static void main(String[] args) {

        CommandLineConf conf = new CommandLineConf();
        conf.setIpAddress("127.0.0.1");
        // create root beanFactory
        DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();

        // register bean definition for the command line
        ExternalBeanReferneceFactoryBean.setInstance("commandLineConf", conf);
        beanFactory.registerBeanDefinition(
            "commandLineConf",
            BeanDefinitionBuilder.rootBeanDefinition(
                ExternalBeanReferneceFactoryBean.class)
                .getBeanDefinition());

        GenericApplicationContext rootAppContext = new GenericApplicationContext(
            beanFactory);
        rootAppContext.refresh();

        // create the application context
        ApplicationContext appContext = new ClassPathXmlApplicationContext(new String[] { 
            "/applicationContext.xml"
        }, rootAppContext);

        InterfacesConfig hazelcastInterface = (InterfacesConfig)appContext.getBean("hazelcastInterface");
        System.out.println(hazelcastInterface.getInterfaces().get(0));

    }

所以你使用正确的语法来引用地址,即:#{commandLineConf.ipAddress}

这让我觉得问题出在conf变量中。您的代码没有显示它是如何填充的,我怀疑缺少ipAddress。我无法确定,因为你没有在你的snipplet中包含解析参数。 在开始构建spring上下文之前(例如通过打印它),确保confAddress中存在ipAddress。

我包含您可能需要的其余课程,以获得有效的代码:

  • InterfacesConfig.java

    public class InterfacesConfig {
        private List<String>interfaces;
        private boolean enabled;
    
        public List<String> getInterfaces() {
            return interfaces;
        }
    
        public void setInterfaces(List<String> interfaces) {
            this.interfaces = interfaces;
        }
        public boolean isEnabled() {
            return enabled;
        }
    
        public void setEnabled(boolean enabled) {
            this.enabled = enabled;
        }
    
    }
    
  • CommandLineConf.java

    public class CommandLineConf {
        private String ipAddress;
        public String getIpAddress() {
            return ipAddress;
        }
    
        public void setIpAddress(String ipAddress) {
            this.ipAddress = ipAddress;
        }
    }
    
  • ExternalBeanReferneceFactoryBean.java

    import java.util.HashMap;
    import java.util.Map;
    
    import org.springframework.beans.factory.BeanNameAware;
    import org.springframework.beans.factory.config.AbstractFactoryBean;
    
    public class ExternalBeanReferneceFactoryBean extends AbstractFactoryBean implements BeanNameAware {
    
        private static Map<String, Object> instances = new HashMap<String, Object>();
        private String beanName;
    
        /**
         * @param instance the instance to set
         */
        public static void setInstance(String beanName, Object instance) {
            instances.put(beanName, instance);
        }
    
        @Override
        protected Object createInstance() 
            throws Exception {
            return instances.get(beanName);
        }
    
        @Override
        public Class<?> getObjectType() {
            return instances.get(beanName).getClass();
        }
    
        @Override
        public void setBeanName(String name) {
            this.beanName = name;
        }
    
    }
    
  • applicationContext.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    <bean id="hazelcastInterface" class="com.hazelcast.config.InterfacesConfig">
            <property name="interfaces">
                <list>
                    <value>#{commandLineConf.ipAddress}</value>
                </list>
            </property>
            <property name="enabled" value="true" />
        </bean>
    </beans>