spring在專案啟動時就執行特定方法
阿新 • • 發佈:2018-12-09
如何在專案啟動時就執行特定方法
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
}