Spring Boot -10

@PostConstruct作用

     PostConstruct是一个注解,它被用于标记一个方法,该方法会在类实例化之后调用,但在依赖注入(DI)完成之前调用。它的作用是在依赖注入完成后,用于执行一些初始化操作。在Spring框架中,@PostConstruct注解通常与@Autowired注解一起使用,用于在依赖注入完成后执行一些必要的初始化操作,例如初始化数据库连接、加载配置文件等。

使用 `@PostConstruct` 注解

`@PostConstruct` 注解可以在 Spring Bean 被初始化后自动执行指定的方法。这对于在应用启动时执行一次性初始化任务非常有用。


import javax.annotation.PostConstruct;
import org.springframework.stereotype.Service;

@Service
public class StartupService {

    @PostConstruct
    public void init() {
        // 这里放置你需要在应用启动时执行的代码
        System.out.println("Application has started!");
    }
}

ApplicationRunner` 或 `CommandLineRunner` 接口

Spring Boot 提供了 `ApplicationRunner` 和 `CommandLineRunner` 接口,它们允许你在应用启动后执行特定的代码。这两者的区别在于传递给 `run` 方法的参数类型不同:

- `CommandLineRunner` 接口接收 `String... args` 参数。
- `ApplicationRunner` 接口接收 `ApplicationArguments` 参数,这可以提供更丰富的启动参数信息。

示例:实现 `CommandLineRunner`


import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Component
public class StartupCommandLineRunner implements CommandLineRunner {

    @Override
    public void run(String... args) throws Exception {
        // 这里放置你需要在应用启动时执行的代码
        System.out.println("Application has started with CommandLineRunner!");
    }
}

示例:实现 `ApplicationRunner`


import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

@Component
public class StartupApplicationRunner implements ApplicationRunner {

    @Override
    public void run(ApplicationArguments args) throws Exception {
        // 这里放置你需要在应用启动时执行的代码
        System.out.println("Application has started with ApplicationRunner!");
    }
}

使用 `SpringApplication` 的 `setListeners` 方法

你还可以通过设置 `SpringApplication` 的 `setListeners` 方法来在应用启动期间执行代码。创建一个实现 `ApplicationListener` 接口的监听器,然后将其添加到 `SpringApplication` 实例中。

示例:创建 `ApplicationListener`


import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

@Component
public class StartupApplicationListener implements ApplicationListener<ContextRefreshedEvent> {

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        // 这里放置你需要在应用启动时执行的代码
        System.out.println("Application context refreshed!");
    }
}

示例:配置 `SpringApplication`

在主应用类中,配置 `SpringApplication` 实例:


import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

@SpringBootApplication
public class YourApplication {

    public static void main(String[] args) {
        SpringApplication application = new SpringApplication(YourApplication.class);
        // 可以在这里添加其他配置
        ConfigurableApplicationContext context = application.run(args);
    }
}

@EeventListener` 注解

通过 `@EventListener` 注解,可以在 Spring Boot 应用启动时监听 `ContextRefreshedEvent` 事件来执行特定的代码。

**示例:使用 `@EventListener`**


import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;
import org.springframework.context.event.EventListener;

@Component
public class StartupEventListener {

    @EventListener(ContextRefreshedEvent.class)
    public void onApplicationEvent() {
        // 这里放置你需要在应用启动时执行的代码
        System.out.println("Application context refreshed (EventListener)!");
    }
}

 总结

     

Spring框架提供了多种方式来在不同的时机执行代码。这些方式包括使用注解和接口来定义钩子方法,从而实现在特定的时间点执行特定的代码。

首先,@PostConstruct注解可以用于在Spring Bean初始化后执行代码。当Spring容器完成对Bean的实例化、依赖注入和初始化后,会调用被@PostConstruct注解标记的方法。这个方法可以用来执行一些需要在Bean初始化后立即执行的逻辑。

其次,CommandLineRunnerApplicationRunner接口提供了一种在Spring Boot应用启动后执行代码的方式。实现这两个接口的类可以包含一个或多个run方法,当应用启动后,这些方法会按照定义的顺序被调用。这种方式可以用于在应用启动后执行一些初始化逻辑、数据加载等操作。

第三,ApplicationListener接口定义了一个监听应用事件的机制。通过实现ApplicationListener接口,可以监听和处理Spring应用上下文中的特定事件。当应用触发这些事件时,ApplicationListener的实现类会被调用,并执行相应的逻辑。这种方式可以用于在应用运行过程中对特定事件进行处理,比如在应用上下文刷新完成时执行一些操作。

最后,@EventListener注解可以用于监听应用事件并执行代码。这个注解可以直接标记在方法上,当触发对应的事件时,被@EventListener注解标记的方法会被调用。这种方式可以用于实现更细粒度的事件监听和处理。

  • 22
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值