SpringBoot应用启动成功后回调

应用

如果你有一些资源需要在SpringBoot启动后预加载, 可以通过SpringBoot给我们提供了两个接口完成: CammandLineRunnerApplicationRunner。两个接口的工作方式类似,如果有多个启动回调接口实现类,我们还可以通过添加@Order注解指定顺序

使用示例:

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

示例

CommandLineRunner接口
/**
 * 该类可用于SpringBoot应用启动后提前做一些初始化操作,比如缓存数据
 */
@Slf4j
@Component
public class MyInitialization implements CommandLineRunner {

	// args 可以获取启动应用时通过命令行传进来的参数
	@Override
	public void run(String... args) throws Exception {
		log.info("应用启动了,我准备初始化数据了");
		for(String arg : args) {
			log.info("arg: " + arg);
		}
	}
}
ApplicationRunner接口
/**
 * 该类可用于SpringBoot应用启动后提前做一些初始化操作,比如缓存数据
 */
@Slf4j
@Component
public class MyInitialization2 implements ApplicationRunner {

	// ApplicationArguments 参数获取形如:--name=zhangsan --gender=nan 格式的命令行参数
	@Override
	public void run(ApplicationArguments args) throws Exception {
		log.info("应用启动了,我准备初始化数据了");
		for(String argKey : args.getOptionNames()) {
			log.info("参数key: " + argKey+",参数值:" + args.getOptionValues(argKey));
		}
	}
}
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: 在 Spring Boot 中,您可以使用 `@PostConstruct` 和 `@EventListener` 注解来实现属性注入成功后的回调事件。 示例代码如下: ```java @Component public class MyService { @Value("${my.property}") private String myProperty; @PostConstruct public void init() { // 在属性注入成功后执行 System.out.println("My property value is: " + myProperty); } @EventListener(ApplicationReadyEvent.class) public void onApplicationReady() { // 在应用启动完成后执行 System.out.println("Application is ready!"); } } ``` 在上面的示例中,`@PostConstruct` 注解用于在属性注入成功后执行 `init()` 方法,而 `@EventListener` 注解用于在应用启动完成后执行 `onApplicationReady()` 方法。您可以根据您的需求在这些方法中添加自己的业务逻辑。 ### 回答2: 在Spring Boot中,当属性注入成功后,可以使用事件机制进行回调。具体步骤如下: 1. 创建一个自定义事件类,该类继承自ApplicationEvent,并在该类中定义需要回调的方法。 ```java public class CustomEvent extends ApplicationEvent { public CustomEvent(Object source) { super(source); } // 定义需要回调的方法 public void callbackMethod() { // 处理自定义事件的逻辑 System.out.println("属性注入成功,执行回调方法"); } } ``` 2. 在需要进行回调的地方,使用ApplicationEventPublisher对象发布自定义事件。 ```java @RestController public class UserController { @Autowired private ApplicationEventPublisher applicationEventPublisher; @PostMapping("/user") public void createUser() { // 创建自定义事件对象 CustomEvent event = new CustomEvent(this); // 发布自定义事件 applicationEventPublisher.publishEvent(event); } } ``` 3. 创建一个事件监听器,该监听器实现ApplicationListener接口,并在onApplicationEvent方法中进行事件的监听和处理。 ```java @Component public class CustomEventListener implements ApplicationListener<CustomEvent> { @Override public void onApplicationEvent(CustomEvent event) { // 获取事件源对象 Object source = event.getSource(); // 执行回调方法 if (source instanceof UserController) { UserController userController = (UserController) source; userController.callbackMethod(); } } } ``` 这样,在属性注入成功后,会触发自定义事件的发布,事件监听器会接收到该事件并执行回调方法,从而实现属性注入成功后的回调事件。 ### 回答3: Spring Boot 提供了一种方便的方式来注册属性注入成功后的回调事件。在应用程序的配置类上使用 `@ConfigurationPropertiesBinding` 注解,将属性绑定的转换器注册为一个 `Converter` bean,然后使用 `@PostConstruct` 注解将回调事件的处理方法标记为初始化方法。 首先,我们创建一个类来处理属性注入成功后的回调事件。在这个类中,我们使用 `@ConfigurationPropertiesBinding` 注解将属性绑定的转换器注册为一个 `Converter` bean,然后在 `@PostConstruct` 注解的方法中处理回调事件。 ```java @Configuration public class PropertyCallbackConfig { @Bean public ConversionService conversionService() { DefaultConversionService conversionService = new DefaultConversionService(); conversionService.addConverter(new MyPropertyConverter()); return conversionService; } @Bean public PropertyCallbackHandler propertyCallbackHandler() { return new PropertyCallbackHandler(); } @ConfigurationPropertiesBinding public static class MyPropertyConverter implements Converter<String, MyProperty> { @Override public MyProperty convert(String source) { // 根据需要的逻辑进行属性转换 return new MyProperty(source); } } public static class MyProperty { private String value; public MyProperty(String value) { this.value = value; } // getter 和 setter 方法省略 } public static class PropertyCallbackHandler { @Autowired private MyProperty myProperty; @PostConstruct public void handlePropertyCallback() { // 属性注入成功后的处理逻辑 System.out.println("属性注入成功,值为:" + myProperty.getValue()); } } } ``` 在这个例子中,`MyPropertyConverter` 类实现了 `Converter` 接口来进行属性转换,`MyProperty` 类用于存储属性的值。`PropertyCallbackHandler` 类使用 `@Autowired` 注解将属性注入到 `myProperty` 字段中,并使用 `@PostConstruct` 注解的方法来处理回调事件。 最后,我们需要在应用程序的启动类上使用 `@EnableConfigurationProperties` 注解来启用属性注入。 ```java @SpringBootApplication @EnableConfigurationProperties public class MyApplicaton { public static void main(String[] args) { SpringApplication.run(MyApplicaton.class, args); } } ``` 通过使用 Spring Boot 提供的这种方式,我们可以很方便地注册属性注入成功后的回调事件。当属性注入成功时,回调事件的处理方法将被自动调用,并可以在其中进行需要的处理逻辑。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值