gpt4 book ai didi

java - 在 Spring 中将服务 bean Autowiring 到 XML 定义的 bean 中

转载 作者:搜寻专家 更新时间:2023-11-01 03:08:53 25 4
gpt4 key购买 nike

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

<beans:bean id="tokenStore" class="com.myproject.core.security.RemoteTokenStore" />

我也有我创建的这个类,并用@Service 注释:

com.myproject.core.service.CacheService

在我的 RemoteTokenStore.java 文件中,如果我尝试像这样 Autowiring CacheService:

@Autowired
private CacheService cacheService;

它始终为空。我该如何解决这个问题?

最佳答案

将此添加到您的上下文 xml 文件中:

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

一旦指定了这一点,您的注解将被处理,您的组件将被 Spring 识别。

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

正在阅读:http://www.mkyong.com/spring/spring-auto-scanning-components/可能有帮助

关于java - 在 Spring 中将服务 bean Autowiring 到 XML 定义的 bean 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14403934/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com