Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I use spring property sources within a child annotation context?
    primarykey
    data
    text
    <p>I'm trying to replicate something that works with XML config using annotated configuration classes. The problem I'm hitting is that property sources defined in the child context are not accessible.</p> <p>The xml that works looks like</p> <p><strong>Parent context :</strong></p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;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.xsd"&gt; &lt;bean id="proxyChannelQueue" class="java.util.concurrent.ArrayBlockingQueue"&gt; &lt;constructor-arg value="10"/&gt; &lt;/bean&gt; &lt;/beans&gt; </code></pre> <p><strong>Child Context :</strong></p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;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" xmlns:p="http://www.springframework.org/schema/p" 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"&gt; &lt;context:property-placeholder location="proxy-host.properties"/&gt; &lt;bean class="org.eclipse.jetty.server.Server" p:handler-ref="proxyHostHandler" init-method="start"&gt; &lt;constructor-arg value="${proxyHostPort}"/&gt; &lt;/bean&gt; &lt;bean id="proxyHostHandler" class="com.sjl.web.ProxyHostHandler" p:proxyChannelQueue-ref="proxyChannelQueue"/&gt; &lt;/beans&gt; </code></pre> <p><strong>Start up code :</strong> </p> <pre><code> ClassPathXmlApplicationContext parentContext = new ClassPathXmlApplicationContext("test/parent-context.xml"); ClassPathXmlApplicationContext childContext = new ClassPathXmlApplicationContext(new String[] {"test/child-context.xml"}, parentContext); </code></pre> <p>My attempt at doing this using configuration classes looks like.</p> <p><strong>Parent Context :</strong></p> <pre><code>@Configuration public class ParentConfiguration { @Bean(name = "proxyChannelQueue") public BlockingQueue&lt;ProxyChannel&gt; getProxyChannelQueue() { return new ArrayBlockingQueue&lt;ProxyChannel&gt;(10); } } </code></pre> <p><strong>Child Context :</strong></p> <pre><code>@Configuration @PropertySource("classpath:proxy-host.properties") public class ChildContext { private static final Logger LOGGER = LoggerFactory.getLogger(ChildContext.class); @Autowired private Environment environment; @Resource(name = "proxyChannelQueue") private BlockingQueue&lt;ProxyChannel&gt; proxyChannelQueue; public static void main(String[] args) { new HierarchicalAnnotationConfigApplicationContext(ChildContext.class); } @Bean public Server getJettyServer() throws Exception { int proxyHostPort = environment.getProperty("proxyHostPort", Integer.class); Server server = new Server(proxyHostPort); server.setHandler(getHandler()); server.start(); return server; } @Bean public Handler getHandler() { ProxyHostHandler proxyHostHandler = new ProxyHostHandler(); proxyHostHandler.setProxyChannelQueue(proxyChannelQueue); return proxyHostHandler; } } </code></pre> <p><strong>Start up code :</strong></p> <pre><code>AnnotationConfigApplicationContext parentContext = new AnnotationConfigApplicationContext(ParentConfiguration.class); AnnotationConfigApplicationContext childContext = new AnnotationConfigApplicationContext(); childContext.setParent(parentContext); childContext.register(ChildContext.class); childContext.refresh(); </code></pre> <p>I'm getting a null pointer while trying to retrieve the proxyHostPort within the ChildContext getJettyServer method. Inspecting the environment variable shows that it contains only 2 property sources (systemProperties and systemEnvironment) and not the 3 I expect.</p> <p>The same configuration works if I run them as a single combined context. E.g. :</p> <pre><code>AnnotationConfigApplicationContext parentContext = new AnnotationConfigApplicationContext(ParentConfiguration.class, ChildContext.class); </code></pre> <p>However I want the isolation that using parent contexts provide.</p> <p>Cheers, Peter</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload