繁体   English   中英

在Spring MVC控制器上注入单例

[英]Inject singleton on spring mvc controller

我上了Singleton课:

@Singleton
public class CacheManager {
    ...
}

在我的控制器中,我想注入这个Singleton:

@Controller
@RequestMapping("/myRequest.do")
public class ListeSinistresController extends AFoaGanController {

    @Autowired
    private CacheManager cacheManager;

    ...
}

在部署时,出现以下错误:

<2 juin 2015 14 h 31 CEST> <Warning> <HTTP> <BEA-101162> <User defined listener org.springframework.web.context.ContextLoaderListener failed:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'listeSinistresController': Injection of autowired dependencies failed;
nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.blabla.cache.CacheManager 
com.blabla.web.controller.personne.ListeSinistresController.cacheManager; nested exception is
org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [com.blabla.cache.CacheManager] found for dependency:
expected at least 1 bean which qualifies as autowire candidate for this dependency. 

我试图注入我的单身@Inject - @Authowired - @EJB并没有什么工作。

这样做的方式是什么?

Singleton的软件包导入是什么? 您可以尝试使用@Component注释吗? (提示:默认情况下,所有spring bean是单例,如果未显式配置为prototype )。

如果上述方法不起作用,请同时显示您的应用程序配置。 然后,软件包扫描可能不正确。

您必须将@Autowired与spring一起使用,这是正确的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM