Spring项目启动时执行某个业务

Spring项目启动时执行某个业务

Posted by wenfengSAT on March 19, 2019

当Spring项目启动时需要初始化执行某个业务,可以通过@PostConstruct实现


package com.example.demo.init;

import javax.annotation.PostConstruct;

import org.springframework.stereotype.Component;

@Component
public class InitDemo {

    @PostConstruct
    public void init(){
        //项目启动就会执行这个方法
        doSomething();
    }
    
    public void doSomething() {
    	System.out.println("=============================");
    	System.out.println("============启动执行===========");
    	System.out.println("=============================");
    }

}