首页 技术 正文
技术 2022年11月16日
0 收藏 862 点赞 2,742 浏览 1569 个字

之前从Java web一路学习过来,一直没有学习过Servlet容器类的一些高级用法,因为学完简单的JSP以及Servlet编写之后就开始了Spring的学习

对web应用的一些常用变量进行 application 也就是应用开启的生存周期做静态化处理

<listener>
<listener-class>xyz.springabc.web.listener.StartUpListener</listener-class>
</listener>

首先添加Listen,一个观察者

public class StartUpListener implements ServletContextListener {//这个观察者实现了 ServletContextListener    /**
* Default constructor.
*/
public StartUpListener() {
// TODO Auto-generated constructor stub
} /**
* @see ServletContextListener#contextDestroyed(ServletContextEvent)
*/
public void contextDestroyed(ServletContextEvent event) {
// TODO Auto-generated method stub
} /**
* @see ServletContextListener#contextInitialized(ServletContextEvent)
*/
public void contextInitialized(ServletContextEvent event) {//Servlet上下文初始化的时候调用这个函数
ServletContext application = event.getServletContext();//得到上下文
ApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(application);//利用spring的工具类得到Spring上下文
PropertyServ propertyServ = applicationContext.getBean(PropertyServ.class);//用spring上下文得到Service bean
propertyServ.setup(application);//把设置信息都扔到里面,页面可以直接读取
}}

下面是Service的一些实现

@Service
public class PropertyServ { @Autowired
private PropertyRepo propertyRepo; @Autowired
private FieldRepo fieldRepo; private ServletContext application; /**
* 启动监听设置这个属性
* @param application
*/
public void setup(ServletContext application){//调用了setup方法
this.application=application;
for(Property property:propertyRepo.findAll()){
Field field = fieldRepo.findOneByProperty(property); application.setAttribute(property.getKeyword(), field);//这里从dao层读出 key value 全部加载到application对象里面去,这样在JSP页面可以直接取得这些常量值 而不用去调用Service每次从数据库获得这些网站静态变量
}
}}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,942
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,468
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,281
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,096
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,728
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,765