自动接线例外。不能Autowire Field

时间:2015-09-24 16:50:21

标签: java spring

我正在使用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)

2 个答案:

答案 0 :(得分:0)

您没有包含RegistrationDaoImpl的代码,但在深入了解堆栈跟踪后,您是否可能在RegistrationDaoImpl中导入错误的SessionFactory类?堆栈跟踪表明RegistrationDaoImpl需要一个org.h2.engine.SessionFactory,我希望它需要一个org.hibernate.SessionFactory。尝试在RegistrationDaoImpl中修复SessionFactory的导入,看看是否能解决您的问题。

堆栈跟踪的相关部分:

  

创建名为'registrationDaoImpl'的bean时出错:注入   自动连接依赖失败;嵌套异常是   org.springframework.beans.factory.BeanCreationException:不能   autowire字段:org.h2.engine.SessionFactory   com.cts.Dao.RegistrationDaoImpl.sessionFactory;嵌套异常是   org.springframework.beans.factory.NoSuchBeanDefinitionException:不   匹配找到的类型为[org.h2.engine.SessionFactory]的bean   依赖:预计至少有1个bean可以作为autowire   这种依赖的候选人。

答案 1 :(得分:0)

替换

import org.h2.engine.SessionFactory;

通过这个

import org.hibernate.SessionFactory;

解析SessionFactory导入后,您似乎缺少aspectjweaver库。 你可以得到它here

如果你正在使用maven:

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