spring启动时只执行一次的方法实现

spring项目如何在启动项目是执行一些操作,在spring中能通过那些操作实现这个功能呢。

1.方法一

我在spring的配置文件中添加上这条,这个配置只能在启动项目是执行一遍。 
还有一点 要注意 这个方法不能是controller层的方法

<-- class是类的全名加包名 这是指定运行的方法在那个类里面   -->
<-- scope 值得范围 这里给的参数是 singleton   -->
<-- inti-method 是指要执行的方法  -->
<bean id="startRun" class="com.shr.bojs.StartRun" scope="singleton" init-method="test"></bean>

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层肯定能被扫描到)

@Service
public 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这个方法就会在项目启动时执行。


@Service
public 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)方法

@Service
public class SearchReceive implements  ApplicationListener<ContextRefreshedEvent> {
    @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初始化完成后调用逻辑代码,其他的容器的初始化完成,则不做任何处理

发布了136 篇原创文章 · 获赞 65 · 访问量 16万+

猜你喜欢

转载自blog.csdn.net/jakeswang/article/details/92805046