【发布时间】:2016-01-06 17:39:05
【问题描述】:
我从 spring-boot 开始,我有一些配置问题。我无法自动装配某些服务。我得到一个 BeanCreationException。
我的应用程序类:
@SpringBootApplication
@EnableAutoConfiguration
@ComponentScan(basePackages = "com.x.server", basePackageClasses = { OAuth2ServerConfiguration.class,
AController.class, BController.class })
public class Application extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
public static void main(String[] args) {
new Application().configure(new SpringApplicationBuilder(Application.class)).run(args);
}
}
继承我的项目结构:
----com.x.server
--------Application.java
----com.x.server.controller
--------AController.java
--------BController.java
----other packages
----com.x.server.service
--------WYXService
--------ABCService
----com.x.server.service.serviceImpl
--------WYXServiceImpl
--------ABCServiceImpl
我的服务接口
public interface WYXService<T> {
public void setClazz(final Class<T> clazzToSet);
public void createEntity(T t);
public void removeEntity(T t);
}
以及实现
@Transactional
@Service("wyxService")
public class WYXServiceImpl<T> implements WYXService<T>
Hier 是我自动连接服务的控制器:
@Path("/test")
@Component
@Controller
@Produces(MediaType.APPLICATION_JSON)
public class BController {
@Autowired
WYXService wyxService;
控制台的错误:
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.x.server.service.WYXService com.x.server.controller.BController.wyxService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.x.server.service.WYXService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:561)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
... 25 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.x.server.service.WYXService ] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1301)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1047)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:942)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:533)
... 27 more
当我从 compenentscan 中删除两个控制器时,我没有出现此错误,但我需要扫描这些控制器以访问端点。
有人有什么想法吗?
干杯
【问题讨论】:
-
您是否尝试过将相关类包含到 basePackageClasses 中,看看是否能找到它?
-
只是一个建议,请您尝试将@Service("wyxService") 更改为@Service("WYXService")
-
@Aeseir,您指的是哪个相关课程?控制器?我用componentscan做了。 @ComponentScan(basePackages = "com.x.server", basePackageClasses = { OAuth2ServerConfiguration.class, AController.class, BController.class })
-
WYXService 类。我遇到了一个问题,它没有被 basePackages 拾取,但是当我直接在 basePackageClasses 下添加它时,它没有问题。您还可以将整个堆栈错误放入开始结束,因为可能存在导致级联故障的隐藏内容。
标签: java spring spring-mvc spring-boot