2014-02-14 56 views
0

我需要在我的applicationContext.xml設置參數值接口IP地址如何從applicationContext.xml中的bean讀取值?

我讀這個設置從屬性文件我用它這種方式:

<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(); 

但我不知道如何從這個bean在applicationContext.xml中獲得價值。我嘗試了很多方法,例如但它對我不起作用。

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

我在做什麼錯?

+0

您是否嘗試過這樣的:http://stackoverflow.com/questions/132231/dealing-with-command-line-arguments-and-spring? –

+0

是的,我的源代碼的一部分是從這個問題... – user2148736

回答

1

我用適當的類測試你的XML應用程序上下文,我不得不從這個主預期的ip地址:

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是否丟失。我不能確定,因爲你沒有在你的snipplets中解析參數。 在開始構建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> 
    
+0

不,它不工作......我已經簡化了我的代碼最大化,問題是當我試圖從'ClassPathXmlApplicationContext'目的。 Spring拋出該bean不存在的錯誤 - 「線程中的異常」main「org.springframework.beans.factory.NoSuchBeanDefinitionException:沒有名爲'clc'的bean被定義爲 '。但代碼與你的完全一樣:)我不知道它有什麼問題。 – user2148736

+0

看起來您正在引用未聲明的bean。我需要看你的applicationContext.xml來幫助你。你可以發佈嗎? –

+0

我正在使用非常空的applicationContext.xml文件。這就是問題,我試圖獲得的bean(clc)在Java代碼中以您提到的相同方式聲明。這很奇怪。 – user2148736