解决 org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.te
在做springmvc项目开发中出现如下错误
org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:497) Context initialization failed org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.test.service.UserService org.test.controller.UserController.userService; nested exception is java.lang.IllegalArgumentException: Can not set org.test.service.UserService field org.test.controller.UserController.userService to com.sun.proxy.$Proxy17 at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1204) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:538) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:229) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:762) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:757) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480) at org.springframework.web.servlet.FrameworkServlet.configureAndRefreshWebApplicationContext(FrameworkServlet.java:663) at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:629) at org.springframework.web.servlet.FrameworkServlet.createWebApplicationContext(FrameworkServlet.java:677) at org.springframework.web.servlet.FrameworkServlet.initWebApplicationContext(FrameworkServlet.java:548) at org.springframework.web.servlet.FrameworkServlet.initServletBean(FrameworkServlet.java:489) at org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:136) 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$Sync.innerRun(FutureTask.java:303) at java.util.concurrent.FutureTask.run(FutureTask.java:138) at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918) at java.lang.Thread.run(Thread.java:662) Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.test.service.UserService org.test.controller.UserController.userService; nested exception is java.lang.IllegalArgumentException: Can not set org.test.service.UserService field org.test.controller.UserController.userService to com.sun.proxy.$Proxy17 at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:555) at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331) ... 30 more Caused by: java.lang.IllegalArgumentException: Can not set org.test.service.UserService field org.test.controller.UserController.userService to com.sun.proxy.$Proxy17 at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:146) at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:150) at sun.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:63) at java.lang.reflect.Field.set(Field.java:657) at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:551) ... 32 more
原因是service类不是指定的接口类,需要使用接口类才可以。
UserService需要定义成接口类如下
public interface UserService { delete(User entity); }
定义实现类
package org.test.service.impl; import java.util.List; import javax.annotation.Resource; import org.test.bean.User; import org.test.common.BaseMethod; import org.test.mybatis.mapper.UserMapper; import org.springframework.stereotype.Service; @Service public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Override public void delete(User entity) throws Exception { userMapper.delete(entity); } /** * @param userMapper the userMapper to set */ public void setUserMapper(UserMapper userMapper) { this.userMapper = userMapper; } } 在controller类中调用的写法
@Controller public class UserController{ @Autowired private UserService userService; @RequestMapping(value="/hello") public String home() throws Exception{ User user =new User(); user.setId(1L); userServie.delete(user); return "/hello"; } }
来源://作者:/更新时间:2014-12-03
顶
踩
相关文章:
- 【解决】Caused by: org.apache.tiles.definition.Def
- springmvc controller跳转到下一个controller,action
- 解决java.util.concurrent.ExecutionException: org.a
- 解决com.fasterxml.jackson.databind.JsonMappingExce
- springmvc controller返回字符串乱码
- junit java.lang.NoClassDefFoundError: net/minidev/
- junit java.lang.NoClassDefFoundError: com/jayway/j
- springmvc项目进行junit测试方法
- springmvc 文件上传报错org.springframework.web.util
- springmvc 在controller类中如何读取.properties变量