溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

怎么解決spring懶加載和@PostConstruct結(jié)合的坑

發(fā)布時(shí)間:2021-12-21 17:07:07 來源:億速云 閱讀:292 作者:iii 欄目:開發(fā)技術(shù)

本篇內(nèi)容主要講解“怎么解決spring懶加載和@PostConstruct結(jié)合的坑”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“怎么解決spring懶加載和@PostConstruct結(jié)合的坑”吧!

spring懶加載及@PostConstruct的坑

舉例說明:

下面是一個(gè)初始化數(shù)據(jù)的組件

@Component
public class InitData {
    /**
     * 初始化加載bean
     */
    @PostConstruct
    public void init() {
        Map<String, String> map = new HashMap<String, String>();
        for (int i=0;i<10;i++) {
            map.put(i+"", i+"");
        }
        //模擬加載一些別單例模式bean的數(shù)據(jù)初始化
        ErrorMsgUtil1.getInstance().setMap(map);
        ErrorMsgUtil2.getInstance().setMap(map);
    }

好了,如果你開啟了spring的懶加載模式,而且 InitData這個(gè)bean只是被掃描而沒有被注入,那么ErrorMsgUtil里的map永遠(yuǎn)是空的。

@PostConstruct實(shí)在bean初始化的時(shí)候被創(chuàng)建的,開啟了懶加載顯然如果InitData沒有被用到那么就一直不執(zhí)行了。

此坑已踩,小弟還是對(duì)spring理解不深,繼續(xù)學(xué)習(xí)。

ps:如何開啟spring的懶加載模式,在spring.xml中加上下面的代碼中最后一句即可

<?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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="  
    http://www.springframework.org/schema/beans   
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd  
    http://www.springframework.org/schema/context  
    http://www.springframework.org/schema/context/spring-context-3.0.xsd  
    " default-lazy-init="true">

遺留問題 @PostConstruct注入不成功

前兩天做了個(gè)純java代碼的rabbitMQ監(jiān)聽多個(gè)ip的客戶端功能,由于用的不是配置方式的listener方式—博文中有這一節(jié),無法自動(dòng)啟動(dòng)。就用@PostConstruct來項(xiàng)目啟動(dòng)時(shí)運(yùn)行監(jiān)聽mq,但是老遇到調(diào)用業(yè)務(wù)邏輯層方法時(shí),注入不成功導(dǎo)致空指針異常。今天排查了一下,發(fā)現(xiàn)主要問題是框架掃包忽略了。

直接先說原因吧

1.忽略ssm本身對(duì)注解是通過掃包才讓注解有效的
<!-- 自動(dòng)掃描該包,支持注解的層限制,把a(bǔ)pi這個(gè)controller層排除在外了。另外多個(gè)包中間用逗號(hào)或者分號(hào)隔開都可以。 -->
<context:component-scan base-package="com.**.service,com.**.action,com.**.common" >
    <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    <context:exclude-filter type="annotation" expression="org.springframework.stereotype.api"/>
</context:component-scan>
2.忽略@Service的注解

由于掃包是掃service層和action層(相當(dāng)于service層),common工具層。所以在api層(相當(dāng)于controller層)用@Service和不用注解都是錯(cuò)誤的,都會(huì)導(dǎo)致注入失敗。

3.注意掃包區(qū)間

出了這個(gè)范圍@PostConstruct是無效的。應(yīng)用在啟動(dòng)時(shí)是不會(huì)走帶有這個(gè)注解的方法的。

4.@PostCoustruct注解用于

在依賴關(guān)系注入完成之后需要執(zhí)行的方法上,以執(zhí)行任何初始化。此方法所在的類必須放入服務(wù)之前調(diào)用。也就是該注解的類上不能隨便注解:經(jīng)驗(yàn)總結(jié)是能用@Service注解,不能用@Controller注解,否則啟動(dòng)不會(huì)走這個(gè)方法。這個(gè)類定位為服務(wù)層/業(yè)務(wù)層。而不是控制層(web層)

怎么解決spring懶加載和@PostConstruct結(jié)合的坑

有了上面說的注意點(diǎn)。我重新在工具類包c(diǎn)ommon包中寫了個(gè)測試類。然后spring配置文件上掃包范圍增加了這個(gè)common包。代碼如下:com.zhanglf.common.cache.CommonCacheMap.java

package com.zhanglf.common.cache;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.zlf.bo.StaffBo;
import com.zlf.service.IStaffService;
@Service("CommonCacheMap")
public class CommonCacheMap {
    @Resource
    private IStaffService staffService;
    @PostConstruct
    public void getOneStaff(){
        StaffBo staffBo = staffService.selectByPrimaryKey("s01");
        System.out.println(staffBo.getName());
    }
}

結(jié)果是注入成功,運(yùn)行結(jié)果如下:

怎么解決spring懶加載和@PostConstruct結(jié)合的坑

到此,相信大家對(duì)“怎么解決spring懶加載和@PostConstruct結(jié)合的坑”有了更深的了解,不妨來實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI