Monday, October 20, 2014

Reading a custom properties or default message properties in spring service/controller..etc


Custom properties file
Need to configure the property files in this format.

Make sure you keep the properties in your class path.

<?xml version="1.0" encoding="UTF-8" ?>

 <beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:util="http://www.springframework.org/schema/util"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">
  <util:properties id="npAdminProperties" location="classpath:npadmin.properties" />
 </beans>

Example:

npadmin.properties file:

environment.code=DEV


To read the values from the properties files in service/controller you can inject like

private @Value("#{npAdminProperties['environment.code']}") String environment;


To read a Message properties in controller or service :

One way of doing:

@Autowired
private MessageSource messageSource;

messageSource.getMessage("domaindetector.builder.email.subject",null, Locale.ENGLISH)

Other way:
@Value("${domaindetector.builder.email.subject}")

private String domainDetecorEmailSubject;

No comments: