SpringBoot启动后实现自动执行其它业务方法功能

一、业务应用场景

1:需要在容器启动的时候执行一些内容。比如读取配置文件,数据库连接之类的。SpringBoot给我们提供了两个接口来帮助我们实现这种需求。

2:应用服务启动时,加载一些数据和执行一些应用的初始化动作。如:删除临时文件,清除缓存信息,读取配置文件信息,数据库连接等。
对于小型项目进行定时任务的启动。

二、解决方案

方案1:ApplicationRunner

方案2:CommandLineRunner

SpringBoot给我们提供了两个接口来帮助我们实现这种需求。他们的执行时机为容器启动完成的时候。

ApplicationRunner中run方法的参数为ApplicationArguments,

而CommandLineRunner接口中run方法的参数为String数组。

当接口有多个实现类时,提供了@order注解实现自定义执行顺序,也可以实现Ordered接口来自定义顺序。

方案3:利用@Scheduled注解解决

注意:数字越小,优先级越高,也就是@Order(1)注解的类会在@Order(2)注解的类之前执行。

方案4:实现InitializingBean接口,重写afterPropertiesSet方法。

  • 被spring管理
  • 实现InitializingBean接口 
  • 重写afterPropertiesSet方法

InitializingBean接口为bean提供了初始化方法的方式,它只包括afterPropertiesSet方法,凡是继承该接口的类,在初始化bean的时候都会执行该方法。 测试每次调用这个方法都会重新初始化,但是只初始化一次。

三、CommandLineRunner和ApplicationRunner接口代码验证

方案1:ApplicationRunner接口

package org.spring.springboot.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Component  //被 spring 容器管理
@Order(1)   //如果多个自定义的 ApplicationRunner  ,用来标明执行的顺序
public class ApplicationRunnerStartService implements ApplicationRunner {
	
	private static Logger logger =  LoggerFactory.getLogger(ApplicationRunnerStartService.class);
	
	@Override
	public void run(ApplicationArguments args) throws Exception {
		logger.info("===SpringBoot项目启动后,执行ApplicationRunnerStartService方法,进行初始化操作 =============");
	}

}

方案2:CommandLineRunnerStartService

package org.spring.springboot.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

@Component  //被 spring 容器管理
@Order(10)  //如果多个自定义的 CommandLineRunner,用来标明执行的顺序,数字越小,顺序越靠前
public class CommandLineRunnerStartService  implements CommandLineRunner{

	private static Logger logger =  LoggerFactory.getLogger(CommandLineRunnerStartService.class);
	
	@Autowired
	private UserService userService;
	
	@Override
	public void run(String... args) throws Exception {
		logger.info("===SpringBoot项目启动后,执行CommandLineRunnerStartService方法,进行初始化操作 =============");
		//执行自己的业务逻辑
		System.out.println(userService.Sel(1).toString());
	}

}
package org.spring.springboot;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * Spring Boot 应用启动类
*/
@SpringBootApplication
public class ApplicationApp {

    public static void main(String[] args) {
        // 程序启动入口
        // 启动嵌入式的 Tomcat 并初始化 Spring 环境及其各 Spring 组件
        SpringApplication.run(ApplicationApp.class,args);
    }
}

ApplicationApp主启动类启动之后,控制台输出如下信息

四、利用@Scheduled注解解决

方案3:@Scheduled注解

@Component
public class OACheckScheduleTask {

    private static final Logger LOG = LoggerFactory.getLogger(OACheckScheduleTask.class);

    @Autowired
    private OARoleConfigService oaRoleConfigService;

    //@Scheduled(cron = "0 0 2 1/1 * ? ")//每天02:00执行
    @Scheduled(fixedDelay = 1 * 60 * 1000)//每1分钟一次
    public void configureTasks() {

        LOG.info("**********************************************************************************************");
        LOG.info("***************************检查人员数据异常信息开始************************************************");
        LOG.info("**********************************************************************************************");

        /**
         * 获取角色配置离职人员配置信息
         */
        oaRoleConfigService.getRoleConfig();

        LOG.info("**********************************************************************************************");
        LOG.info("***************************检查人员数据异常信息结束************************************************");
        LOG.info("**********************************************************************************************");

    }
}

 注意:主启动类要加上@EnableScheduling

@SpringBootApplication
@EnableScheduling
@MapperScan("com.aac.oacheck.dao")
public class OACheckApplication {
    public static void main(String[] args) {
        SpringApplication.run(OACheckApplication.class, args);
    }
}

五、实现InitializingBean接口,重写afterPropertiesSet方法。

@Service
public class UserService implements InitializingBean {
    @Resource
    private UserMapper userMapper;

    public List<User> getByName() {
        List<User> userList = userMapper.selectList(null);
        return userList;
    }

    @Override
    public void afterPropertiesSet() {
        System.out.println("执行了UserService的初始化InitializingBean.afterPropertiesSet方法");
    }
}

测试代码,项目启动时候就会执行此方法。

1、Spring为bean提供了两种初始化bean的方式,实现InitializingBean接口,实现afterPropertiesSet方法,或者在配置文件中通过init-method指定,两种方式可以同时使用。

2、实现InitializingBean接口是直接调用afterPropertiesSet方法,比通过反射调用init-method指定的方法效率要高一点,但是init-method方式消除了对spring的依赖。

3、如果调用afterPropertiesSet方法时出错,则不调用init-method指定的方法。

参考文章

springboot启动后自动执行方法的两种方法_happytaohaha的博客-CSDN博客_springboot项目启动后执行方法

springboot 启动后执行特定的方法_倪默遥的博客-CSDN博客

Springboot bean初始化方法InitializingBean

  • 4
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Spring Boot中,通过`@RestController`注解执行定时任务管理的业务流程可以按照以下步骤进行: 1. 首先,确保在Spring Boot项目的pom.xml文件中添加了以下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> ``` 2. 创建一个任务管理类,该类负责定义定时任务的具体执行逻辑。在该类上添加`@Component`注解,将其作为一个组件进行注册。 ```java import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; @Component public class TaskManager { // 每隔5秒执行一次任务 @Scheduled(fixedRate = 5000) public void task() { // 任务逻辑代码 System.out.println("执行定时任务"); } } ``` 在上述代码中,我们定义了一个名为`task`的方法,并使用`@Scheduled`注解指定了任务的执行频率为每隔5秒执行一次。 3. 创建一个控制器类,使用`@RestController`注解声明为一个RESTful接口,并注入任务管理类的实例。 ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class TaskController { @Autowired private TaskManager taskManager; @GetMapping("/startTask") public String startTask() { // 启动定时任务 return "定时任务已启动"; } @GetMapping("/stopTask") public String stopTask() { // 停止定时任务 return "定时任务已停止"; } } ``` 在上述代码中,我们使用`@RestController`注解将类声明为一个RESTful接口。在`startTask`和`stopTask`方法中,可以调用任务管理类的相关方法启动和停止定时任务。 4. 在Spring Boot应用程序的启动类上添加`@EnableScheduling`注解,以启用定时任务的支持,并运行你的应用程序。 ```java import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.scheduling.annotation.EnableScheduling; @SpringBootApplication @EnableScheduling public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 通过以上步骤,你可以通过访问`/startTask`和`/stopTask`接口来触发定时任务的启动和停止。当调用`/startTask`接口时,定时任务将开始执行;当调用`/stopTask`接口时,定时任务将停止执行。 注意:为了能够使用定时任务功能,需要在Spring Boot应用程序的配置类或启动类上添加`@EnableScheduling`注解。这样,Spring Boot自动扫描并执行被`@Scheduled`注解标记的方法

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值