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

SpringBoot实践之---系统启动加载实现的几种方法init、servelt、CommandLineRunner

2018-04-24 09:40 1141 查看

实际应用中,我们会有在项目服务启动的时候就去加载一些数据或做一些事情这样的需求。

 方法一:CommandLineRunner 

为了解决这样的问题,Spring Boot 为我们提供了一个方法,通过实现接口 CommandLineRunner 来实现。

很简单,只需要一个类就可以,无需其他配置。 
创建实现接口 CommandLineRunner 的类

package org.springboot.sample.runner;

import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

/**
* 服务启动执行
*
* @author   单红宇(365384722)
* @myblog  http://blog.csdn.net/catoop/
* @create    2016年1月9日
*/
@Component
public class MyStartupRunner1 implements CommandLineRunner {

@Override
public void run(String... args) throws Exception {
System.out.println(">>>>>>>>>>>>>>>服务启动执行,执行加载数据等操作<<<<<<<<<<<<<");
}

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
Spring Boot应用程序在启动后,会遍历CommandLineRunner接口的实例并运行它们的run方法。也可以利用@Order注解(或者实现Order接口)来规定所有CommandLineRunner实例的运行顺序。如下我们使用@Order 注解来定义执行顺序。
package org.springboot.sample.runner;

import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
* 服务启动执行
*
* @author   单红宇(365384722)
* @myblog  http://blog.csdn.net/catoop/
* @create    2016年1月9日
*/
@Component
@Order(value=2)
public class MyStartupRunner1 implements CommandLineRunner {

@Override
public void run(String... args) throws Exception {
System.out.println(">>>>>>>>>>>>>>>服务启动执行,执行加载数据等操作 11111111 <<<<<<<<<<<<<");
}

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
package org.springboot.sample.runner;

import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
* 服务启动执行
*
* @author   单红宇(365384722)
* @myblog  http://blog.csdn.net/catoop/
* @create    2016年1月9日
*/
@Component
@Order(value=1)
public class MyStartupRunner2 implements CommandLineRunner {

@Override
public void run(String... args) throws Exception {
System.out.println(">>>>>>>>>>>>>>>服务启动执行,执行加载数据等操作 22222222 <<<<<<<<<<<<<");
}

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
启动程序后,控制台输出结果为:
>>>>>>>>>>>>>>>服务启动执行,执行加载数据等操作 22222222 <<<<<<<<<<<<<
>>>>>>>>>>>>>>>服务启动执行,执行加载数据等操作 11111111 <<<<<<<<<<<<<
  • 1
  • 2
根据控制台结果可判断,@Order 注解的执行优先级是按value值从小到大顺序。

方法二:在过滤器(Filter)和 监听器(Listener)中加载初始化

过滤器(Filter)文件

MyFilter.java
package org.springboot.sample.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;

/**
* 使用注解标注过滤器
* @WebFilter将一个实现了javax.servlet.Filter接口的类定义为过滤器
* 属性filterName声明过滤器的名称,可选
* 属性urlPatterns指定要过滤 的URL模式,也可使用属性value来声明.(指定要过滤的URL模式是必选属性)
*
* @author   单红宇(365384722)
* @myblog  http://blog.csdn.net/catoop/
* @create    2016年1月6日
*/
@WebFilter(filterName="myFilter",urlPatterns="/*")
public class MyFilter implements Filter {

@Override
public void destroy() {
System.out.println("过滤器销毁");
}

@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
System.out.println("执行过滤操作");
chain.doFilter(request, response);
}

@Override
public void init(FilterConfig config) throws ServletException {
System.out.println("过滤器初始化");
}

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43

ServletContext监听器(Listener)文件

MyServletContextListener.java
package org.springboot.sample.listener;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

/**
* 使用@WebListener注解,实现ServletContextListener接口
*
* @author   单红宇(365384722)
* @myblog  http://blog.csdn.net/catoop/
* @create    2016年1月6日
*/
@WebListener
public class MyServletContextListener implements ServletContextListener {

@Override
public void contextInitialized(ServletContextEvent sce) {
System.out.println("ServletContex初始化");
System.out.println(sce.getServletContext().getServerInfo());
}

@Override
public void contextDestroyed(ServletContextEvent sce) {
System.out.println("ServletContex销毁");
}

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
ServletContext监听器(Listener)文件MyHttpSessionListener.java
package org.springboot.sample.listener;

import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

/**
* 监听Session的创建与销毁
*
* @author   单红宇(365384722)
* @myblog  http://blog.csdn.net/catoop/
* @create    2016年1月6日
*/
@WebListener
public class MyHttpSessionListener implements HttpSessionListener {

@Override
public void sessionCreated(HttpSessionEvent se) {
System.out.println("Session 被创建");
}

@Override
public void sessionDestroyed(HttpSessionEvent se) {
System.out.println("ServletContex初始化");
}

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
注意不要忘记在 SpringBootSampleApplication.java 上添加 @ServletComponentScan 注解。在启动的过程中我们会看到输出:
ServletContex初始化
Apache Tomcat/8.0.30
过滤器初始化
  • 1
  • 2
  • 3

方法三:在SpringBoot主启动类中嵌套加载初始操作代码

  [html] view plain copy
  1. import org.springframework.boot.SpringApplication;  
  2. import org.springframework.boot.autoconfigure.SpringBootApplication;  
  3. import org.springframework.boot.context.properties.EnableConfigurationProperties;  
  4. import org.springframework.cache.annotation.EnableCaching;  
  5. import org.springframework.context.annotation.ImportResource;  
  6. import org.springframework.transaction.annotation.EnableTransactionManagement;  
  7.   
  8. @SpringBootApplication(scanBasePackages={"com.ceiec"})  
  9. @EnableTransactionManagement  
  10. @EnableCaching  
  11. @ImportResource("classpath:consumer.xml")  
  12. public class WebInsightApplication {  
  13.   
  14.     public static void main(String[] args) {  
  15.         SpringApplication.run(WebInsightApplication.class, args);  
  16.         SystemInit.init();  
  17.     }  
  18.   
  19. }  
 实际加载类  [html] view plain copy
  1. public class SystemInit {  
  2.   
  3.     /** 记录用户每次请求时间:key为userId,value为请求时间毫秒值 */  
  4.     private static Map<String, Object> requestTime = new HashMap<String, Object>();  
  5.   
  6.     public static Map<String, Object> getRequestTime() {  
  7.         return requestTime;  
  8.     }  
  9.   
  10.     /**  
  11.      * 系统初始化  
  12.      */  
  13.     public static void init() {  
  14.         //清空本地用户登陆token缓存  
  15.         RedisUtils.delete(ERedisDomain.TOKEN_LOGIN_USER);  
  16.   
  17.         //初始化ES连接池  
  18.         ESUtils.initClient();  
  19.   
  20.         //启动系统主线程  
  21.         SystemThread thread = new SystemThread();  
  22.         thread.setDaemon(true);  
  23.         thread.start();  
  24.     }  
  25. }  
阅读更多
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: