Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在Spring中将Autowire服务bean转换为XML定义的bean_Java_Spring_Spring Mvc - Fatal编程技术网

Java 在Spring中将Autowire服务bean转换为XML定义的bean

Java 在Spring中将Autowire服务bean转换为XML定义的bean,java,spring,spring-mvc,Java,Spring,Spring Mvc,在我的Spring应用程序中,我在xml中定义了一个bean: <beans:bean id="tokenStore" class="com.myproject.core.security.RemoteTokenStore" /> 在我的RemoteTokenStore.java文件中,如果我尝试像这样自动连接CacheService: @Autowired private CacheService cacheService; 它总是空的。如何修复此问题?将其添加到上下文xml文

在我的Spring应用程序中,我在xml中定义了一个bean:

<beans:bean id="tokenStore" class="com.myproject.core.security.RemoteTokenStore" />
在我的RemoteTokenStore.java文件中,如果我尝试像这样自动连接CacheService:

@Autowired
private CacheService cacheService;

它总是空的。如何修复此问题?

将其添加到上下文xml文件中:

<context:component-scan base-package="com.myproject.core.security" />

一旦这被指定,您的注释将被处理,您的组件将被Spring识别

如果还没有,那么还需要在应用程序XML中包含类路径和上下文命名空间上的
spring context
依赖项


阅读以下内容:可能会有所帮助

您没有提供足够的信息。哪个春季版本?哪个应用服务器

一般来说,你应该

<context:component-scan base-package="com.packagebase" /> 

在SpringXMLconf文件中使用带注释的bean

然而,它并不总是有效的


我对Spring2.5.6+JBossAS7有问题。Jboss一直报告未定义带注释的bean。(没有这样的定义或类似的东西)。我切换到spring 3.0.0。请放松以解决此问题

这并不完全正确<代码>足以启用许多注释(如
自动连线
),但您需要进行上述扫描,以便Spring搜索XML中未描述的带注释组件。更新了答案,因此不会产生误导:)在某些情况下,了解如何将XML定义的bean与自动连线的bean的引用混合使用是很有用的。有人知道怎么做吗?
<context:component-scan base-package="com.packagebase" />