最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

項目使用的是SSM體系,spring的配置如下,配置沒問題,因為我發(fā)現(xiàn)其他文件中的@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>

  <!-- 自定義注解實現(xiàn)日志記錄 -->
  <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讀取文件。經(jīng)過我的測試,我發(fā)現(xiàn)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;
  }

}

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

原因是如果有注入bean的那個類,在被其他類作為對象引用的話(被調用)。這個被調用的類也必須選擇注解的方式,注入到調用他的那個類中,不能用 new出來做對象,new出來的對象再注入其他bean就會 發(fā)生獲取不到的現(xiàn)象。所以要被調用的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中fork-join的原理解析

    java中fork-join的原理解析

    Fork/Join框架是Java7提供用于并行執(zhí)行任務的框架,是一個把大任務分割成若干個小任務,今天通過本文給大家分享java中fork join原理,感興趣的朋友一起看看吧
    2021-04-04
  • 解決IDEA中Maven依賴包導入失敗報紅問題(總結最有效8種解決方案)

    解決IDEA中Maven依賴包導入失敗報紅問題(總結最有效8種解決方案)

    這篇文章主要介紹了解決IDEA中Maven依賴包導入失敗報紅問題,本文通過圖文詳解給大家總結了最有效的8種解決方法,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-07-07
  • Springboot整合Shiro之加鹽MD5加密的方法

    Springboot整合Shiro之加鹽MD5加密的方法

    這篇文章主要介紹了Springboot整合Shiro之加鹽MD5加密的方法,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2018-12-12
  • 關于Java中@SuppressWarnings的正確使用方法

    關于Java中@SuppressWarnings的正確使用方法

    這篇文章主要介紹了關于Java中@SuppressWarnings的正確使用方法,@SuppressWarnings注解主要用在取消一些編譯器產生的警告對代碼左側行列的遮擋,有時候這會擋住我們斷點調試時打的斷點,需要的朋友可以參考下
    2023-05-05
  • 使用Java找出兩個List中的重復元素三種方法

    使用Java找出兩個List中的重復元素三種方法

    在Java編程中,我們經(jīng)常需要找出兩個列表(List)中的重復元素,在本文中,我們將探討三種方法來實現(xiàn)這一目標,需要的朋友可以參考下
    2023-10-10
  • java簡單坦克大戰(zhàn)制作代碼

    java簡單坦克大戰(zhàn)制作代碼

    這篇文章主要介紹了java簡單坦克大戰(zhàn)制作代碼,利用Java語言中的集合、Swing、線程等知識點編寫一個坦克大戰(zhàn)游戲,需要的朋友可以參考下
    2016-07-07
  • 深入了解java內存分配和回收策略

    深入了解java內存分配和回收策略

    下面小編就為大家?guī)硪黄钊肓私鈐ava內存分配和回收策略。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-06-06
  • SSH框架網(wǎng)上商城項目第25戰(zhàn)之使用java email給用戶發(fā)送郵件

    SSH框架網(wǎng)上商城項目第25戰(zhàn)之使用java email給用戶發(fā)送郵件

    這篇文章主要為大家詳細介紹了SSH框架網(wǎng)上商城項目第25戰(zhàn)之使用java email給用戶發(fā)送郵件,感興趣的小伙伴們可以參考一下
    2016-06-06
  • Spring Bean創(chuàng)建和循環(huán)依賴

    Spring Bean創(chuàng)建和循環(huán)依賴

    這篇文章主要介紹了Spring Bean創(chuàng)建和循環(huán)依賴,講述了Spring容器中?Bean?的創(chuàng)建過程已經(jīng)主要的方法,另外也著重分析了循環(huán)依賴的問題,需要的小伙伴可以參考一下
    2022-05-05
  • Java 數(shù)組的兩種初始化方式

    Java 數(shù)組的兩種初始化方式

    這篇文章主要介紹了Java 數(shù)組的兩種初始化方式,幫助大家更好的理解和學習使用Java,感興趣的朋友可以了解下
    2021-02-02

最新評論

诸暨市| 堆龙德庆县| 平定县| 宕昌县| 革吉县| 永川市| 鹤峰县| 沾益县| 昌黎县| 安阳市| 德安县| 易门县| 巴东县| 海盐县| 股票| 上虞市| 卢湾区| 湘西| 娄烦县| 西和县| 资源县| 徐州市| 乌恰县| 巩留县| 中山市| 闸北区| 习水县| 固镇县| 弥渡县| 靖西县| 孝义市| 筠连县| 崇文区| 仙桃市| 剑川县| 环江| 青阳县| 巫溪县| 托里县| 深州市| 台山市|