I develop Spring-Mvc project and this project's application properties count too much. Also I can't change this properties during program is running. So, I want to get this properties from database and assign to member of classes.
Now, I take this properties from database by using a service but I don't think, this code is reusable. I wonder, is there any methodology to get this properties to members via custom annotation?
ApplicationPropertiesDao.java
@Repository
public class ApplicationPropertiesDao implements IApplicationPropertiesDao{
@Value("#{appProperties.instanceName}")
private String instanceName;
@Override
@Cacheable("applicationProperties")
public HashMap<String, String> getApplicationPropertiesFromDB(){
Map<String, Object> inputMap = new HashMap<String, Object>();
inputMap.put("instanceName", instanceName);
String sql = getSQL("getApplicationProperties");
Map<String, String> list = new HashMap<String, String>();
getNamedParameterJdbcTemplate().query(sql, inputMap, new ResultSetExtractor<Map<String, String>>() {
@Override
public Map<String, String> extractData(ResultSet resultSet) throws SQLException, DataAccessException {
while (resultSet.next()) {
list.put( resultSet.getString("NAME"),resultSet.getString("VALUE"));
}
return list;
}
});
return (HashMap<String, String>) list;
}
}
ApplicationPropertiesService.java
@Service
public class ApplicationPropertiesService implements IApplicationPropertiesService{
@Autowired
private IApplicationPropertiesDao applicationPropertiesDao;
public HashMap<String, String> getApplicationPropertiesFromDB(){
return applicationPropertiesDao.getApplicationPropertiesFromDB();
}
public Object getApplicationPropertyByName(String name, ApplicationPropertyValueTypeEnum applicationPropertyValueTypeEnum){
String value = getApplicationPropertiesFromDB().get(name);
if (applicationPropertyValueTypeEnum.name().equals(ApplicationPropertyValueTypeEnum.INTEGER.toString())){
return Integer.parseInt(value);
}else if(applicationPropertyValueTypeEnum.name().equals(ApplicationPropertyValueTypeEnum.BOOLEAN.toString())){
String upperCaseValue = value.toUpperCase(Locale.ENGLISH);
return upperCaseValue.equals("TRUE") ? Boolean.TRUE : Boolean.FALSE;
}
return value;
}
}
Can I create a custom annotation as shown below for this situation? If there is , I will be happy. How can i make this?
@Service
public class Demo{
private String val;
@Autowired
public ApplicationPropertiesService applicationPropertiesService;
@GetPropertyFromDB(key = "val", type = "String")
public getVal(){
//set(applicationPropertiesService.getApplicationPropertyByName("val", "String"));
return this.val;
}
public void setVal(String val){
this.val = val;
}
}
Thank you for your help.