Spring提供的解決方案三種:
1.InitializingBean
package com.foriseland.fsoa.fabricca;
import com.foriseland.fsoa.fabricca.service.IVerifyNumberService;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
* @version V1.0
* @Description
* @Author ZhangYue
* @Date 2018/4/12 11:58
*/
@Component
public class InitData2 implements InitializingBean {
@Autowired
private IVerifyNumberService iVerifyNumberService;
/**
* Invoked by a BeanFactory after it has set all bean properties supplied
* (and satisfied BeanFactoryAware and ApplicationContextAware).
* <p>This method allows the bean instance to perform initialization only
* possible when all bean properties have been set and to throw an
* exception in the event of misconfiguration.
*
* @throws Exception in the event of misconfiguration (such
* as failure to set an essential property) or if initialization fails.
*/
@Override
public void afterPropertiesSet() throws Exception {
Integer number = iVerifyNumberService.findAllNumber();
System.out.println(number);
}
}
若采用XML來配置Bean的話,可以指定屬性init-method。
2.ApplicationListener
package com.foriseland.fsoa.fabricca;
import com.foriseland.fsoa.fabricca.service.IVerifyNumberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;
/**
* @version V1.0
* @Description
* @Author ZhangYue
* @Date 2018/4/12 10:35
*/
@Component
public class InitData implements ApplicationListener<ContextRefreshedEvent> {
@Autowired
private IVerifyNumberService iVerifyNumberService;
/**
* Handle an application event.
*
* @param event the event to respond to
*/
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
//保證在web容器中只執行一次該事件
if (event.getApplicationContext().getParent() == null) {
Integer number = iVerifyNumberService.findAllNumber();
System.out.println(number);
}
}
}
注意是監聽的ContextRefreshedEvent事件。
在web 項目中(spring mvc),系統會存在兩個容器,一個是root application context ,另一個就是我們自己的 projectName-servlet context(作為root application context的子容器)。這種情況下,就會造成onApplicationEvent方法被執行兩次。為了避免上面提到的問題,我們可以只在root application context初始化完成后調用邏輯代碼,其他的容器的初始化完成,則不做任何處理。
event.getApplicationContext().getParent() == null
3.@PostConstruct
package com.foriseland.fsoa.fabricca;
import com.foriseland.fsoa.fabricca.service.IVerifyNumberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
/**
* @version V1.0
* @Description
* @Author ZhangYue
* @Date 2018/4/12 12:01
*/
@Component
public class InitData3 {
@Autowired
private IVerifyNumberService iVerifyNumberService;
@PostConstruct
public void testInit(){
System.out.println(iVerifyNumberService.findAllNumber());
}
}
個人建議用第一種方法