当前位置: 首页 > 知识库问答 >
问题:

NoSuchBeanDefinitionException:没有限定bean

郑嘉悦
2023-03-14
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'webController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.nickolay.app.service.UserService com.nickolay.app.WebController.userService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.nickolay.app.service.UserService] 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.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538)
    at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:667)
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:633)
    at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:681)
    at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:552)
    at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:493)
    at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136)
    at javax.servlet.GenericServlet.init(GenericServlet.java:158)
    at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1231)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1144)
    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1031)
    at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4997)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5289)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.StandardContext.reload(StandardContext.java:3831)
    at org.apache.catalina.loader.WebappLoader.backgroundProcess(WebappLoader.java:292)
    at org.apache.catalina.core.StandardContext.backgroundProcess(StandardContext.java:5616)
    at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1377)
    at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1381)
    at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1381)
    at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.run(ContainerBase.java:1349)
    at java.lang.Thread.run(Thread.java:745)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.nickolay.app.service.UserService com.nickolay.app.WebController.userService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.nickolay.app.service.UserService] 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:573)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
    ... 31 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.nickolay.app.service.UserService] 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:1373)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1119)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:545)
    ... 33 more
@Controller
public class WebController {


    @Autowired
    private UserService userService;

.................
}
@Service
public class UserService implements UserServiceInterface {

    @Autowired
    private UserDao userDao;

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    @Override
    @Transactional
    public void create(User user) {
        userDao.create(user);
    }
................
}
@Repository("UserDao")
public class HibernateUserDao implements UserDao {

    @Autowired
    SessionFactory sessionFactory;

    public HibernateUserDao() {
    }

    @Override
    @Transactional
    public void create(User user) {
        if (user == null) {
            throw new NullPointerException();
        }
        Session session = sessionFactory.getCurrentSession();
        session.save(user);
    }
.............
}

我的servlet-contents.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd"


    <annotation-driven />

    <tx:annotation-driven />

    <resources mapping="/resources/**" location="/resources/" />


    <beans:bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/WEB-INF/views/" />
        <beans:property name="suffix" value=".jsp" />
    </beans:bean>

    <context:component-scan base-package="com.nickolay.app" />

    <beans:bean id="userDao" class="com.nickolay.app.dao.HibernateUserDao" />
    <beans:bean id="roleDao" class="com.nickolay.app.dao.HibernateRoleDao" />
    <beans:bean id="userService" class="com.nickolay.app.service.UserService">
    </beans:bean>


</beans:beans>

共有1个答案

拓拔奇
2023-03-14

在这种情况下,您无法注入具体类UserService。这是因为Spring正在为您的类创建一个代理,以便处理@Transactional。由于您的服务实现了一个接口,Spring正在创建一个基于JDK接口的代理,即代理实现UserServiceInterface和对UserService的委托。因此Spring管理的bean不是UserService而是UserServiceInterface

有两种方法可以解决。最简单的方法是将依赖项从UserService更改为UserServiceInterface

@Controller
public class WebController {

    @Autowired
    private UserServiceInterface userService;

}

第二种方法是指示Spring使用基于CGLIB类的代理,而不是基于JDK接口的代理。将tx:annotation-driven更改为将proxy-target-class指定为true

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd"


    <annotation-driven />

    <tx:annotation-driven proxy-target-class="true/>

    <resources mapping="/resources/**" location="/resources/" />


    <beans:bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/WEB-INF/views/" />
        <beans:property name="suffix" value=".jsp" />
    </beans:bean>

    <context:component-scan base-package="com.nickolay.app" />

    <beans:bean id="userDao" class="com.nickolay.app.dao.HibernateUserDao" />
    <beans:bean id="roleDao" class="com.nickolay.app.dao.HibernateRoleDao" />
    <beans:bean id="userService" class="com.nickolay.app.service.UserService">
    </beans:bean>


</beans:beans>
 类似资料:
  • 我在调用get请求(modes-calcul)时遇到这个错误,我不明白为什么...我的依赖注入是正确的吗? ModeCalculController: 谢谢你的帮助..

  • 我正在尝试将我的应用程序spring配置从xml迁移到注释。因此,我做了一些改变。首先,我从DAO层开始,然后我会做服务层,然后是Web层。迁移所有DAO对象并用存储库注释替换基于XML的配置后,我收到以下错误消息: 包有作为配置文件,它引用Web层中使用的bean。包有作为配置文件,它引用服务的bean。包包含POJO和道豆。 Applicationontext.xml源代码: serviceC

  • 2020-09-23T15:28:00.3483912Zjava.lang.IllegalStateExcture:未能加载Application ationContext 2020-09-23T15:28:00.3489821Z引起的原因:org.springframework.beans.factory.不满意依赖异常:创建在文件[/home/run/work/comation-service

  • 在使用MapStruct、Lombock和Spring时,我在Tomcat 9中的应用程序部署中遇到以下错误: 我的spring配置类在包层次结构的根目录中设置包扫描。映射器的实现也是在target/generated-sources下生成的: 我的错误表明Spring找不到实现类,我遗漏了什么?我试图将generated-sources文件夹添加到构建路径并将其包含在包扫描中,但没有成功。

  • 我在构建时遇到了这个错误。我在这里参考了其他的答案,但没有一个对我有效。 应用程序.属性 存储库: 实体: 和pom.xml 和主类(spring boot应用程序) 我从.m2目录中删除了所有文件夹。再次运行mvn clean install。得到了同样的错误。服务类自动连接存储库。控制器使用@RestController注释。 而这正是确切的原因: 原因:org.springframework

  • 我还有bean,它与位于同一个包中,并扩展了相同的类,但它的注入没有问题 你知道为什么会出现这个例外吗?