Spring @Value注解失效问题解决方案

网友投稿 834 2023-06-15

Spring @Value注解失效问题解决方案

Spring @Value注解失效问题解决方案

项目使用的是SSM体系,spring的配置如下,配置没问题,因为我发现其他文件中的@Value可以使用,只有一处@Value失效了。

spring-servlet.xml

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:mvc="http://springframework.org/schema/mvc"

xmlns:context="http://springframework.org/schema/context"

xmlns:aop="http://springframework.org/schema/aop"

xmlns:tx="http://springframework.org/schema/tx"

xmlns:websocket="http://springframework.org/schema/websocket"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans-4.0.xsd

http://springframework.org/schema/mvc

http://springframework.org/schema/mvc/spring-mvc-4.0.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context-4.0.xsd

http://springframework.org/schema/aop

http://springframework.org/schema/aop/spring-aop-4.0.xsd

http://springframework.org/schema/tx

http://springframework.org/schema/tx/spring-tx-4.0.xsd

http://springframework.org/schema/websocket

http://springframework.org/schema/websocket/spring-websocket.xsd">

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:mvc="http://springframework.org/schema/mvc"

xmlns:context="http://springframework.org/schema/context"

xmlns:aop="http://springframework.org/schema/aop"

xmlns:tx="http://springframework.org/schema/tx"

xmlns:websocket="http://springframework.org/schema/websocket"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans-4.0.xsd

http://springframework.org/schema/mvc

http://springframework.org/schema/mvc/spring-mvc-4.0.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context-4.0.xsd

http://springframework.org/schema/aop

http://springframework.org/schema/aop/spring-aop-4.0.xsd

http://springframework.org/schema/tx

http://springframework.org/schema/tx/spring-tx-4.0.xsd

http://springframework.org/schema/websocket

http://springframework.org/schema/websocket/spring-websocket.xsd">

spring.xml

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://springframework.org/schdnMolema/context

http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd">

class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" >

xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://springframework.org/schdnMolema/context

http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd">

class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" >

class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" >

失效的@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 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 read_xml(String appuserId, String uri) {

return null;

}

}

// parser1

@Component

public class Parser1 extends Parser {

@Override

public Map 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 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 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 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;

}

}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Spring RedirectAttributes参数跳转代码实例
下一篇:Spring使用redis遇到的问题及解决方案
相关文章

 发表评论

暂时没有评论,来抢沙发吧~