欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

在spring中實(shí)例化bean無效的問題

 更新時(shí)間:2022年02月25日 10:34:28   作者:croder  
這篇文章主要介紹了在spring中實(shí)例化bean無效的問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

spring中實(shí)例化bean無效

在做Struts2和Spring整合時(shí)遇到Spring實(shí)例化無效的情況,

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>

通過注解實(shí)例化UserService時(shí)一直得到的是null。最后經(jīng)過查找,發(fā)現(xiàn)沒有導(dǎo)入Struts2-Spring-plugin.jar的原因。 

spring實(shí)例化bean順序問題,導(dǎo)致注入失敗

我們可以通過Spring進(jìn)行非常方便的管理bean,只需要在類上面加一個(gè)注解就可以進(jìn)行bean的注入,也就是所謂的DI。今天碰到了個(gè)小問題,來總結(jié)一下。

問題如下

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í)例化,所以是先建立對象,再進(jìn)行注入str的值。那么創(chuàng)建對象的時(shí)候,根據(jù)構(gòu)造方法創(chuàng)建的對象中,還沒有注入str的值,所以只能為null。

解決

我們需要確認(rèn)在str已經(jīng)注入進(jìn)來的情況下再對父類中的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;
? ? }
}

問題成功解決。注入成功

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Springboot以Repository方式整合Redis的方法

    Springboot以Repository方式整合Redis的方法

    這篇文章主要介紹了Springboot以Repository方式整合Redis的方法,本文通過圖文并茂實(shí)例詳解給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-04-04
  • java RSAUtils 加密工具類操作

    java RSAUtils 加密工具類操作

    這篇文章主要介紹了java RSAUtils 加密工具類操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-08-08
  • 使用Java把文本內(nèi)容轉(zhuǎn)換成網(wǎng)頁的實(shí)現(xiàn)方法分享

    使用Java把文本內(nèi)容轉(zhuǎn)換成網(wǎng)頁的實(shí)現(xiàn)方法分享

    這篇文章主要介紹了使用Java把文本內(nèi)容轉(zhuǎn)換成網(wǎng)頁的實(shí)現(xiàn)方法分享,利用到了Java中的文件io包,需要的朋友可以參考下
    2015-11-11
  • 使用C3P0改造JDBC對數(shù)據(jù)庫的連接

    使用C3P0改造JDBC對數(shù)據(jù)庫的連接

    這篇文章主要為大家詳細(xì)介紹了使用C3P0改造JDBC對數(shù)據(jù)庫的連接,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-08-08
  • IDEA+Maven打JAR包的兩種方法步驟詳解

    IDEA+Maven打JAR包的兩種方法步驟詳解

    Idea中為一般的非Web項(xiàng)目打Jar包是有自己的方法的,下面這篇文章主要給大家介紹了關(guān)于IDEA+Maven打JAR包的兩種方法,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-09-09
  • Java8新特性之新日期時(shí)間庫的使用教程

    Java8新特性之新日期時(shí)間庫的使用教程

    這篇文章主要給大家介紹了關(guān)于Java8新特性之新日期時(shí)間庫使用的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-12-12
  • MybatisPlus 插入或更新數(shù)據(jù)時(shí)自動(dòng)填充更新數(shù)據(jù)解決方案

    MybatisPlus 插入或更新數(shù)據(jù)時(shí)自動(dòng)填充更新數(shù)據(jù)解決方案

    本文主要介紹了MybatisPlus 插入或更新數(shù)據(jù)時(shí)自動(dòng)填充更新數(shù)據(jù)解決方案,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • Spring中Controller應(yīng)用深入理解

    Spring中Controller應(yīng)用深入理解

    這篇文章主要介紹了Spring項(xiàng)目中的Controller,Spring Controller本身也是一個(gè)Spring Bean,只是它多提供了Web能力,只需要造類上提供@Controller注解即可
    2022-12-12
  • Mybatis @SelectKey用法解讀

    Mybatis @SelectKey用法解讀

    這篇文章主要介紹了Mybatis @SelectKey用法解讀,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • 在java中使用dom解析xml的示例分析

    在java中使用dom解析xml的示例分析

    本篇文章介紹了,在java中使用dom解析xml的示例分析。需要的朋友參考下
    2013-05-05

最新評論