繁体   English   中英

自动装配异常。 无法自动连线字段

[英]AutoWiring Exceptions. Cannot Autowire Field

我正在使用Spring MVC,Hibernate插入数据库,但出现自动装配错误。

获取Dis错误-java.lang.ClassNotFoundException:org.aspectj.util.PartialOrder $ PartialComparable

这是我的dispatcher-servlet.xml

            <?xml version="1.0" encoding="UTF-8"?>

            <beans
                xsi:schemaLocation="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.xsd 
                http://www.springframework.org/schema/mvc 
                http://www.springframework.org/schema/mvc/spring-mvc.xsd  
                http://www.springframework.org/schema/aop
                http://www.springframework.org/schema/aop/spring-aop.xsd"

                xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
                xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
                xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                xmlns="http://www.springframework.org/schema/beans">

                <mvc:annotation-driven />
                <context:annotation-config />
                <context:component-scan base-package="com.cts.*" />

                 <bean id="RegServiceImpl"class="com.cts.Services.RegistrationServiceImpl" />
 <bean id="RegDaoImpl" class="com.cts.Dao.RegistrationDaoImpl" /> 
                <mvc:resources location="/" mapping="/**" />
                <tx:annotation-driven proxy-target-class="true" />
                <bean class="org.springframework.jdbc.datasource.DriverManagerDataSource"
                    id="DataSource">
                    <property value="com.mysql.jdbc.Driver" name="driverClassName" />
                    <property value="jdbc:mysql://localhost:3306/shopingdata"
                        name="url" />
                    <property value="root" name="username" />
                    <property value="root" name="password" />
                </bean>



                <aop:config proxy-target-class="true" />

                <!-- <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> 
                    <property name="urlMap"> <map> <entry key="/login.html"> <ref bean="LoginController"/> 
                    </entry> </map> </property> </bean> -->

       <bean
        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
                    id="newSessionFactory">
                    <property name="dataSource" ref="DataSource" />
                    <property name="annotatedClasses">

                        <list>
                            <value>com.cts.entity.RegistrationDetails</value>
                            <value>com.cts.entity.LoginDetails</value>
                        </list>

                    </property>
                    <property name="hibernateProperties">

                        <props>
                            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                            <prop key="hibernate.hbm2ddl.auto">update</prop>
                            <prop key="hibernate.show_sql">true</prop>
                        </props>
                    </property>
                </bean>
                <bean class="org.springframework.orm.hibernate3.HibernateTransactionManager"
                    id="transactionManager">
                    <property name="sessionFactory" ref="newSessionFactory" />
                </bean>

                <bean id="h" class="org.springframework.orm.hibernate3.HibernateTemplate"
                    autowire="constructor" />
                <bean
                    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
                    <property name="prefix" value="/WEB-INF/views/" />
                    <property name="suffix" value=".jsp" />
                </bean>

                <bean id="messageSource"
                    class="org.springframework.context.support.ResourceBundleMessageSource">
                    <property name="basename" value="message" />
                </bean>


            </beans>

控制者

        package com.cts.Controller;

        import javax.servlet.http.HttpServletRequest;

        import org.hibernate.mapping.Map;
        import org.springframework.beans.factory.annotation.Autowired;
        import org.springframework.http.HttpRequest;
        import org.springframework.stereotype.Controller;
        import org.springframework.web.bind.annotation.ModelAttribute;
        import org.springframework.web.bind.annotation.RequestMapping;
        import org.springframework.web.bind.annotation.RequestMethod;
        import org.springframework.web.bind.annotation.RequestParam;
        import org.springframework.web.servlet.ModelAndView;

        import com.cts.Services.RegistrationService;
        import com.cts.entity.RegistrationDetails;

        @Controller
        public class RegistrationController 
        {

            @Autowired
            RegistrationService regService;

            UserIDGeneration uid=new UserIDGeneration();

        @RequestMapping(value ="/registration", method = RequestMethod.GET)
            public ModelAndView registrationSuccess(@ModelAttribute RegistrationDetails rd)
            {

                System.out.println("my value of id is:"+rd.getRegistrationId());
                System.out.println("My frst name is:"+rd.getFirstname());
                String userID=uid.nextSessionId();
                System.out.println("user id is"+userID);


                ModelAndView model=new ModelAndView("RegistrationSuccess");
                model.addObject("abc",rd.getFirstname());
                return model;
            }

        }

        this is my  Service class

        package com.cts.Services;

        import org.springframework.beans.factory.annotation.Autowired;
        import org.springframework.stereotype.Service;

        import com.cts.Dao.RegistrationDao;

        @Service
        public class RegistrationServiceImpl implements RegistrationService
        {
            @Autowired
            RegistrationDao regdao;

            @Override
            public int RegisterUser() 
            {
                return 0;

            }
        }

我无法注入bean会话工厂,registrationDAO,RegistrationService bean

DAO类

        package com.cts.Services;

        import org.springframework.beans.factory.annotation.Autowired;
        import org.springframework.stereotype.Service;

        import com.cts.Dao.RegistrationDao;

        @Service
        public class RegistrationServiceImpl implements RegistrationService
        {
            @Autowired
            RegistrationDao regdao;

            @Override
            public int RegisterUser() 
            {
                return 0;  
            }

        }

我遇到的错误

        org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'registrationController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.cts.Services.RegistrationService com.cts.Controller.RegistrationController.regService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'registrationServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.cts.Dao.RegistrationDao com.cts.Services.RegistrationServiceImpl.regdao; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'registrationDaoImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: org.h2.engine.SessionFactory com.cts.Dao.RegistrationDaoImpl.sessionFactory; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [org.h2.engine.SessionFactory] 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:287)
            at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1106)
            at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
            at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
            at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
            at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
            at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
            at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
            at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:609)
            at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)
            at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
            at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:647)
            at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:598)
            at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:661)
            at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:517)
            at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:458)
            at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:138)
            at javax.servlet.GenericServlet.init(GenericServlet.java:160)
            at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1280)
            at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1193)
            at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1088)
            at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5176)
            at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5460)
            at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
            at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
            at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
            at java.util.concurrent.FutureTask.run(FutureTask.java:262)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
            at java.lang.Thread.run(Thread.java:745)
        Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.cts.Services.RegistrationService com.cts.Controller.RegistrationController.regService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'registrationServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.cts.Dao.RegistrationDao com.cts.Services.RegistrationServiceImpl.regdao; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'registrationDaoImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: org.h2.engine.SessionFactory com.cts.Dao.RegistrationDaoImpl.sessionFactory; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [org.h2.engine.SessionFactory] 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:506)
            at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
            at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:284)
            ... 29 more
        Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'registrationServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.cts.Dao.RegistrationDao com.cts.Services.RegistrationServiceImpl.regdao; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'registrationDaoImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: org.h2.engine.SessionFactory com.cts.Dao.RegistrationDaoImpl.sessionFactory; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [org.h2.engine.SessionFactory] 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:287)
            at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1106)
            at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517)
            at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
            at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
            at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
            at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
            at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
            at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:876)
            at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:818)
            at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:735)
            at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:478)
            ... 31 more
        Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.cts.Dao.RegistrationDao com.cts.Services.RegistrationServiceImpl.regdao; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'registrationDaoImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: org.h2.engine.SessionFactory com.cts.Dao.RegistrationDaoImpl.sessionFactory; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [org.h2.engine.SessionFactory] 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:506)
            at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
            at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:284)
            ... 42 more
        Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'registrationDaoImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: org.h2.engine.SessionFactory com.cts.Dao.RegistrationDaoImpl.sessionFactory; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [org.h2.engine.SessionFactory] 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:287)

您没有包括RegistrationDaoImpl的代码,但是在深入研究了堆栈跟踪之后,您是否可能在RegistrationDaoImpl中导入了错误的SessionFactory类? 堆栈跟踪指示RegistrationDaoImpl需要一个org.h2.engine.SessionFactory,我希望在那里需要一个org.hibernate.SessionFactory。 尝试在RegistrationDaoImpl中修复对SessionFactory的导入,看看是否可以解决您的问题。

堆栈跟踪的相关部分:

创建名称为'registrationDaoImpl'的bean时出错:自动连接依赖项的注入失败; 嵌套的异常是org.springframework.beans.factory.BeanCreationException:无法自动连线字段:org.h2.engine.SessionFactory com.cts.Dao.RegistrationDaoImpl.sessionFactory; 嵌套的异常是org.springframework.beans.factory.NoSuchBeanDefinitionException: 未找到依赖项类型为[org.h2.engine.SessionFactory]的匹配bean :至少应有1个可以作为该依赖项的自动装配候选的bean。

更换

import org.h2.engine.SessionFactory;

这样

import org.hibernate.SessionFactory;

解决SessionFactory导入后,您似乎缺少了Aspectjweaver库。 你可以在这里得到

如果您正在使用Maven:

<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.8.7</version>
</dependency>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM