如何将Spring Boot启动前执行方法代码的几种方式巧妙地改写成长尾?

2026-04-19 06:441阅读0评论SEO基础
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计549个文字,预计阅读时间需要3分钟。

如何将Spring Boot启动前执行方法代码的几种方式巧妙地改写成长尾?

第一种 + @PostConstruct + @Configurationpublic class Test1 { @Autowired private Environment environment; @PostConstruct public void test() { String property=environment.getProperty(aaa.bbb); System.out.println(test1 + property); }}

第一种 @PostConstruct注解

@Configuration public class Test1 { @Autowired private Environment environment; @PostConstruct public void test(){ String property = environment.getProperty("aaa.bbb"); System.out.println("test1"+property); } }

第二种 实现InitializingBean接口

@Configuration public class Test2 implements InitializingBean { @Autowired private Environment environment; @Override public void afterPropertiesSet() throws Exception { String property = environment.getProperty("aaa.bbb"); System.out.println("test2"+property); } }

第三种 实现BeanPostProcessor接口

@Configuration public class Test3 implements BeanPostProcessor { @Autowired private Environment environment; @Override public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { String property = environment.getProperty("aaa.bbb"); System.out.println("test3"+property); return bean; } @Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { return bean; } }

@Component public class Runner implements CommandLineRunner { @Override public void run(String... args) throws Exception { System.out.println("The Runner start to initialize ..."); } }

第四种 在启动类run之前执行方法

@SpringBootApplication public class DemoApplication { public static void main(String[] args) { System.out.println("test4"); SpringApplication.run(DemoApplication.class, args); } }

当然这是不可取的

他们运行的优先级是

启动类前->BeanPostProcessor->@PostConstruct->InitializingBean

值得注意的是第三种方式,他可以让实现类里的方法提前执行

同样的使用@PostConstruct的两个类

@Configuration public class Test1 { @PostConstruct public void test(){ System.out.println("test1"); } }

@Configuration public class Test3 implements BeanPostProcessor { @Autowired private Environment environment; @PostConstruct public void test(){ System.out.println("test3"); } }

第一个没有实现BeanPostProcessor接口

第二个实现了BeanPostProcessor接口,但是没有重写他的方法

如何将Spring Boot启动前执行方法代码的几种方式巧妙地改写成长尾?

可以看到同样是使用了@PostConstruct注解,但是他们的执行顺序却截然不同

BeanPostProcessor为每一个spring维护的对象调用前后做操作,实现了它我们当前类就会变成一个BeanPostProcessor对象,就可以像BeanPostProcessor一样在容器加载最初的几个阶段被实例化,只要被实例化,PostConstruct注解的标注的方法就会立即执行.

知道了启动时的加载顺序,对我们做一些初始化工作有帮助。

如果执行后加载时呢?

那就借助SpringBoot的ApplicationRunner及CommandLineRunner了,只要实现对应的接口即可:

import org.springframework.boot.ApplicationArguments; import org.springframework.boot.ApplicationRunner; import org.springframework.stereotype.Component; @Component public class JDDRunner implements ApplicationRunner { @Override public void run(ApplicationArguments args) throws Exception { System.out.println(args); System.out.println("这个是测试ApplicationRunner接口"); }

CommandLineRunner 同理!

如果控制执行顺序可以使用@Order(1) 来控制执行顺序

标签:几种

本文共计549个文字,预计阅读时间需要3分钟。

如何将Spring Boot启动前执行方法代码的几种方式巧妙地改写成长尾?

第一种 + @PostConstruct + @Configurationpublic class Test1 { @Autowired private Environment environment; @PostConstruct public void test() { String property=environment.getProperty(aaa.bbb); System.out.println(test1 + property); }}

第一种 @PostConstruct注解

@Configuration public class Test1 { @Autowired private Environment environment; @PostConstruct public void test(){ String property = environment.getProperty("aaa.bbb"); System.out.println("test1"+property); } }

第二种 实现InitializingBean接口

@Configuration public class Test2 implements InitializingBean { @Autowired private Environment environment; @Override public void afterPropertiesSet() throws Exception { String property = environment.getProperty("aaa.bbb"); System.out.println("test2"+property); } }

第三种 实现BeanPostProcessor接口

@Configuration public class Test3 implements BeanPostProcessor { @Autowired private Environment environment; @Override public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { String property = environment.getProperty("aaa.bbb"); System.out.println("test3"+property); return bean; } @Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { return bean; } }

@Component public class Runner implements CommandLineRunner { @Override public void run(String... args) throws Exception { System.out.println("The Runner start to initialize ..."); } }

第四种 在启动类run之前执行方法

@SpringBootApplication public class DemoApplication { public static void main(String[] args) { System.out.println("test4"); SpringApplication.run(DemoApplication.class, args); } }

当然这是不可取的

他们运行的优先级是

启动类前->BeanPostProcessor->@PostConstruct->InitializingBean

值得注意的是第三种方式,他可以让实现类里的方法提前执行

同样的使用@PostConstruct的两个类

@Configuration public class Test1 { @PostConstruct public void test(){ System.out.println("test1"); } }

@Configuration public class Test3 implements BeanPostProcessor { @Autowired private Environment environment; @PostConstruct public void test(){ System.out.println("test3"); } }

第一个没有实现BeanPostProcessor接口

第二个实现了BeanPostProcessor接口,但是没有重写他的方法

如何将Spring Boot启动前执行方法代码的几种方式巧妙地改写成长尾?

可以看到同样是使用了@PostConstruct注解,但是他们的执行顺序却截然不同

BeanPostProcessor为每一个spring维护的对象调用前后做操作,实现了它我们当前类就会变成一个BeanPostProcessor对象,就可以像BeanPostProcessor一样在容器加载最初的几个阶段被实例化,只要被实例化,PostConstruct注解的标注的方法就会立即执行.

知道了启动时的加载顺序,对我们做一些初始化工作有帮助。

如果执行后加载时呢?

那就借助SpringBoot的ApplicationRunner及CommandLineRunner了,只要实现对应的接口即可:

import org.springframework.boot.ApplicationArguments; import org.springframework.boot.ApplicationRunner; import org.springframework.stereotype.Component; @Component public class JDDRunner implements ApplicationRunner { @Override public void run(ApplicationArguments args) throws Exception { System.out.println(args); System.out.println("这个是测试ApplicationRunner接口"); }

CommandLineRunner 同理!

如果控制执行顺序可以使用@Order(1) 来控制执行顺序

标签:几种