Wednesday, April 8, 2009

Spring Custom Extensions

  • CustomPropertyPlaceHolderConfigurer
public class CustomPropertyPlaceHolderConfigurer extends PropertyPlaceholderConfigurer{

@Override
public void setLocations(Resource[] locations) {

Resource[] newLocations =new Resource[locations.length+1];
for(int i=0;i newLocations[i]=locations[i];
}
newLocations[locations.length]= new ClassPathResource("env/"+System.getProperty(PROP_MODE)+".properties");
super.setLocations(newLocations);
}
}


  • CustomContextLoaderListener
public class CustomContextLoaderListener extends ContextLoaderListener {

private Log log = LogFactory.getLog(getClass());

@Override
public void contextInitialized(ServletContextEvent event) {

String port=null;
Parameters para = (Parameters)event.getServletContext().getAttribute(PROP_RS_PARAMS);
for(String p:Arrays.asList(para.getPorts())){
if(p.contains("public")){
port=p.substring(0,p.indexOf("/"));
break;
}
}
log.info("Startup Params: \nMODE .... "+para.getMode()+ "\nHOST .... "+para.getHost()+"\nPORT ... "+port);
System.setProperty(PROP_MODE, para.getMode());
System.setProperty(PROP_HOST_IP, para.getHost());
System.setProperty(PROP_HOST_PORT, port);
super.contextInitialized(event);
}
}

No comments:

Post a Comment