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

Spring @Value注解失效問題解決方案

 更新時間:2020年04月08日 11:11:41   作者:Erneste  
這篇文章主要介紹了Spring @Value注解失效問題解決方案,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下

項目使用的是SSM體系,spring的配置如下,配置沒問題,因為我發(fā)現其他文件中的@Value可以使用,只有一處@Value失效了。

spring-servlet.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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:websocket="http://www.springframework.org/schema/websocket"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
            http://www.springframework.org/schema/mvc
            http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context-4.0.xsd
            http://www.springframework.org/schema/aop
            http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
            http://www.springframework.org/schema/tx
            http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
            http://www.springframework.org/schema/websocket
            http://www.springframework.org/schema/websocket/spring-websocket.xsd">

  <context:property-placeholder ignore-unresolvable="true" location="classpath*:config.properties" />


  <!-- 使用Annotation自動注冊Bean,Controllerller -->
  <context:component-scan base-package="com.magicmed.ecg" use-default-filters="false"> <!--base-package 如果多個,用“,”分隔-->
    <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
  </context:component-scan>

  <!-- 自定義注解實現日志記錄 -->
  <aop:aspectj-autoproxy />

  <mvc:annotation-driven />


  <import resource="classpath:mybatis-spring.xml" />
  <import resource="classpath:mail-spring.xml" />
  <import resource="classpath:rabbitmq-spring.xml" />

</beans>

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"
    xsi:schemaLocation="http://www.springframework.org/schema/context
    http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd">

  <bean id="propertyConfigurer"
     class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" >
    <property name="locations" >
      <list>
      </list>
    </property>
    <property name="ignoreUnresolvablePlaceholders" value="true" />
  </bean>


  <!-- 導入配置文件-->
  <import resource="classpath*:mybatis-spring.xml" />
  <import resource="classpath*:mail-spring.xml" />
  <import resource="classpath*:rabbitmq-spring.xml" />

</beans>

失效的@Value是Parser這個父類的一個屬性上的注解,而Parser的兩個子類Parser1與Parser2繼承這個屬性;我的目的就是先用Parser執(zhí)行一定得判斷邏輯——判斷版本號,如果是版本1就用Parser1讀取文件,如果是版本2就用Parser2讀取文件。經過我的測試,我發(fā)現Parser使用fileRoot屬性是不為null,也就是注入成功了,而Parser怎么也注入不成功,fileRoot的值為null。 代碼如下:

// parse
@Component
public class Parser {

  @Value("${fileRoot}")
  protected String fileRoot;   //文件根路徑


  protected String getFilePath(String appuserId, String uri) {
    return fileRoot + appuserId + System.getProperty("file.separator")+ uri;
  }


  public Map<String, String> getXML_version(String appuserId, String uri) {
    Element root = null;

    try {
      Document document = new SAXReader().read(new File(getFilePath(appuserId, uri) + ".xml"));
      root = document.getRootElement();  //獲取根節(jié)點元素對象
    } catch (DocumentException e) {
      e.printStackTrace();
    }
    return root.element("XMLInfo").element("Version").getTextTrim();
  }


  public Map<String, Object> read_xml(String appuserId, String uri) {
    return null;
  }

}

// parser1
@Component
public class Parser1 extends Parser {
  
  @Override
  public Map<String, Object> read_xml(String appuserId, String uri) {
    try {
      InputStream in = new FileInputStream(new File(getFilePath(appuserId, uri) + ".xml"));
    } catch (IOException e) {
      e.printStackTrace();
    }
    /**
     * 待處理的邏輯
     */
    return null;
  }

}


// parser2
@Component
public class Parser2 extends Parser {

  @Override
  public Map<String, Object> read_xml(String appuserId, String uri) {
    try {
      InputStream in = new FileInputStream(new File(getFilePath(appuserId, uri) + ".xml"));
    } catch (IOException e) {
      e.printStackTrace();
    }
    /**
     * 待處理的邏輯
     */
    return null;
  }

}
@Service
public class testServiceImpl implements testService {

  @Autowired
  private Parser parser;

  public Integer test(String id, String uri) {

    Map<String,String> versionMap = parser.getXML_version(id,uri);
    if(versionMap.get("mv").equals("1")){
      parser = new Parser1();
    }else if(versionMap.get("mv").equals("2")){
      parser = new Pparser2();
    }

    parser.read_xml(id,uri);

    return null;
  }

}

剛開始我也懷疑配置文件,也懷疑緩存的問題。后來我在網上查閱資料,找到這樣一段話,茅塞頓開:

原因是如果有注入bean的那個類,在被其他類作為對象引用的話(被調用)。這個被調用的類也必須選擇注解的方式,注入到調用他的那個類中,不能用 new出來做對象,new出來的對象再注入其他bean就會 發(fā)生獲取不到的現象。所以要被調用的javabean,都需要@service,交給Spring去管理才可以,這樣他就默認注入了。

于是我把代碼改成如下形式,注入成功了。

@Service
public class testServiceImpl implements testService {
  @Autowired
  private Parser parser;
  @Autowired
  private Parser1 parser1;
  @Autowired
  private Parser2 parser2;
  public Integer test(String id, String uri) {
    Map<String,String> versionMap = parser.getXML_version(id,uri);
    if(versionMap.get("mv").equals("1")){
      parser = parser1;
    }else if(versionMap.get("mv").equals("2")){
      parser = parser2;
    }
    parser.read_xml(id,uri);
    return null;
  }
}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • Java?數據結構與算法系列精講之貪心算法

    Java?數據結構與算法系列精講之貪心算法

    我們可能在好多地方都會聽到貪心算法這一概念,并且它的算法思想也比較簡單就是說算法只保證局部最優(yōu),進而達到全局最優(yōu)。但我們實際編程的過程中用的并不是很多,究其原因可能是貪心算法使用的條件比較苛刻,所要解決的問題必須滿足貪心選擇性質
    2022-02-02
  • 超簡單的java獲取鼠標點擊位置坐標的實例(鼠標在Jframe上的坐標)

    超簡單的java獲取鼠標點擊位置坐標的實例(鼠標在Jframe上的坐標)

    在Java窗體Jframe上獲取鼠標點擊的坐標,其中使用了匿名內部類,實例代碼非常簡單易懂,大家可以學習一下
    2018-03-03
  • IntelliJ安裝并使用Rust IDE插件

    IntelliJ安裝并使用Rust IDE插件

    這篇文章主要介紹了IntelliJ安裝并使用Rust IDE插件,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-01-01
  • JSON 與對象、集合之間的轉換的示例

    JSON 與對象、集合之間的轉換的示例

    在開發(fā)過程中,經常需要和別的系統(tǒng)交換數據,數據交換的格式有XML、JSON等,JSON作為一個輕量級的數據格式比xml效率要高,本篇文章主要介紹了JSON 與 對象 、集合 之間的轉換,有興趣的可以了解一下。
    2017-01-01
  • spring打包到jar包的問題解決

    spring打包到jar包的問題解決

    這篇文章主要給大家介紹了關于spring打包到jar包遇到的問題的解決方法,文中通過實例代碼結束的非常詳細,對大家的學習或者使用spring打包具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧
    2020-08-08
  • Java包裝類之自動裝箱與拆箱

    Java包裝類之自動裝箱與拆箱

    這篇文章主要介紹了Java包裝類之自動裝箱與拆箱,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-09-09
  • Struts2實現自定義攔截器的三種方式詳解

    Struts2實現自定義攔截器的三種方式詳解

    這篇文章主要介紹了Struts2實現自定義攔截器的三種方式詳解,一些與系統(tǒng)邏輯相關的通用功能如權限的控制和用戶登錄控制等,需要通過自定義攔截器實現,本節(jié)將詳細講解如何自定義攔截器,需要的朋友可以參考下
    2023-07-07
  • Java如何防止JS腳本注入代碼實例

    Java如何防止JS腳本注入代碼實例

    這篇文章主要介紹了Java如何防止JS腳本注入代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-09-09
  • Java注解之Elasticsearch的案例詳解

    Java注解之Elasticsearch的案例詳解

    學會了技術就要使用,否則很容易忘記,因為自然界壓根就不存在什么代碼、變量之類的玩意,這都是一些和生活常識格格不入的東西。這篇文章主要介紹了Java中Elasticsearch的案例,感興趣的可以了解一下
    2022-10-10
  • java中CopyOnWriteArrayList源碼解析

    java中CopyOnWriteArrayList源碼解析

    為了將讀取的性能發(fā)揮到極致,jdk中提供了CopyOnWriteArrayList類,下面這篇文章主要給大家介紹了關于java中CopyOnWriteArrayList源碼解析的相關資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2022-02-02

最新評論