解決spring懶加載以及@PostConstruct結合的坑
spring懶加載及@PostConstruct的坑
舉例說明:
下面是一個初始化數(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這個bean只是被掃描而沒有被注入,那么ErrorMsgUtil里的map永遠是空的。
@PostConstruct實在bean初始化的時候被創(chuàng)建的,開啟了懶加載顯然如果InitData沒有被用到那么就一直不執(zhí)行了。
此坑已踩,小弟還是對spring理解不深,繼續(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注入不成功
前兩天做了個純java代碼的rabbitMQ監(jiān)聽多個ip的客戶端功能,由于用的不是配置方式的listener方式—博文中有這一節(jié),無法自動啟動。就用@PostConstruct來項目啟動時運行監(jiān)聽mq,但是老遇到調(diào)用業(yè)務邏輯層方法時,注入不成功導致空指針異常。今天排查了一下,發(fā)現(xiàn)主要問題是框架掃包忽略了。
直接先說原因吧
1.忽略ssm本身對注解是通過掃包才讓注解有效的
<!-- 自動掃描該包,支持注解的層限制,把api這個controller層排除在外了。另外多個包中間用逗號或者分號隔開都可以。 --> <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層(相當于service層),common工具層。所以在api層(相當于controller層)用@Service和不用注解都是錯誤的,都會導致注入失敗。
3.注意掃包區(qū)間
出了這個范圍@PostConstruct是無效的。應用在啟動時是不會走帶有這個注解的方法的。
4.@PostCoustruct注解用于
在依賴關系注入完成之后需要執(zhí)行的方法上,以執(zhí)行任何初始化。此方法所在的類必須放入服務之前調(diào)用。也就是該注解的類上不能隨便注解:經(jīng)驗總結是能用@Service注解,不能用@Controller注解,否則啟動不會走這個方法。這個類定位為服務層/業(yè)務層。而不是控制層(web層)
有了上面說的注意點。我重新在工具類包common包中寫了個測試類。然后spring配置文件上掃包范圍增加了這個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()); } }
結果是注入成功,運行結果如下:
這樣@PostConstruct注入問題就解決了。
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
java String.split 無法使用小數(shù)點分割的問題
這篇文章主要介紹了java String.split 無法使用小數(shù)點分割的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02