springboot项目静态资源CDN统一管理

代码如下

1.添加这个

@Component	
public class InitConfig implements ApplicationListener<ContextRefreshedEvent>{
	public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
		WebApplicationContext webApplicationContext = (WebApplicationContext) contextRefreshedEvent.getApplicationContext();
		ServletContext sc = webApplicationContext.getServletContext();
		ResourceBundle  resource = ResourceBundle.getBundle("cdn");//配置文件相对工程根目录的相对路径(不含扩展名)文件在resources目录下
		for(Object key:resource.keySet()) {
			sc.setAttribute((String)key,resource.getString((String)key));
		}
	}
}

2.CDN配置(CDN网站推荐BootCDN)

根目录添加文件cdn.properties

layuicss=https://www.layuicdn.com/layui/css/layui.css
layuijs=https://www.layuicdn.com/layui/layui.js
jquery=https://cdn.bootcss.com/jquery/3.4.1/jquery.js
vuejs=https://cdn.bootcss.com/vue/2.6.10/vue.js
......

jsp引用 如与其他域对象值无重复可省略application.



thymeleaf引用 application.不可省略



你可能感兴趣的:(笔记)