如何在项目启动时就执行特定方法
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
}