springboot启动后异步启动一个程序
- 作者: 五速梦信息网
- 时间: 2026年04月04日 13:27
@Async@EnableAsync
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync; @SpringBootApplication
@EnableAsync
public class MyApplication { public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
- 创建一个服务类: 创建一个服务类,包含你要异步执行的方法。
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service; @Service
public class MyAsyncService { @Async
public void asyncMethod() {
// 在这里编写你的异步方法逻辑
System.out.println("异步方法被执行了");
}
}
mainMyAsyncService
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext; @SpringBootApplication
@EnableAsync
public class MyApplication { public static void main(String[] args) {
ApplicationContext context = SpringApplication.run(MyApplication.class, args);
MyAsyncService myAsyncService = context.getBean(MyAsyncService.class);
myAsyncService.asyncMethod();
}
}
main
@Async
相关文章
-
springboot实现服务器端消息推送(websocket + sockjs + stomp)
springboot实现服务器端消息推送(websocket + sockjs + stomp)
- 互联网
- 2026年04月04日
-
SpringBoot整合aspectj实现面向切面编程(即AOP)
SpringBoot整合aspectj实现面向切面编程(即AOP)
- 互联网
- 2026年04月04日
-
springboot整合mybatis源码分析
springboot整合mybatis源码分析
- 互联网
- 2026年04月04日
-
Springboot接口字符串判空
Springboot接口字符串判空
- 互联网
- 2026年04月04日
-
springbootvue环境搭建
springbootvue环境搭建
- 互联网
- 2026年04月04日
-
springboot2.2.2企业级项目整合redis与redis 工具类大全
springboot2.2.2企业级项目整合redis与redis 工具类大全
- 互联网
- 2026年04月04日






