There is a class loading XML in which bins are described

public class BuilderInstanceContext { static DefaultListableBeanFactory factory = new DefaultListableBeanFactory(); static XmlBeanDefinitionReader xml_bdr = new XmlBeanDefinitionReader(factory); static { xml_bdr.setValidating(false); loadXmlBeanDefinitionReader( BuilderInstanceContext.class.getClassLoader().getResourceAsStream("core_config.xml")); } public static void loadXmlBeanDefinitionReader(InputStream is) { System.out.println("loadBeanDefinitions : " + xml_bdr.loadBeanDefinitions(new InputStreamResource(is))); } public static DefaultListableBeanFactory getContext() { return factory; } } 

This works if the values ​​in the xml are statically written.

If in a descriptive file I use

 <context:property-placeholder location="config/config.properties"/> 

and I describe the beans so

 <bean id="data1" class="ru.firston.ms.develop_1.Data"> <property name="value" value="${URL_TEMPLATE_SERVICE}"/> </bean> 

then when performing:

 BuilderInstanceContext.loadXmlBeanDefinitionReader(TestLoadFile.class.getClassLoader().getResourceAsStream(path_example)); Data data = (Data) BuilderInstanceContext.getContext().getBean("data1"); System.out.println(data.getValue()); 

I get to the console: ${URL_TEMPLATE_SERVICE}


Enzo, thanks. With Spring, while everything is new, I tried the following

 public static void loadXmlBeanDefinitionReader(InputStream is, Resource...resources){ PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer(); ppc.setIgnoreResourceNotFound(true); ppc.setIgnoreUnresolvablePlaceholders(true); ppc.setLocations(resources); ppc.postProcessBeanFactory(factory); System.out.println("loadBeanDefinitions : " + xml_bdr.loadBeanDefinitions(new InputStreamResource(is))); } 

but it did not solve the problem apparently missed something else. The parameters of the file *. Properties output, with them all the buzz. I tried to add through

 ppc.setLocations(resources); ppc.setProperties(properties); 

As I understand, there is not enough binding of PropertyPlaceholderConfigurer and DefaultListableBeanFactory , but in the contents of the context an object of class PropertyPlaceholderConfigurer present.

  • It seems logical. Does an XmlBeanDefinitionReader have to create a PropertyPlaceholderConfigurer for you, just because you wrote context:property-placeholder ? Its function of detecting beans is read. You need to create it manually, and make postProcessBeanFactory() . See how the tests. - enzo
  • Why not create an ApplicationContext humanly: ApplicationContext context = new ClassPathXmlApplicationContext("core_config.xml", BuilderInstanceContext.class) ? - Roman

1 answer 1

Here is an example of what you are trying to do. I can not imagine why, but still.

 src β”œβ”€β”€ main β”‚  β”œβ”€β”€ java β”‚  β”‚  └── example β”‚  β”‚  β”œβ”€β”€ app.properties β”‚  β”‚  β”œβ”€β”€ config.xml β”‚  β”‚  β”œβ”€β”€ Data.java β”‚  β”‚  └── Main.java 

config.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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="data" class="example.Data"> <property name="value" value="${URL_TEMPLATE_SERVICE}" /> </bean> </beans> 

app.properties

 URL_TEMPLATE_SERVICE = http:/localhost/templates 

Data.class

 public class Data { private String value; public String getValue() { return value; } public void setValue(String value) { this.value = value; } } 

Main.class

 public class Main { public static void main(String[] args) throws Exception { DefaultListableBeanFactory factory = new DefaultListableBeanFactory(); XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(factory); reader.loadBeanDefinitions(new ClassPathResource("config.xml", Main.class)); PropertyPlaceholderConfigurer props = new PropertyPlaceholderConfigurer(); props.setLocation(new ClassPathResource("app.properties", Main.class)); props.postProcessBeanFactory(factory); Data data = (Data) factory.getBean("data"); System.out.println(data.getValue()); // http:/localhost/templates } }