java - 如何从applicationContext.xml中的bean读取值?

标签 java xml spring

我需要在我的applicationContext.xml中设置参数值接口(interface)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>

我做错了什么?

最佳答案

我使用适当的类测试了您的 xml 应用程序上下文,并且我从该 main 中获得了预期的 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 丢失了。我不能确定,因为您没有在代码片段中包含参数解析。 在开始构建 spring 上下文(即通过打印它)之前,请确保 ipAddress 存在于 conf 变量中。

我包括了您可能需要有一个工作代码的其余类:

  • 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>
    

关于java - 如何从applicationContext.xml中的bean读取值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21786780/

相关文章:

Java - Swing - JOptionPane 填充

xml - serde-xml-rs 反序列化 u8 有效但 u16 无效

xml - 如何在不进行转义的情况下在 PowerShell 中获取转义的 xml 属性值

java - Spring Security oAuth 提示缺少 JWT 验证程序 key ,即使它已定义

java - 在 Spring 写入/更新属性文件值

java - OpenCSV - 将多个 CSV 列映射到单个 bean 属性

java - 批处理文件未在 Windows 8.1 中执行所需的命令

java - 无法使用调试器附加到正在运行的 spring 集成测试

java - 在FigureCanvas上的现有节点之间绘制PolylineConnection

c++ - 如何使用 tinyxml2 在节点内创建节点?