简体   繁体   中英

AutoWiring Exceptions. Cannot Autowire Field

I am doing insert into database using spring MVC, Hibernate but I am getting Autowiring Errors.

Getting dis error-java.lang.ClassNotFoundException: org.aspectj.util.PartialOrder$PartialComparable

This is my 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>

Controller

        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;

            }
        }

I'm not able to inject bean session factory, registrationDAO,RegistrationService bean

DAO 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;  
            }

        }

Errors which I'am getting

        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)

You didn't include the code for RegistrationDaoImpl, but after digging into your stack trace, did you possibly import the wrong SessionFactory class in RegistrationDaoImpl? The stack trace indicates that RegistrationDaoImpl needs a org.h2.engine.SessionFactory, where I would expect that it would want an org.hibernate.SessionFactory. Try fixing your import of SessionFactory in RegistrationDaoImpl and see if that fixes your issue.

Relevant part of stack trace:

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.

Replace

import org.h2.engine.SessionFactory;

By this

import org.hibernate.SessionFactory;

After resolving SessionFactory import, You seem to be missing the aspectjweaver library. You can get it here

if you are using maven:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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