[英]How to import applicationContext.xml and Bean from external jar to @SpringBootTest?
[英]How to read value from bean in applicationContext.xml?
我需要在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>
我究竟做錯了什么?
我使用適當的類測試了您的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是否丟失。 我不確定,因為您沒有在代碼段中包含解析的參數。 開始構建spring上下文之前,請確保conf變量中存在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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.