I have this strange problem, that Spring does not try to pass defined values to placeholder in constructor-arg. Currently it is defined as ${myProperty}
, but I could write anything there, there are no errors. It just passes the literal string ${myProperty}
to the bean constructor, otherwise the configuration seems to work perfectly.
My beans.xml looks like:
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:property-placeholder order="1" properties-ref="propertiesBean" />
<bean id="propertiesBean" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="properties">
<props>
<prop key="myProperty">Foo</prop>
</props>
</property>
</bean>
<bean id="wrapperBean" class="springapp.bean.Wrapper">
<constructor-arg value="${myProperty}">
</constructor-arg>
</bean>
</beans>
Does anyone have an idea what I am missing with this configuration. Maybe it is something obvious, I don't have much experience with Spring. Using Spring version 3.2.x and WildFly 8.1 as a container.
EDIT:
The beans.xml is loaded like this:
public class TestServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private static final Log log = LogFactory.getLog(TestServlet.class);
private XmlBeanFactory factory;
public void init() throws ServletException {
ClassPathResource resource = new ClassPathResource("beans.xml", TestServlet.class.getClassLoader());
factory = new XmlBeanFactory(resource);
}
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Wrapper bean = (Wrapper) factory.getBean("wrapperBean");
String value = bean.inner.value;
resp.getWriter().print(value);
}
}
Your loading is flawed you should use an ApplicationContext
instead of a BeanFactory
.
public class TestServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private static final Log log = LogFactory.getLog(TestServlet.class);
private ApplicationContext ctx;
public void init() throws ServletException {
ctx = new ClassPathXmlApplicationContext("beans.xml");
}
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Wrapper bean = ctx.getBean("wrapperBean", Wrapper.class);
String value = bean.inner.value;
resp.getWriter().print(value);
}
}
For the differences check the reference guide.