This example shows how Spring implicitly convert text to other types for the fields annotated with @Value annotations
theCurrency=PLN thePrice=12,323.7654 theTradeDate=2016-9-14
package com.logicbig.example; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import java.util.Currency; public class ValueAnnotationExample { public static void main (String[] args) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Config.class); ClientBean bean = context.getBean(ClientBean.class); bean.doSomething(); } @Configuration @PropertySource("classpath:app.properties") public static class Config { @Bean public ClientBean clientBean () { return new ClientBean(); } } public static class ClientBean { @Value("${theCurrency}") private Currency currency; public void doSomething () { System.out.printf("The currency from prop file is %s%n", currency); System.out.printf("The currency name is %s%n", currency.getDisplayName()); } } }
The currency from prop file is PLNThe currency name is Polish Zloty
package com.logicbig.example; import org.springframework.beans.PropertyEditorRegistrar; import org.springframework.beans.PropertyEditorRegistry; import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.config.CustomEditorConfigurer; import org.springframework.beans.propertyeditors.CustomDateEditor; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.PropertySource; import java.text.SimpleDateFormat; import java.util.Date; public class ValueAnnotationExample2 { public static void main (String[] args) { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Config.class); ClientBean bean = context.getBean(ClientBean.class); bean.doSomething(); } @Configuration @PropertySource("classpath:app.properties") public static class Config { @Bean public ClientBean clientBean () { return new ClientBean(); } @Bean public static CustomEditorConfigurer customEditorConfigurer () { CustomEditorConfigurer cec = new CustomEditorConfigurer(); cec.setPropertyEditorRegistrars( new PropertyEditorRegistrar[]{ new MyCustomBeanRegistrar()}); return cec; } } public static class ClientBean { @Value("${theTradeDate}") private Date tradeDate; public void doSomething () { System.out.printf("The trade date from prop file is %s%n", tradeDate); } } public static class MyCustomBeanRegistrar implements PropertyEditorRegistrar { @Override public void registerCustomEditors (PropertyEditorRegistry registry) { SimpleDateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd"); dateFormatter.setLenient(false); registry.registerCustomEditor(Date.class, new CustomDateEditor(dateFormatter, true)); } } }
The trade date from prop file is Wed Sep 14 00:00:00 CDT 2016
Dependencies and Technologies Used:
Version compatibilities of spring-context with this example:
Versions in green have been tested.