无法自动装配字段

无法自动装配字段

本文介绍了spring boot org.springframework.beans.factory.BeanCreationException:无法自动装配字段:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从spring-boot开始,我遇到了一些配置问题。我不能自动提供某些服务。我得到一个BeanCreationException。

I'm starting with spring-boot and i have some configuration problems. I cannot autowire some services. I get an 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是我自动提供服务的控制器:

Hier is the controller where i autowire the service:

@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中删除两个控制器时,我没有此错误,但我需要扫描这些控制器以访问端点。

when i remove the two controllers from compenentscan, i don't have this error but i need to scan those controllers to access the endpoints.

有人有想法吗?

干杯

推荐答案

我也删除了球衣依赖:

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

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

and then I added the sprint-boot-starter-web dependency

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

这篇关于spring boot org.springframework.beans.factory.BeanCreationException:无法自动装配字段:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-29 16:51