您的位置:首页 > 编程语言 > Java开发

如何让springmvc在启动的时候执行特定的业务处理

2017-10-29 15:44 495 查看

如何让springmvc在启动的时候执行特定的业务处理

 

        java 的 web服务器启动时,经常会做一些特定的业务逻辑处理,比如数据库初始化,

    初始化系统参数,读取配置文库等。

 

       很多web服务的中间件,可以 通过这样的思路去实现。比如消息分发服务。

 

 

实现方法:

 

一、Web项目,非Spring

解决方法:实现【 ServletContextListener】 接口

       (1)、把实现了ServletContextListener 的类配置到【 web.xml】 文件中

      

[html] view plain copy print?
  1. <?xml version="1.0" encoding="UTF-8"?>    
  2. <web-app version="2.5"     
  3.     xmlns="http://java.sun.com/xml/ns/javaee"     
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     
  5.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee     
  6.     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">    
  7.   <display-name></display-name>     
  8.   <welcome-file-list>    
  9.     <welcome-file>index.jsp</welcome-file>    
  10.   </welcome-file-list>    
  11.   <listener>    
  12.     <listener-class>com.chinaso.init.StartInit</listener-class>    
  13.   </listener>    
  14.   <filter>    
  15.     <filter-name>struts2</filter-name>    
  16.     <filter-class>    
  17.         org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter    
  18.     </filter-class>    
  19.   </filter>    
  20.   <filter-mapping>    
  21.     <filter-name>struts2</filter-name>    
  22.     <url-pattern>/*</url-pattern>    
  23.   </filter-mapping>    
  24.   </web-app>    
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<listener>
<listener-class>com.chinaso.init.StartInit</listener-class>
</listener>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>

 

 

   (2)、加入自己的实现逻辑

 

[java] view plain copy print?
  1. public class StartInit implements ServletContextListener {    
  2.     static final Logger logger = LoggerFactory.getLogger(StartInit.class);    
  3.     // 系统初始化执行方法    
  4.     public void contextDestroyed(ServletContextEvent e) {    
  5.         logger.info("系统停止...");    
  6.     }    
  7.     
  8.     public void contextInitialized(ServletContextEvent e) {    
  9.         logger.info("系统初始化开始...");    
  10.             
  11.         // 获取项目根目录    
  12.         String root_path  = e.getServletContext().getRealPath("/");    
  13.         logger.info("application path : {}",root_path);    
  14.             
  15.         // 初始化 ConfigFactorty    
  16.         ConfigFactory.init(root_path);    
  17.         // 初始化数据链接信息    
  18.         DBManager.init();    
  19.         // 初始化定时统计任务    
  20.         TaskManager.init();    
  21.         // 初始化用户信息查询位置    
  22.         UserInfo.init();    
  23.             
  24.         logger.info("系统初始化结束...");    
  25.     }    
  26.         
  27. }    
public class StartInit implements ServletContextListener {
static final Logger logger = LoggerFactory.getLogger(StartInit.class);
// 系统初始化执行方法
public void contextDestroyed(ServletContextEvent e) {
logger.info("系统停止...");
}

public void contextInitialized(ServletContextEvent e) {
logger.info("系统初始化开始...");

// 获取项目根目录
String root_path  = e.getServletContext().getRealPath("/");
logger.info("application path : {}",root_path);

// 初始化 ConfigFactorty
ConfigFactory.init(root_path);
// 初始化数据链接信息
DBManager.init();
// 初始化定时统计任务
TaskManager.init();
// 初始化用户信息查询位置
UserInfo.init();

logger.info("系统初始化结束...");
}

}



 

二、Spring项目

Spring-MVC的应用中,要实现类似的功能,主要是通过实现下面这些接口(任选一,至少一个即可)

 

1、ApplicationContextAware接口

 

[java] view plain copy print?
  1. package org.springframework.context;  
  2.    
  3. import org.springframework.beans.BeansException;  
  4. import org.springframework.beans.factory.Aware;  
  5. import org.springframework.context.ApplicationContext;  
  6.    
  7. public interface ApplicationContextAware extends Aware {  
  8.     void setApplicationContext(ApplicationContext var1) throws BeansException;  
  9. }  
package org.springframework.context;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.Aware;
import org.springframework.context.ApplicationContext;

public interface ApplicationContextAware extends Aware {
void setApplicationContext(ApplicationContext var1) throws BeansException;
}

 


2、ServletContextAware 接口

 

 

[java] view plain copy print?
  1. package org.springframework.web.context;  
  2.    
  3. import javax.servlet.ServletContext;  
  4. import org.springframework.beans.factory.Aware;  
  5.    
  6. public interface ServletContextAware extends Aware {  
  7.     void setServletContext(ServletContext var1);  
  8. }  
package org.springframework.web.context;

import javax.servlet.ServletContext;
import org.springframework.beans.factory.Aware;

public interface ServletContextAware extends Aware {
void setServletContext(ServletContext var1);
}



 

3、InitializingBean 接口

 

   

[java] view plain copy print?
  1. package org.springframework.beans.factory;  
  2.    
  3. public interface InitializingBean {  
  4.     void afterPropertiesSet() throws Exception;  
  5. }  
package org.springframework.beans.factory;

public interface InitializingBean {
void afterPropertiesSet() throws Exception;
}

 

 

4、ApplicationListener<ApplicationEvent> 接口

 

[java] view plain copy print?
  1. package org.springframework.context;  
  2.    
  3. import java.util.EventListener;  
  4. import org.springframework.context.ApplicationEvent;  
  5.    
  6. public interface ApplicationListener<E extends ApplicationEvent> extends EventListener {  
  7.     void onApplicationEvent(E var1);  
  8. }  
package org.springframework.context;

import java.util.EventListener;
import org.springframework.context.ApplicationEvent;

public interface ApplicationListener<E extends ApplicationEvent> extends EventListener {
void onApplicationEvent(E var1);
}


java代码:

 

 

[java] view plain copy print?
  1. package test.web.listener;  
  2.    
  3. import org.apache.logging.log4j.*;  
  4. import org.springframework.beans.*;  
  5. import org.springframework.beans.factory.InitializingBean;  
  6. import org.springframework.context.*;  
  7. import org.springframework.context.event.ContextRefreshedEvent;  
  8. import org.springframework.stereotype.Component;  
  9. import org.springframework.web.context.ServletContextAware;  
  10. import javax.servlet.ServletContext;  
  11.    
  12. @Component  
  13. public class StartupListener implements ApplicationContextAware, ServletContextAware,  
  14.         InitializingBean, ApplicationListener<ContextRefreshedEvent> {  
  15.    
  16.     protected Logger logger = LogManager.getLogger();  
  17.    
  18.     @Override  
  19.     public void setApplicationContext(ApplicationContext ctx) throws BeansException {  
  20.         logger.info("1 => StartupListener.setApplicationContext");  
  21.     }  
  22.    
  23.     @Override  
  24.     public void setServletContext(ServletContext context) {  
  25.         logger.info("2 => StartupListener.setServletContext");  
  26.     }  
  27.    
  28.     @Override  
  29.     public void afterPropertiesSet() throws Exception {  
  30.         logger.info("3 => StartupListener.afterPropertiesSet");  
  31.     }  
  32.    
  33.     @Override  
  34.     public void onApplicationEvent(ContextRefreshedEvent evt) {  
  35.         logger.info("4.1 => MyApplicationListener.onApplicationEvent");  
  36.         if (evt.getApplicationContext().getParent() == null) {  
  37.             logger.info("4.2 => MyApplicationListener.onApplicationEvent");  
  38.         }  
  39.     }  
  40.    
  41. }  
package test.web.listener;

import org.apache.logging.log4j.*;
import org.springframework.beans.*;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.*;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;
import org.springframework.web.context.ServletContextAware;
import javax.servlet.ServletContext;

@Component
public class StartupListener implements ApplicationContextAware, ServletContextAware,
InitializingBean, ApplicationListener<ContextRefreshedEvent> {

protected Logger logger = LogManager.getLogger();

@Override
public void setApplicationContext(ApplicationContext ctx) throws BeansException {
logger.info("1 => StartupListener.setApplicationContext");
}

@Override
public void setServletContext(ServletContext context) {
logger.info("2 => StartupListener.setServletContext");
}

@Override
public void afterPropertiesSet() throws Exception {
logger.info("3 => StartupListener.afterPropertiesSet");
}

@Override
public void onApplicationEvent(ContextRefreshedEvent evt) {
logger.info("4.1 => MyApplicationListener.onApplicationEvent");
if (evt.getApplicationContext().getParent() == null) {
logger.info("4.2 => MyApplicationListener.onApplicationEvent");
}
}

}



 

 

运行时,输出的顺序如下:

1 => StartupListener.setApplicationContext
2 => StartupListener.setServletContext
3 => StartupListener.afterPropertiesSet
4.1 => MyApplicationListener.onApplicationEvent
4.2 => MyApplicationListener.onApplicationEvent
4.1 => MyApplicationListener.onApplicationEvent

注意:onApplicationEvent方法会触发多次,初始化这种事情,越早越好,建议在setApplicationContext方法中处理。

 

 

 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: