在spring中實(shí)例化bean無(wú)效的問(wèn)題
spring中實(shí)例化bean無(wú)效
在做Struts2和Spring整合時(shí)遇到Spring實(shí)例化無(wú)效的情況,
Action中代碼如下
public class UserAction extends ActionSupport {
? ? @Resource
? ? private UserService userService;
? ? public String execute(){
? ? ? ? //userService.saveUser(new Object());
? ? ? ? System.out.println(userService);
? ? ? ? System.out.println("struts2spring整合成功");
? ? ? ? return "success";
? ? }
}applicationContext.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:context="http://www.springframework.org/schema/context" ? ? ? ? xmlns:tx="http://www.springframework.org/schema/tx" ? ? ? ? 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 ? ? ? ? ? ? ? ? http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> ? ? <!-- 自動(dòng)掃描與裝配bean --> ? ? <context:component-scan base-package="com.bjwl"></context:component-scan> </beans>
通過(guò)注解實(shí)例化UserService時(shí)一直得到的是null。最后經(jīng)過(guò)查找,發(fā)現(xiàn)沒(méi)有導(dǎo)入Struts2-Spring-plugin.jar的原因。
spring實(shí)例化bean順序問(wèn)題,導(dǎo)致注入失敗
我們可以通過(guò)Spring進(jìn)行非常方便的管理bean,只需要在類上面加一個(gè)注解就可以進(jìn)行bean的注入,也就是所謂的DI。今天碰到了個(gè)小問(wèn)題,來(lái)總結(jié)一下。
問(wèn)題如下
public abstract class TestBean {
? ? public String str;
? ??
? ? public TestBean(){
? ? ? ? this.str = initStr();
? ? }
? ??
? ? protected abstract String initStr();
}
public class TestSon extends TestBean {
? ? @Resource
? ? public String str;
? ? @Override
? ? protected String initStr() {
? ? ? ? return this.str;
? ? }
}但是發(fā)現(xiàn)這個(gè)str始終是null。
原因
在實(shí)例化TestBean的時(shí)候不能確認(rèn)str已經(jīng)實(shí)例化,所以是先建立對(duì)象,再進(jìn)行注入str的值。那么創(chuàng)建對(duì)象的時(shí)候,根據(jù)構(gòu)造方法創(chuàng)建的對(duì)象中,還沒(méi)有注入str的值,所以只能為null。
解決
我們需要確認(rèn)在str已經(jīng)注入進(jìn)來(lái)的情況下再對(duì)父類中的str賦值,那么這個(gè)時(shí)候需要子類實(shí)現(xiàn) InitializingBean 這個(gè)接口,實(shí)現(xiàn)其中的afterPropertiesSet()
public class TestSon extends TestBean implements InitializingBean
{
? ? @Resource
? ? public String str;
? ? @Override
? ? protected String initStr() {
? ? ? ? return this.str;
? ? }
? ? @Override
? ? public void afterPropertiesSet() throws Exception {
? ? ? ? super.str = this.str;
? ? }
}問(wèn)題成功解決。注入成功
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
java版簡(jiǎn)單的猜數(shù)字游戲?qū)嵗a
猜數(shù)字游戲是一款經(jīng)典的游戲,該游戲說(shuō)簡(jiǎn)單也很簡(jiǎn)單,說(shuō)不簡(jiǎn)單確實(shí)也很難,那么下面這篇文章主要給大家介紹了java版簡(jiǎn)單的猜數(shù)字游戲的相關(guān)資料,文中給出了詳細(xì)的實(shí)現(xiàn)分析和示例代碼供大家參考學(xué)習(xí),需要的朋友們下面來(lái)一起看看吧。2017-05-05
Java接口方法默認(rèn)靜態(tài)實(shí)現(xiàn)代碼實(shí)例
這篇文章主要介紹了Java接口方法默認(rèn)靜態(tài)實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06
Java類型通配符應(yīng)用實(shí)戰(zhàn)分析
這篇文章主要介紹了Java類型通配符應(yīng)用實(shí)戰(zhàn),簡(jiǎn)單分析了Java類型通配符概念、原理并結(jié)合實(shí)例形式給出了Java類型通配符相關(guān)使用技巧,需要的朋友可以參考下2019-07-07

