利用Spring的ServletContextAware给ServletContext初始化数据

来源:互联网 发布:网络分布式继电器接线 编辑:程序博客网 时间:2024/06/06 14:18
package com.xxximport org.springframework.beans.factory.annotation.Value;import org.springframework.context.annotation.Lazy;import org.springframework.stereotype.Component;import org.springframework.web.context.ServletContextAware;@Component@Lazy(false)public class PropertyHolder implements ServletContextAware{    private static ServletContext servletContext;    private static String uploadDir;    // 生成的js文件存放目录    private static String jsGenerateDir;    /**    * 给上下文设置值    */    @Override    public void setServletContext(ServletContext servletContext) {        PropertyHolder.servletContext = servletContext;        servletContext.setAttribute("ctx", servletContext.getContextPath());        servletContext.setAttribute("uploadDir", uploadDir);        //TODO 初始化任何值    }    //从配置文件(/application.properties)给属性赋值,其中upload.dir 配置文件在项目根路径,里面定义了upload.dir才能赋值成功,否则初始化事值为null    @Value("${upload.dir}")    public void setUploadDir(String uploadDir) {        PropertyHolder.uploadDir = uploadDir;    }    //TODO 从配置文件初始化任何属性    //It's so easy! let's get it;}
阅读全文
0 0