理論
剛好再開發過程中遇到了要在項目啟動后自動開啟某個服務,由於使用了spring,我在使用了spring的listener,它有onApplicationEvent()方法,在Spring容器將所有的Bean都初始化完成之后,就會執行該方法。
應用場景:很多時候我們想要在某個類加載完畢時干某件事情,但是使用了spring管理對象,我們這個類引用了其他類(可能是更復雜的關聯),所以當我們去使用這個類做事情時發現包空指針錯誤,這是因為我們這個類有可能已經初始化完成,但是引用的其他類不一定初始化完成,所以發生了空指針錯誤,解決方案如下:
1、寫一個類繼承spring的ApplicationListener監聽,並監控ContextRefreshedEvent事件(容易初始化完成事件)
2、定義簡單的bean:<bean id="beanDefineConfigue" class="com.creatar.portal.webservice.BeanDefineConfigue"></bean>
或者直接使用@Service注解方式
實戰:
這里就只給出直接使用注解的方式
1、編寫一個實現ApplicationListener的listener類,
import org.springframework.context.ApplicationListener; import org.springframework.context.event.ContextRefreshedEvent; import org.springframework.stereotype.Service; @Service public class StartAddDataListener implements ApplicationListener<ContextRefreshedEvent> { @Override public void onApplicationEvent(ContextRefreshedEvent event) { if(event.getApplicationContext().getParent() == null)//root application context 沒有parent,他就是老大. { //需要執行的邏輯代碼,當spring容器初始化完成后就會執行該方法。 System.out.println("\n\n\n\n\n______________\n\n\n加載了\n\n_________\n\n"); } //或者下面這種方式 if(event.getApplicationContext().getDisplayName().equals("Root WebApplicationContext")) { System.out.println("\n\n\n_________\n\n加載一次的 \n\n ________\n\n\n\n"); } } }
2、在配置文件(applicationContext-servlet.xml)中設置Service掃描的包
<!-- 注冊@Controller 、@Service--> <context:component-scan base-package="com.test.controller" use-default-filters="false"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" /> <context:include-filter type="annotation" expression="org.springframework.stereotype.Service" /> </context:component-scan>
3、部署啟動項目,即可在加載完spring后就打印出 “加載”
知識擴展:
<?xml version="1.0" encoding="UTF-8" ?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> <context:component-scan base-package=”com.eric.spring”> </beans>
component-scan標簽默認情況下自動掃描指定路徑下的包(含所有子包),將帶有@Component、@Repository、@Service、@Controller標簽的類自動注冊到spring容器。對標記了 Spring's @Required、@Autowired、JSR250's @PostConstruct、@PreDestroy、@Resource、JAX-WS's @WebServiceRef、EJB3's @EJB、JPA's @PersistenceContext、@PersistenceUnit等注解的類進行對應的操作使注解生效(包含了annotation-config標簽的作用)
可以使用對掃描的標簽進行過濾
context:exclude-filter:取消對被這些標簽標示的類加載
context:include-filter:掃描過程中要加載這些類
eg:
1 在主容器中(applicationContext.xml),將Controller的注解打消掉
<context:component-scan base-package="com"> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" /> </context:component-scan>
2 而在springMVC配置文件中將Service注解給去掉,只加載@Controller
<context:component-scan base-package="com"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" /> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" /> </context:component-scan>
致謝:感謝您的耐心閱讀!