项目使用的是SSM体系,spring的配置如下,配置没问题,因为我发现其他文件中的@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执行一定得判断逻辑——判断版本号,如果是版本1就用Parser1读取文件,如果是版本2就用Parser2读取文件。经过我的测试,我发现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(); //获取根节点元素对象
} 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就会 发生获取不到的现象。所以要被调用的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;
} }

我遇到的Spring的@Value注解失效问题的更多相关文章

  1. Spring中@Translational注解失效场景

    今天面试被问到@Translational注解什么场景下失效,我一脸懵逼,说的恍恍惚惚的,下来我就总结一下@Translational注解失效的场景! @Transactional 注解相信大家并不陌 ...

  2. spring mvc 自动扫描注解失效原因

    关于spring自动扫描,在控制层,采用注解配置@Controller,项目能够成功启动,且无任何报错.但是 在进行页面跳转时,并未进行相应的拦截,整个界面只能在默认界面 ,跳转报404,由于楼主初次 ...

  3. Spring的Bean内部方法调用无法使用AOP切面(CacheAble注解失效)

    Spring的Bean内部方法调用无法使用AOP切面(CacheAble注解失效) 前言 今天在使用Spring cache的Cacheable注解的过程中遇见了一个Cacheable注解失效的问题, ...

  4. Spring,SpringMvc配置常见的坑,注解的使用注意事项,applicationContext.xml和spring.mvc.xml配置注意事项,spring中的事务失效,事务不回滚原因

    1.Spring中的applicationContext.xml配置错误导致的异常 异常信息: org.apache.ibatis.binding.BindingException: Invalid ...

  5. spring事务注解失效问题

    问题描述: 由于工作需要,需要在spring中配置两个数据源,有一天突然发现@Transactional注解失效 环境框架: springmvc+spring+spring jdbcTemplate ...

  6. Spring aop注解失效

    问题 在spring 中使用 @Transactional . @Cacheable 或 自定义 AOP 注解时,对象内部方法中调用该对象的其他使用aop机制的方法会失效. @Transactiona ...

  7. SpringBoot @Async注解失效分析

    有时候在使用的过程中@Async注解会失效(原因和@Transactional注解有时候会失效的原因一样). 下面定义一个Service: 两个异步执行的方法test03()和test02()用来模拟 ...

  8. Spring Aop 动态代理失效分析

    1. Spring Aop 原理 Spring Aop 通过动态代理创建代理对象,在调用代理对象方法前后做增强. 2. Transactional, Async 注解失效? 当在动态代理方法中调用当前 ...

  9. Spring/SpringBoot常用注解总结

    转自:[Guide哥] 0.前言 可以毫不夸张地说,这篇文章介绍的 Spring/SpringBoot 常用注解基本已经涵盖你工作中遇到的大部分常用的场景.对于每一个注解我都说了具体用法,掌握搞懂,使 ...

随机推荐

  1. C#杂记-隐式类型的局部变量

    基础知识:方法中声明的变量,叫局部变量 普通局部变量:有明确数据类型. string name; 隐式类型的局部变量:使用“var”代替明确的数据类型. var name = "abc&qu ...

  2. Java开发笔记(六十七)清单:ArrayList和LinkedList

    前面介绍了集合与映射两类容器,它们的共同特点是每个元素都是唯一的,并且采用二叉树方式的类型还自带有序性.然而这两个特点也存在弊端:其一,为啥内部元素必须是唯一的呢?像手机店卖出了两部Mate20,虽然 ...

  3. Mybatis框架基础支持层——反射工具箱之MetaClass(7)

    简介:MetaClass是Mybatis对类级别的元信息的封装和处理,通过与属性工具类的结合, 实现了对复杂表达式的解析,实现了获取指定描述信息的功能 public class MetaClass { ...

  4. HTML和CSS前端教程05-CSS盒模型

    目录 1. CSS盒模型 1.1 元素的尺寸 1.2. 元素内边距 padding 1.3. 元素外边距 margin 1.4. 处理溢出overflow 1.5. 元素的可见性Visibility ...

  5. 广州.NET微软技术俱乐部 微信群有用信息集锦

    考虑到广州.NET微软技术俱乐部 微信群 十分活跃. 有用信息很有可能被淹没. 所以建立此贴. 首先群的活跃是十分重要的. 所以我是不可能把群搞得像技术论坛和github一样, 因为微信群的定位我在& ...

  6. 35.Odoo产品分析 (四) – 工具板块(6) – 午餐管理(1)

    查看Odoo产品分析系列--目录 很多公司为都会为员工提供午餐.然而,公司内部的午餐需要适当的管理,特别是在员工或供应商数量非常重要的时候."午餐订单"模块的开发,使管理更容易,也 ...

  7. EBGP在非直连网络时,需要配置ebgp的最大跳数,否则无法建立非直连的EBGP邻居

    结论: 1.默认情况下,EBGP只能在物理直连的路由器之间建立邻居. 2.要想配置非直连设备间的BGP邻居,必须加配置. 组网图: 抓包: 1.默认情况下,EBGP邻居之间的BGP报文的TTL为1. ...

  8. Fragment与Activity的接口回调

    这里说一个官方推荐的写法: private OnFragmentInteractionListener mListener; @Override public void onAttach(Contex ...

  9. JavaScript中的高阶函数

    之前写的<JavaScript学习手册>,客户跟我说有些内容不适合初学者,让我删了,感觉挺可惜的,拿到这里和大家分享. JavaScript中的一切都是对象,这句话同样适用于函数.函数对象 ...

  10. dotnet core 入门命令

    官方资料: https://docs.microsoft.com/zh-cn/dotnet/core/tools/dotnet-restore?tabs=netcore2x 常规 项目引用 NuGet ...