Spring服务启动后就执行某个方法

下边按照执行顺序前后,测试代码结果截图放到最后:

1、注解@PostConstruct

时间:当前bean被创建并且所有的依赖注入完成之后执行;

使用:当前bean 所在类内的某个方法上 添加该注解;该方法没有参数,返回值类型可以是void或任意其他类型。

2、实现InitializingBean

时间:当前bean被创建并且所有的依赖注入完成之后执行,在@PostConstruct之后;

使用:implements InitializingBean 重写 afterPropertiesSet() ,具体业务写在该方法内;

3、xml 配置bean: init-method

时间:当前bean被创建并且所有的依赖注入完成之后执行,在 afterPropertiesSet() 之后;

使用:xml中定义bean,指定 为 单例模式,且init-method参数值 设置为类内的一个方法名;

4、实现SmartInitializingSingleton(spring4.1及 以上)

时间:在 所有bean 初始化完成后 执行;

使用:implements SmartInitializingSingleton,重写afterSingletonsInstantiated(),具体业务写在该方法内;

一般 MQ、HTTP、监听器、定时任务   设置在此时启动;

5、实现ApplicationListener

时间:spring服务启动后 执行;

使用:implements ApplicationListener,重写onApplicationEvent(),具体业务写在该方法内;

6、springboot 启动类main方法

如果是springboot 项目就简单,在main方法SpringApplication.run(*****Application.class, args)之后写上业务逻辑。

时间:springBoot服务启动完成之后;

@Component
public class MyIInitializingBean implements InitializingBean {

    @Autowired
    private StudentExportImpl studentExport;

    public MyIInitializingBean() {
        System.out.println("当前类的Bean初始化后:执行构造器方法完成,,,,");
    }

    @PostConstruct
    public void myInitBean() {
        System.out.println("@Autowired 是否注入完成:"+ (studentExport != null ? "是":"否"));
        System.out.println("当前类的Bean初始化后:执行@PostConstruct注解的方法完成,,,,");
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("当前类的Bean初始化后:执行实现InitializingBean接口重写的方法完成,,,,");
    }

}
@Component
public class MySmartInitiailzingSingleton implements SmartInitializingSingleton {
    @Override
    public void afterSingletonsInstantiated() {
        System.out.println("所有bean初始化完成后:执行实现SmartInitializingSingleton接口重写的方法完成,,,,,,");
    }
}
@Component
public class MyAppliactionListener implements ApplicationListener {
    @Override
    public void onApplicationEvent(ApplicationEvent applicationEvent) {
        // 保证执行一次: 在root application context 初始化完成后调用
        if(((ServletWebServerInitializedEvent) applicationEvent).getApplicationContext().getParent() == null) {
            System.out.println("spring服务启动后:执行实现ApplicationListener接口重写的方法完成,,,,");
        }
    }
}
@SpringBootApplication
public class *****Application {

    public static void main(String[] args) {
        System.out.println("SpringBoot 开始启动,,,main方法开始执行,,,,");
        SpringApplication.run(*****Application.class, args);
        System.out.println("SpringBoot 已启动,,,main方法已执行完,,,,");
    }

}

Spring MVC应用启动完成后执行特定方法,可以通过以下几种方式实现: ### 1. 实现`ApplicationListener<ContextRefreshedEvent>`接口 通过监听`ContextRefreshedEvent`事件,可以在Spring容器初始化完成时执行特定的逻辑。以下是实现代码示例: ```java import org.springframework.context.ApplicationListener; import org.springframework.context.event.ContextRefreshedEvent; import org.springframework.stereotype.Component; @Component public class StartupListener implements ApplicationListener<ContextRefreshedEvent> { @Override public void onApplicationEvent(ContextRefreshedEvent event) { // 在这里编写需要执行的逻辑 System.out.println("Spring MVC 应用启动完成,执行特定方法..."); } } ``` 这种方式会在整个Spring上下文刷新完成后触发[^1]。 ### 2. 使用`@PostConstruct`注解 如果希望在某个Bean初始化完成后立即执行某些逻辑,可以使用`@PostConstruct`注解。需要注意的是,这种方式仅适用于单个Bean的初始化逻辑,而非整个应用启动完成后执行。 ```java import javax.annotation.PostConstruct; import org.springframework.stereotype.Component; @Component public class StartupBean { @PostConstruct public void init() { // 在这里编写需要执行的逻辑 System.out.println("Bean 初始化完成后执行特定方法..."); } } ``` 此方法适合用于Bean级别的初始化操作,但不适用于整个Spring MVC应用启动完成后执行逻辑[^2]。 ### 3. 实现`CommandLineRunner`接口 `CommandLineRunner`接口允许在Spring Boot应用启动完成后执行特定逻辑。虽然这是Spring Boot特有的功能,但在Spring MVC中也可以通过类似方式实现。 ```java import org.springframework.boot.CommandLineRunner; import org.springframework.stereotype.Component; @Component public class StartupRunner implements CommandLineRunner { @Override public void run(String... args) throws Exception { // 在这里编写需要执行的逻辑 System.out.println("Spring MVC 应用启动完成,执行特定方法..."); } } ``` 这种方式通常与Spring Boot结合使用,但在纯Spring MVC项目中也可以通过类似的事件监听机制实现[^3]。 ### 4. 配置`init-param`参数(Servlet方式) 如果希望在`DispatcherServlet`初始化完成后执行某些逻辑,可以通过配置`web.xml`中的`init-param`参数来实现。 ```xml <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> ``` 然后在`DispatcherServlet`初始化完成后,通过自定义逻辑实现特定方法执行[^4]。 ### 注意事项 - 如果使用`ApplicationListener`方式,请确保该类被Spring扫描到并注册为Bean。 - 如果使用`@PostConstruct`,请确保该方法所在的类是一个Spring管理的Bean。 - 如果使用`CommandLineRunner`,建议结合Spring Boot项目使用,否则需要额外配置事件监听机制。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值