spring初始化参数

  首先要在初始化参数的类上写上注解@Compoent,并且这个类要实现InitializingBean,ServletContextAware两个接口,然后在通过类的加载器获得参数配置文件

在利用Properties类得到参数的值。

  

 1 @Component
 2 public class InitArgument implements InitializingBean,ServletContextAware{
 3 
 4     private ServletContext context;
 5     public void afterPropertiesSet() throws Exception {
 6         InputStream in = ReadFile.class.getClassLoader().getResourceAsStream("config.properties");
 7         Properties p = new Properties();
 8         p.load(in);
 9         String zipPath = p.getProperty("WEBRESOURCE_ZIP_PATH").trim();
10         if(zipPath != null && !zipPath.equals("")){
11             ConstantUtil.setWEBRESOURCE_ZIP_PATH(zipPath);
12         }
13 }
14 
15     public void setServletContext(ServletContext context) {
16         this.context = context;
17     }

 

posted @ 2014-03-18 09:27  哎呦喂,我的小祖宗╰つ  阅读(687)  评论(0编辑  收藏  举报