SpringBoot启动自动执行代码

springboot项目启动自动执行代码几种方案_springboot项目启动自动调用方法_luffy5459的博客-CSDN博客

(2条消息) springboot项目启动,自动执行某个方法中的某个类_YoungLee16的博客-CSDN博客_springboot运行某个类

前言

        SpringBoot项目中,通过启动类(@SpringBootApplication注解声明)启动项目,启动项目后可以在前端页面进行测试,但前后端分离项目,每次调试启动前端比较麻烦,而且部分服务启动条件受限(如定时任务等),因此希望在项目启动后自动执行某段代码,做初始化操作,执行完毕不重复执行。

1.ServletContextListener接口

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

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

@Component
public class AutoStartByServletContextListener implements ServletContextListener {

    @Resource
    private CommonService commonService;

    @Value("1-开始:实现了ServletContextListener")
    private String s1;

    @Value("1-销毁:实现了ServletContextListener")
    private String s2;

    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent){
        commonService.sys(s1);
    }

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent){
        commonService.sys(s2);
    }

}

2.InitializingBean接口

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class AutoStartByInitializingBean implements InitializingBean {

    /**
     * 不建议使用@Autowired注解
     */
    @Autowired
    private CommonService commonService;

    /**
     * 在此不列举@Value注解别的使用方式
     */
    @Value("22:实现了InitializingBean")
    private String s;

    @Override
    public void afterPropertiesSet() throws Exception {
        commonService.sys(s);
    }

}

3.PostConstruct注解

import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

@Component
public class AutoStartByPostConstruct {

    @PostConstruct
    public void run(){
        System.out.println("333:使用了PostConstruct");
    }

}

4.ServletContextAware接口

import org.springframework.stereotype.Component;
import org.springframework.web.context.ServletContextAware;

import javax.servlet.ServletContext;

@Component
public class AutoStartByServletContextAware implements ServletContextAware {

    @Override
    public void setServletContext(ServletContext servletContext){
        System.out.println("4444:实现了ServletContextAware");
    }

}

5.ApplicationRunner接口

import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

@Component
public class AutoStartByApplicationRunner implements ApplicationRunner {

    @Override
    public void run(ApplicationArguments args) throws Exception {
        System.out.println("55555:实现了ApplicationRunner");
    }

}

6.CommandLineRunner接口

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

@Component
public class AutoStartByCommandLineRunner implements CommandLineRunner {

    @Override
    public void run(String... strings) throws Exception {
        System.out.println("666666:实现了CommandLineRunner");
    }

}

结果

 参考

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值