解决spring 自动扫描包的时候出现 service无法自动注入的问题

遇到过这样的问题,使用spring 将 包自动扫描进来在访问的时候出现如下报错:

Error creating bean with name ‘com.xxx.demoController’:
Injection of autowired dependencies failed; nested exception is
org.springframework.beans.factory.BeanCreationException: Could not
autowire field:demoService

demoService不能自动注入,后来发现是我在配置定时器的时候,将这个service中的某个方法做了spring的定时任务,相当于显示的将这个类已经扫描到spring的配置中。这个时候通过自动扫描又将这个类有扫描了一遍,出现了两个bean ,bean不是唯一的。由于要保留定时任务,所以要将spring中的自动扫描的地方去除关于这个类的扫描,在自动扫描的地方去除冲突的类或者包。

type="regex" expression="com.demo.demoService"/> 

参考文章:http://chenjinbo1983.iteye.com/blog/1749437

你可能感兴趣的:(java)