国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

spring 容器加載完成后執行某個方法

2024-07-21 02:46:09
字體:
來源:轉載
供稿:網友
sPRing 容器加載完成后執行某個方法

理論

  剛好再開發過程中遇到了要在項目啟動后自動開啟某個服務,由于使用了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;@Servicepublic 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> 

  致謝:感謝您的耐心閱讀!


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 闻喜县| 新龙县| 大理市| 南岸区| 厦门市| 天祝| 二连浩特市| 金川县| 竹山县| 乌拉特中旗| 濮阳县| 外汇| 永吉县| 遂平县| 东乌珠穆沁旗| 嵊泗县| 平利县| 榆社县| 太保市| 铁岭县| 沙田区| 桃源县| 台安县| 邵武市| 江永县| 于都县| 垣曲县| 张家界市| 中山市| 临沂市| 商水县| 柘荣县| 观塘区| 宁国市| 堆龙德庆县| 和静县| 海淀区| 辽源市| 青海省| 亚东县| 亚东县|