1. 程式人生 > >spring在專案啟動時就執行特定方法

spring在專案啟動時就執行特定方法

如何在專案啟動時就執行特定方法

1. 方法上加註解@PostConstruct

@Compant
public class InitDemo{

    @PostConstruct
    public void init(){
        //專案啟動就會執行這個方法
        doSomething();
    }
}

2.xml配置init-method

<bean id="InitDemo" class="com.xxx.InitDemo" scope="singleton" init-method="init">
</bean>

3.實現InitializingBean介面,重寫afterPropertiesSet方法

@Component
public class InitDemo implements InitializingBean {

    @Override
    public void afterPropertiesSet() throws Exception {

       //doSomeThing
    }