項(xiàng)目使用的是SSM體系,spring的配置如下,配置沒問題,因?yàn)槲野l(fā)現(xiàn)其他文件中的@Value可以使用,只有一處@Value失效了。
創(chuàng)新互聯(lián)是一家專注于成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作與策劃設(shè)計(jì),薩爾圖網(wǎng)站建設(shè)哪家好?創(chuàng)新互聯(lián)做網(wǎng)站,專注于網(wǎng)站建設(shè)十年,網(wǎng)設(shè)計(jì)領(lǐng)域的專業(yè)建站公司;建站業(yè)務(wù)涵蓋:薩爾圖等地區(qū)。薩爾圖做網(wǎng)站價(jià)格咨詢:028-86922220
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 如果多個(gè),用“,”分隔--> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" /> </context:component-scan> <!-- 自定義注解實(shí)現(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> <!-- 導(dǎo)入配置文件--> <import resource="classpath*:mybatis-spring.xml" /> <import resource="classpath*:mail-spring.xml" /> <import resource="classpath*:rabbitmq-spring.xml" /> </beans>
失效的@Value是Parser這個(gè)父類的一個(gè)屬性上的注解,而Parser的兩個(gè)子類Parser1與Parser2繼承這個(gè)屬性;我的目的就是先用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é)點(diǎn)元素對象 } 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的那個(gè)類,在被其他類作為對象引用的話(被調(diào)用)。這個(gè)被調(diào)用的類也必須選擇注解的方式,注入到調(diào)用他的那個(gè)類中,不能用 new出來做對象,new出來的對象再注入其他bean就會 發(fā)生獲取不到的現(xiàn)象。所以要被調(diào)用的javabean,都需要@service,交給Spring去管理才可以,這樣他就默認(rèn)注入了。
于是我把代碼改成如下形式,注入成功了。
@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; } }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
標(biāo)題名稱:Spring@Value注解失效問題解決方案
瀏覽地址:http://sd-ha.com/article8/iedhop.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供做網(wǎng)站、全網(wǎng)營銷推廣、動態(tài)網(wǎng)站、軟件開發(fā)、商城網(wǎng)站、品牌網(wǎng)站設(shè)計(jì)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)