Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring @Value property for custom class

Tags:

spring

Is it possible to use Spring's @Value annotation to read and write property values of a custom class type?

For example:

@Component
@PropertySource("classpath:/data.properties")
public class CustomerService {

    @Value("${data.isWaiting:#{false}}")
    private Boolean isWaiting;

    // is this possible for a custom class like Customer???
    // Something behind the scenes that converts Custom object to/from property file's string value via an ObjectFactory or something like that?
    @Value("${data.customer:#{null}}")
    private Customer customer;

    ...
}

EDITED SOLUTION

Here is how I did it using Spring 4.x APIs...

Created new PropertyEditorSupport class for Customer class:

public class CustomerPropertiesEditor extends PropertyEditorSupport {

    // simple mapping class to convert Customer to String and vice-versa.
    private CustomerMap map;

    @Override
    public String getAsText() 
    {
        Customer customer = (Customer) this.getValue();
        return map.transform(customer);
    }

    @Override
    public void setAsText(String text) throws IllegalArgumentException 
    {
        Customer customer = map.transform(text);
        super.setValue(customer);
    }
}

Then in application's ApplicationConfig class:

@Bean
public CustomEditorConfigurer customEditorConfigurer() {

    Map<Class<?>, Class<? extends PropertyEditor>> customEditors = 
            new HashMap<Class<?>, Class<? extends PropertyEditor>>(1);
    customEditors.put(Customer.class, CustomerPropertiesEditor.class);

    CustomEditorConfigurer configurer = new CustomEditorConfigurer();
    configurer.setCustomEditors(customEditors);

    return configurer;
}

Cheers, PM

like image 711
Going Bananas Avatar asked Sep 26 '14 15:09

Going Bananas


People also ask

What does @value do in spring?

Spring @Value annotation is used to assign default values to variables and method arguments. We can read spring environment variables as well as system variables using @Value annotation.

Can we use @value inside method?

no :) you can use annotations to annotate classes, fields, methods and their arguments. but not in methods, since there is no way, to get method- local variables using reflection in order to process these annotations. Use @Value in your field, and read the value from your method.

How do you inject property value into a class not managed by Spring?

Load Configuration With Class Loader Instead of using third party implementations that support automatic application configuration loading, e.g. that implemented in Spring, we can use Java ClassLoader to do the same. We're going to create a container object that will hold Properties defined in resourceFileName.


2 Answers

You have to create a class extending PropertyEditorSupport.

public class CustomerEditor extends PropertyEditorSupport {
  @Override
  public void setAsText(String text) {
    Customer c = new Customer();
    // Parse text and set customer fields...
    setValue(c);
  }
}
like image 103
Efe Kahraman Avatar answered Sep 17 '22 07:09

Efe Kahraman


It's possible but reading Spring documentation. You could see this example: Example usage

 @Configuration
 @PropertySource("classpath:/com/myco/app.properties")
 public class AppConfig {
     @Autowired
     Environment env;

     @Bean
     public TestBean testBean() {
         TestBean testBean = new TestBean();
         testBean.setName(env.getProperty("testbean.name"));
         return testBean;
     }
 }

See details here

like image 40
Pracede Avatar answered Sep 19 '22 07:09

Pracede