无法自动装配字段:不能com.sun.proxy。$ Proxy22

时间:2014-12-18 06:20:57

标签: spring spring-mvc shiro

Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: protected com.cms.service.FolderService com.cms.action.BaseAction.folderService; nested exception is java.lang.IllegalArgumentException: Can not set com.cms.service.FolderService field com.cms.action.BaseAction.folderService to com.sun.proxy.$Proxy22
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:518)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:287)
    ... 30 more
Caused by: java.lang.IllegalArgumentException: Can not set com.cms.service.FolderService field com.cms.action.BaseAction.folderService to com.sun.proxy.$Proxy22
    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:514)
    ... 32 more

这是我的baseAction

public class BaseAction {

    @Autowired
    protected FolderService folderService;

    @Autowired
    protected ArticleService fileService;

    @Autowired
    protected TemplateService themeService;

    @Autowired
    protected HeadlineService headlineService;

    protected final Logger logger = Logger.getLogger(this.getClass());
}

那是我的FolderService

@Service
public class FolderService {

    protected final Logger logger = Logger.getLogger(this.getClass());

    @Autowired
    private FolderDao folderDao;

    @Autowired
    private AdminFolderDao adminFolderDao;

 ...................next is the java method

}

4 个答案:

答案 0 :(得分:8)

通常这意味着有一个AOP代理委托给你的bean。例如,如果您使用@Transaction注释了其中一个方法。

答案 1 :(得分:1)

当实现被代理后,您应该使用接口而不是实现来声明成员变量。参见spring autowiring fails with @Transactional

例如,代替:

    @Autowired
    protected FolderService folderService;

您应该使用:

    @Autowired
    protected IFolderService folderService;

其中IFolderServiceFolderService实现的接口。

答案 2 :(得分:0)

通过配置

使用cglib代理强制aop波动
spring.aop.proxy-target-class=true

帮助。

有关详细信息,请参阅this article

答案 3 :(得分:0)

将@Transaction从DAO对象移动到Service对象。