博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
spring启动时只执行一次的方法实现
阅读量:4046 次
发布时间:2019-05-24

本文共 3488 字,大约阅读时间需要 11 分钟。

spring项目如何在启动项目是执行一些操作,在spring中能通过那些操作实现这个功能呢。1.方法一我在spring的配置文件中添加上这条,这个配置只能在启动项目是执行一遍。 还有一点 要注意 这个方法不能是controller层的方法<-- class是类的全名加包名 这是指定运行的方法在那个类里面   --><-- scope 值得范围 这里给的参数是 singleton   --><-- inti-method 是指要执行的方法  -->
package com.shr.jobs;public class StartRun { public void test(){ System.out.println("开始执行 startRun 方法!!!"); }}log4j:WARN No appenders could be found for logger (org.springframework.web.context.ContextLoader).log4j:WARN Please initialize the log4j system properly.log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.开始执行 startRun 方法!!!SLF4J: Class path contains multiple SLF4J bindings.SLF4J: Found binding in [jar:file:/D:/tomcat7.manage/webapps/newBLManager/WEB-INF/lib/slf4j-log4j12-1.5.11.jar!/org/slf4j/impl/StaticLoggerBinder.class]SLF4J: Found binding in [jar:file:/D:/tomcat7.manage/webapps/newBLManager/WEB-INF/lib/slf4j-nop-1.5.2.jar!/org/slf4j/impl/StaticLoggerBinder.class]SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.2.方法二 使用@PostContruct ,在方法上添加@PostConstruct注解 注意 一定要放在能被扫面到的地方,如果你写在一个无法被扫描到的位置是不能执行的。(service层肯定能被扫描到)@Servicepublic class TestRun{ @PostConstruct public void text(){ System.out.println("项目开始运行我也就执行!!"); }}SLF4J: Class path contains multiple SLF4J bindings.SLF4J: Found binding in [jar:file:/D:/tomcat7.manage/webapps/newBLManager/WEB-INF/lib/slf4j-log4j12-1.5.11.jar!/org/slf4j/impl/StaticLoggerBinder.class]SLF4J: Found binding in [jar:file:/D:/tomcat7.manage/webapps/newBLManager/WEB-INF/lib/slf4j-nop-1.5.2.jar!/org/slf4j/impl/StaticLoggerBinder.class]SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.Logging initialized using 'class org.apache.ibatis.logging.stdout.StdOutImpl' adapter.项目开始运行我也就执行!!五月 19, 2018 12:21:14 上午 org.apache.catalina.core.ApplicationContext log信息: Initializing Spring FrameworkServlet 'springMVC'3.方法三 实现initiailzingBean接口。afterPropertiesSet这个方法就会在项目启动时执行。@Servicepublic class ArraignedLogService implements InitializingBean { @Override public void afterPropertiesSet() throws Exception { System.out.println("我是通过实现接口 initializingBean来执行的!!!"); }}SLF4J: Class path contains multiple SLF4J bindings.SLF4J: Found binding in [jar:file:/D:/tomcat7.manage/webapps/newBLManager/WEB-INF/lib/slf4j-log4j12-1.5.11.jar!/org/slf4j/impl/StaticLoggerBinder.class]SLF4J: Found binding in [jar:file:/D:/tomcat7.manage/webapps/newBLManager/WEB-INF/lib/slf4j-nop-1.5.2.jar!/org/slf4j/impl/StaticLoggerBinder.class] Logging initialized using 'class org.apache.ibatis.logging.stdout.StdOutImpl' adapter.我是通过实现接口 initializingBean来执行的!!!五月 19, 2018 12:38:33 上午 org.apache.catalina.core.ApplicationContext log信息: Initializing Spring FrameworkServlet 'springMVC'4.因为需要保证所有调度相关的依赖注入spring容器才创建所以定时调度任务,所以需要实现在Spring容器将所有的Bean都初始化完成之后才自动执行一次执行方法(创建一个调度任务)实现实现ApplicationListener接口,并实现 onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent)方法@Servicepublic class SearchReceive implements ApplicationListener
{ @Override public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) { if (contextRefreshedEvent.getApplicationContext().getParent() == null) {//保证只执行一次 //需要执行的方法 } }}至于为什么先做判断,因为Spring存在两个容器,一个是root application context ,另一个就是我们自己的 projectName-servlet context(作为root application context的子容器)。这种情况下,就会造成onApplicationEvent方法被执行两次。为了避免上面提到的问题,我们可以只在root application context初始化完成后调用逻辑代码,其他的容器的初始化完成,则不做任何处理!

 

转载地址:http://fxwci.baihongyu.com/

你可能感兴趣的文章
JAVA实现AES加密
查看>>
JAVA实现DES加密
查看>>
关于AES256算法java端加密,ios端解密出现无法解密问题的解决方案
查看>>
node.js AES/ECB/PKCS5Padding 与其他语言的加密解密通用
查看>>
Java and Nodejs on AES
查看>>
AES加密CBC模式兼容互通四种编程语言平台【PHP、Javascript、Java、C#】
查看>>
js老生常谈之this,constructor ,prototype
查看>>
nodejs-post文件上传原理详解
查看>>
node-formidable详解
查看>>
CenOS Linux安装nginx
查看>>
Nginx 关于 Rewrite 执行顺序详解
查看>>
Linux网络编程socket错误分析
查看>>
CAS实现SSO单点登录原理
查看>>
MongoDB中的_id和ObjectId
查看>>
美团酒店Node全栈开发实践
查看>>
MongoDB分片实战(一):集群搭建
查看>>
MongoDB分片实战(二):Sharding
查看>>
TLS/HTTPS 证书生成与验证
查看>>
用Node.js创建自签名的HTTPS服务器
查看>>
单点登录的三种实现方式
查看>>