java - Spring 启动 org.springframework.beans.factory.BeanCreationException : Could not autowire field:

标签 java spring spring-mvc spring-boot

我从 spring-boot 开始,遇到一些配置问题。我无法 Autowiring 某些服务。我收到 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

我的服务接口(interface)

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 是我自动连接服务的 Controller :

@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 中删除两个 Controller 时,我没有出现此错误,但我需要扫描这些 Controller 才能访问端点。

有人有什么想法吗?

干杯

最佳答案

我删除了 Jersey 依赖项:

 <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-jersey</artifactId>
 </dependency>

然后我添加了 sprint-boot-starter-web 依赖项

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

关于java - Spring 启动 org.springframework.beans.factory.BeanCreationException : Could not autowire field:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33026968/

相关文章:

java - 是否可以从同一情况下的方法中退出(返回)开关情况?

Spring Hibernate 事务在没有刷新的情况下不提交

java - JPA 删除失败(违反完整性约束 : foreign key no action) - data model too convoluted?

spring-mvc - Spring Webflux 与 Spring MVC - 使用 doOnEach 时发出空值

java - Spring 新手 - 使用 sec 标签不起作用

java - 如何使用ivy发布原生库?

java - 避免将对象中的某些属性包含在 WSDL 中

java - Shapefile 数据存储中的多个简单特征类型或属性中的列表类型

java - Spring - 在每次调用 Service/DAO 层时包含当前用户

java - Spring REST 服务 - 设置根 URI