Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If you're going to use <code>@Bean</code>, you'll need <code>@Configuration</code>. You shouldn't declare a xml context to include an annotation context. You also shouldn't use your <code>@Configuration</code> class instance as a bean. <code>ClassPathXmlApplicationContext</code> is no good for processing annotation based configurations.</p> <p>Use something like the following</p> <pre><code>@Configuration @ComponentScan(basePackageClasses = LoadProperties.class) public static class Config { @Bean public static PropertyPlaceholderConfigurer props() { PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer(); Resource[] resource = new ClassPathResource[] { new ClassPathResource( "/EclipseProjectName/src/cluster.properties") }; ppc.setLocations(resource); ppc.setIgnoreUnresolvablePlaceholders(true); return ppc; } @Bean public LoadProperties loadProperties() { return new LoadProperties(); } } public static class LoadProperties { private @Value("${cluster.zooServers}") String zooServers; ... // getters and setters } public static void main(String[] args) throws Exception { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Config.class); LoadProperties load = (LoadProperties) context.getBean(LoadProperties.class); System.out.println(load.getZooServers()); } </code></pre> <p>A few things to note: </p> <ol> <li>In your <code>ClassPathResource</code> you need to specify a classpath resource. Do you really have a resource <code>/EclipseProjectName/src/cluster.properties</code> at the root of your classpath? I very much doubt it.</li> <li>In this case you won't need a <code>@ComponentScan</code>, but familiarize yourself with it.</li> <li>A <code>PropertyPlaceholderConfigurer</code> needs to be declared static so it can be initialized before the other <code>@Bean</code> declarations. You should use <code>PropertySourcesPlaceholderConfigurer</code> as <a href="http://docs.spring.io/spring/docs/3.2.x/javadoc-api/org/springframework/beans/factory/config/PropertyPlaceholderConfigurer.html" rel="nofollow">explained in the javadoc</a>.</li> </ol>
 

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