@Value not resolved when using @PropertySource annotation.

Spring’s gotcha-of-the-day is around using @Value to resolve property placeholders in combination with @PropertySource. I had the following Spring Java configuration: @Configuration @PropertySource("classpath:/test.properties") public class TestAppConfig { @Value("${queue.name}") private String queue; @Bean(name="queue") public String getQueue() { return queue; } } But the value in “queue” was not resolving - it returned “${queue.name}” as the value. It turns out that I needed the following magical incantation to get it to work. [Read More]

Scheduled tasks with Spring and Java configs

Spring 3.2 has some very nice features for scheduling tasks. The pure Java way of doing this looks something like private ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor(); class ScheduledTask implements Runnable { @Override public void run() { System.out.println("Running scheduled task"); } } // Schedule a task every 5 seconds executor.scheduleAtFixedRate(new ScheduledTask(), 1, 5, TimeUnit.SECONDS); // If you don't do this then the JVM won't exit cleanly executor.shutdown(); But now, with the snazzy new Spring scheduling annotations, it can be as simple as this [Read More]

Using Factory Beans in Spring Java Configs

I recently went through an exercise of converting Spring XML configuration files to Java-based configuration. The process went well for the most part, but I encountered an oddity around how to use factory beans when using Java configs. In XML, factory beans would be used like this <bean id="MyDataSource" class="org.springframework.jndi.JndiObjectFactoryBean" p:jndiName="jdbc/MyDataSource" p:resourceRef="false" /> In Java however, this has to be separated into two separate @Beans @Bean public JndiObjectFactoryBean getJndiObjectFactoryBean() { JndiObjectFactoryBean jndi = new JndiObjectFactoryBean(); jndi. [Read More]