java. Lang. illegalargumentexception “Dao fields cannot be set using spring 4 and Hibernate 4”
I'm trying to follow the tutorial (Using STS):
Everything goes well except execution Here's what I got during deployment:
<pre> Jan 17,2015 6:39:22 PM org.apache.catalina.core.ApplicationContext log SEVERE: StandardWrapper.Throwable org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'homeController': Injection of autowired dependencies Failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.example.web.dao.ActivityDAO com.example.web.HomeController.activityDAO; nested exception is java.lang.IllegalArgumentException: Can not set com.example.web.dao.ActivityDAO field com.example.web.HomeController.activityDAO to com.sun.proxy.$Proxy32 at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:292) at org.springframework.beans.factory.support.AbstractAutowireCapablebeanfactory.populateBean(AbstractAutowireCapablebeanfactory.java:1185) at org.springframework.beans.factory.support.AbstractAutowireCapablebeanfactory.doCreateBean(AbstractAutowireCapablebeanfactory.java:537) at org.springframework.beans.factory.support.AbstractAutowireCapablebeanfactory.createBean(AbstractAutowireCapablebeanfactory.java:475) at org.springframework.beans.factory.support.Abstractbeanfactory$1.getObject(Abstractbeanfactory.java:304) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228) at org.springframework.beans.factory.support.Abstractbeanfactory.doGetBean(Abstractbeanfactory.java:300) at org.springframework.beans.factory.support.Abstractbeanfactory.getBean(Abstractbeanfactory.java:195) at org.springframework.beans.factory.support.DefaultListablebeanfactory.preInstantiateSingletons(DefaultListablebeanfactory.java:703) at org.springframework.context.support.AbstractApplicationContext.finishbeanfactoryInitialization(AbstractApplicationContext.java:760) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482) at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:658) at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:624) at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:672) at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:543) at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:484) 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:1241) at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1154) at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1041) at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4944) at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5230) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) at com.springsource.tcserver.serviceability.deploy.TcContainerDeployer.startApplication(TcContainerDeployer.java:371) at com.springsource.tcserver.serviceability.deploy.TcContainerDeployer.startApplication(TcContainerDeployer.java:351) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(UnkNown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(UnkNown Source) at java.lang.reflect.Method.invoke(UnkNown Source) at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:300) at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(UnkNown Source) at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(UnkNown Source) at com.sun.jmx.remote.security.MBeanServerAccessController.invoke(UnkNown Source) at javax.management.remote.rmi.RMIConnectionImpl.doOperation(UnkNown Source) at javax.management.remote.rmi.RMIConnectionImpl.access$300(UnkNown Source) at javax.management.remote.rmi.RMIConnectionImpl$PrivilegedOperation.run(UnkNown Source) at java.security.AccessController.doPrivileged(Native Method) at javax.management.remote.rmi.RMIConnectionImpl.doPrivilegedOperation(UnkNown Source) at javax.management.remote.rmi.RMIConnectionImpl.invoke(UnkNown Source) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(UnkNown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(UnkNown Source) at java.lang.reflect.Method.invoke(UnkNown Source) at sun.rmi.server.UnicastServerRef.dispatch(UnkNown Source) at sun.rmi.transport.Transport$1.run(UnkNown Source) at sun.rmi.transport.Transport$1.run(UnkNown Source) at java.security.AccessController.doPrivileged(Native Method) at sun.rmi.transport.Transport.serviceCall(UnkNown Source) at sun.rmi.transport.tcp.TCPTransport.handleMessages(UnkNown Source) at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(UnkNown Source) at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(UnkNown Source) at java.util.concurrent.ThreadPoolExecutor.runWorker(UnkNown Source) at java.util.concurrent.ThreadPoolExecutor$Worker.run(UnkNown Source) at java.lang.Thread.run(UnkNown Source) Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.example.web.dao.ActivityDAO com.example.web.HomeController.activityDAO; nested exception is java.lang.IllegalArgumentException: Can not set com.example.web.dao.ActivityDAO field com.example.web.HomeController.activityDAO to com.sun.proxy.$Proxy32 at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:508) at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:289) ... 54 more Caused by: java.lang.IllegalArgumentException: Can not set com.example.web.dao.ActivityDAO field com.example.web.HomeController.activityDAO to com.sun.proxy.$Proxy32 at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnkNown Source) at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnkNown Source) at sun.reflect.UnsafeObjectFieldAccessorImpl.set(UnkNown Source) at java.lang.reflect.Field.set(UnkNown Source) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:504) ... 56 more Jan 17,2015 6:39:22 PM org.apache.catalina.core.StandardContext loadOnStartup SEVERE: Servlet /web threw load() exception java.lang.IllegalArgumentException: Can not set com.example.web.dao.ActivityDAO field com.example.web.HomeController.activityDAO to com.sun.proxy.$Proxy32 at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnkNown Source) at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnkNown Source) at sun.reflect.UnsafeObjectFieldAccessorImpl.set(UnkNown Source) at java.lang.reflect.Field.set(UnkNown Source) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:504) at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:289) at org.springframework.beans.factory.support.AbstractAutowireCapablebeanfactory.populateBean(AbstractAutowireCapablebeanfactory.java:1185) at org.springframework.beans.factory.support.AbstractAutowireCapablebeanfactory.doCreateBean(AbstractAutowireCapablebeanfactory.java:537) at org.springframework.beans.factory.support.AbstractAutowireCapablebeanfactory.createBean(AbstractAutowireCapablebeanfactory.java:475) at org.springframework.beans.factory.support.Abstractbeanfactory$1.getObject(Abstractbeanfactory.java:304) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228) at org.springframework.beans.factory.support.Abstractbeanfactory.doGetBean(Abstractbeanfactory.java:300) at org.springframework.beans.factory.support.Abstractbeanfactory.getBean(Abstractbeanfactory.java:195) at org.springframework.beans.factory.support.DefaultListablebeanfactory.preInstantiateSingletons(DefaultListablebeanfactory.java:703) at org.springframework.context.support.AbstractApplicationContext.finishbeanfactoryInitialization(AbstractApplicationContext.java:760) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482) at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:658) at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:624) at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:672) at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:543) at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:484) 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:1241) at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1154) at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1041) at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4944) at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5230) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) at com.springsource.tcserver.serviceability.deploy.TcContainerDeployer.startApplication(TcContainerDeployer.java:371) at com.springsource.tcserver.serviceability.deploy.TcContainerDeployer.startApplication(TcContainerDeployer.java:351) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(UnkNown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(UnkNown Source) at java.lang.reflect.Method.invoke(UnkNown Source) at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:300) at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(UnkNown Source) at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(UnkNown Source) at com.sun.jmx.remote.security.MBeanServerAccessController.invoke(UnkNown Source) at javax.management.remote.rmi.RMIConnectionImpl.doOperation(UnkNown Source) at javax.management.remote.rmi.RMIConnectionImpl.access$300(UnkNown Source) at javax.management.remote.rmi.RMIConnectionImpl$PrivilegedOperation.run(UnkNown Source) at java.security.AccessController.doPrivileged(Native Method) at javax.management.remote.rmi.RMIConnectionImpl.doPrivilegedOperation(UnkNown Source) at javax.management.remote.rmi.RMIConnectionImpl.invoke(UnkNown Source) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(UnkNown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(UnkNown Source) at java.lang.reflect.Method.invoke(UnkNown Source) at sun.rmi.server.UnicastServerRef.dispatch(UnkNown Source) at sun.rmi.transport.Transport$1.run(UnkNown Source) at sun.rmi.transport.Transport$1.run(UnkNown Source) at java.security.AccessController.doPrivileged(Native Method) at sun.rmi.transport.Transport.serviceCall(UnkNown Source) at sun.rmi.transport.tcp.TCPTransport.handleMessages(UnkNown Source) at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(UnkNown Source) at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(UnkNown Source) at java.util.concurrent.ThreadPoolExecutor.runWorker(UnkNown Source) at java.util.concurrent.ThreadPoolExecutor$Worker.run(UnkNown Source) at java.lang.Thread.run(UnkNown Source) </pre>
Active tables in the database created using this script:
CREATE TABLE IF NOT EXISTS `Example`.`Activity` ( `id` INT NOT NULL AUTO_INCREMENT,`Name` VARCHAR(32) NOT NULL,PRIMARY KEY (`id`),UNIQUE INDEX `id_UNIQUE` (`id` ASC)) ENGINE = InnoDB
This is the model class:
package com.example.web.model; public class Activity { private int id; private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
This is my servlet context xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="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/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure --> <!-- Enables the Spring MVC @Controller programming model --> <mvc:annotation-driven /> <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory --> <mvc:resources mapping="/resources/**" location="/resources/" /> <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/" /> <property name="suffix" value=".jsp" /> </bean> <context:component-scan base-package="com.example.web" /> <context:component-scan base-package="com.example.web.dao"/> <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="com.MysqL.jdbc.Driver"/> <property name="url" value="jdbc:MysqL://localhost:3306/Example"/> <property name="username" value="example"/> <property name="password" value="eX$@mp1E"/> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionfactorybean"> <property name="dataSource" ref="dataSource" /> <property name="configLocation" value="classpath:hibernate.cfg.xml" /> </bean> <tx:annotation-driven /> <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <bean id="ActivityDao" class="com.example.web.dao.ActivityDAO"> <constructor-arg> <ref bean="sessionFactory" /> </constructor-arg> </bean> </beans>
This is activitydao:
package com.example.web.dao; import java.util.List; import com.example.web.model.Activity; import org.hibernate.Criteria; import org.hibernate.SessionFactory; import org.springframework.transaction.annotation.Transactional; public class ActivityDAO implements IActivityDAO { private SessionFactory sessionFactory; public ActivityDAO(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } @Override @Transactional public List<Activity> All() { @SuppressWarnings("unchecked") List<Activity> listActivity = (List<Activity>) sessionFactory.getCurrentSession() .createCriteria(Activity.class) .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY).list(); return listActivity; } }
I've read some posts about the incorrect translation of fields, but it doesn't help me Here I tried:
Could not autowire field: Spring-Hibernate
Could not autowire field
Solution
You are using the spring JDK proxy method Your homecontroller has a field like activitydao, such as:
@Autowired private ActivityDAO activityDAO;
However, spring cannot create a class proxy using the JDK proxy method
You can change the type of the activitydao field to iactivitydao:
@Autowired private IActivityDAO activityDAO;
This will solve the problem
Read more about proxy methods: http://docs.spring.io/spring/docs/current/spring-framework-reference/html/aop-api.html#aop -pfb-proxy-types