SpringBoot启动自定义方法
SpringBoot启动自定义方法
一、实现CommandLineRunner接口,重写run方法
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import com.gzy.demo.NettyClient;
@Component
public class MyStart1 implements CommandLineRunner{
@Override
public void run(String... args) throws Exception {
.connect(10007, "127.0.0.1");
NettyClient}
}
二、实现ApplicationRunner接口,重写un方法
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import com.gzy.demo.NettyClient;
@Component
public class MyStart2 implements ApplicationRunner {
@Override
public void run(ApplicationArguments args) throws Exception {
.connect(10007, "127.0.0.1");
NettyClient}
}
三、初始化方法加上@PostConstruct注解
import javax.annotation.PostConstruct;
import org.springframework.stereotype.Component;
import com.gzy.demo.NettyClient;
@Component
public class MyStart3{
@PostConstruct
public void test() throws Exception {
.connect(10007, "127.0.0.1");
NettyClient}
}
四、其他实现思路
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.gzy.demo.NettyClient;
@Configuration
public class MyStart4 {
@Bean
public String startNetty() {
try {
.connect(10007, "127.0.0.1");
NettyClient} catch (Exception e) {
.printStackTrace();
e}
return "startNetty";
}
}
About ME
👋 读书城南,🤔 在未来面前,我们都是孩子~
- 📙 一个热衷于探索学习新方向、新事物的智能产品经理,闲暇时间喜欢coding💻、画图🎨、音乐🎵、学习ing~
👋 Social Media
🛠️ Blog: http://oceaneyes.top
⚡ PM导航: https://pmhub.oceangzy.top
☘️ CNBLOG: https://www.cnblogs.com/oceaneyes-gzy/
🌱 AI PRJ自己部署的一些算法demo: http://ai.oceangzy.top/
📫 Email: 1450136519@qq.com
💬 WeChat: OCEANGZY
💬 公众号: UncleJoker-GZY
👋 加入小组~
👋 感谢打赏~
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 OCAEN.GZY读书城南!