Tuesday, April 14, 2015

Spring Property Example

This is a simple example showing how to set properties on Spring @Component and @Bean using @Value and @PropertySource, respectively. The source code for this example is located on github https://github.com/jamie3/spring-property-config.

First create the properties file.

Next create a Java bean.


package spring.property.example;

public class MyBean {

 String beanProperty;
}


Next create a Java component.


package spring.property.example;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class MyComponent {

 @Value("${componentProperty}")
 String componentProperty;
 
 
}


Next create a Java configuration. As you can see the @PropertySource loads the properties into the Environment class which is then used by the myBean() method to set properties on the bean. This is useful when you have objects from 3rd party jars and want to set properties on them.

The propertyPlaceHolderConfigurer() method is the equivalent of from Spring's XML. Since we are using annotation-based @Configuration we must create a static instance of the PropertyPlaceHolderConfigurer class such that it will perform the automatic injection of the properties into our @Component that contains @Value.
package spring.property.example;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.env.Environment;

@Configuration
@ComponentScan("spring.property.example")
@PropertySource("example.properties")
public class AppConfig {

 /**
  * Uses the environment to inject the property into the beans that do not have the @Value attribute
  * 
  * @param environment
  * @return
  */
 @Bean
 public MyBean myBean(Environment environment) {
  MyBean bean = new MyBean();
  bean.beanProperty = environment.getProperty("beanProperty");
  return bean;
 }
 
 /**
  * Loads the properties into the beans using the @Value attribute
  * 
  * @return
  */
 @Bean
 public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer(Environment environment) {
  PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
  propertySourcesPlaceholderConfigurer.setEnvironment(environment);
  return propertySourcesPlaceholderConfigurer;
 }
}


Finally the main class which creates the Spring Context.


package spring.property.example;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class Main {

 private Main() {
  
 }
 
 public static void main(String[] args) {
  
  Object obj = new Object();
  AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
  try {
   
   MyBean myBean = context.getBean(MyBean.class);
   System.out.println("MyBean.beanProperty = " + myBean.beanProperty);
   
   MyComponent myComponent = context.getBean(MyComponent.class);
   System.out.println("MyComponent.componentProperty = " + myComponent.componentProperty);
   
   synchronized (obj) {
    obj.wait();
   }
  } catch (InterruptedException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } finally {
   context.close();
  }
 }
}

No comments: