Spring 启动 自动调用方法的两种形式

package ....utils;

import javax.annotation.PostConstruct;
import org.springframework.stereotype.Service;
import org.springframework.stereotype.Component;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;

@Service
public class TomcatStart {
    
    @PostConstruct
     public void init(){
     
            System.out.println("开始执行....");
            
     }
}


@Component
public class TomcatStart implements ApplicationListener<ContextRefreshedEvent>{

    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
    
         System.out.println("开始执行....");
        
    }
}

 

posted @ 2020-11-12 17:32  Li&Fan  阅读(327)  评论(0编辑  收藏  举报