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.
XmlBeanDefinitionReaderhave to create aPropertyPlaceholderConfigurerfor you, just because you wrotecontext:property-placeholder? Its function of detecting beans is read. You need to create it manually, and makepostProcessBeanFactory(). See how the tests. - enzoApplicationContexthumanly:ApplicationContext context = new ClassPathXmlApplicationContext("core_config.xml", BuilderInstanceContext.class)? - Roman