5
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

this is my 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());
}

and that's my 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

}
Jens
  • 67,715
  • 15
  • 98
  • 113
lynn_xly
  • 95
  • 1
  • 2
  • 12
  • since this has to do with proxy could you post your complete configuration and mention if you do any transaction management, AOP etc ? – Bond - Java Bond Dec 18 '14 at 06:33
  • I extract interface for the service, and is ok, thank you very much – lynn_xly Dec 18 '14 at 06:39
  • 2
    Post whatever you did to fix your problem as an answer and mark it as resolved or delete the question altogether. – kryger Dec 18 '14 at 23:10
  • How was the problem solved? Do post an answer. – Sajib Acharya Apr 11 '16 at 18:26
  • I suppose you finally found your way out of this problem, but the solution is given here: http://stackoverflow.com/questions/36556358/java-lang-illegalargumentexception-cannot-set-to-com-sun-proxy-proxy/36556498#comment60713609_36556498 – Eusebio Jun 13 '16 at 08:28

4 Answers4

8

Usually this means there is an AOP proxy delegating to your bean. For example, if you have annotated one of your methods with @Transaction.

ocarlsen
  • 1,312
  • 2
  • 16
  • 20
  • 3
    if class need @Transaction annotation, then how to do? – sendreams Feb 25 '16 at 02:50
  • @sendreams Please use interfaces instead of implementations. See https://stackoverflow.com/questions/32579118/spring-autowiring-fails-with-transactional – johnlinp Jun 22 '20 at 06:33
2

Forcing aop to wave by using cglib proxies by configuring

spring.aop.proxy-target-class=true

helps.

Refer this article for more information

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
  • 1
    Keep in mind that links can get broken. It's nice that you annotated it briefly; consider adding more details to your post directly. – YakovL Oct 18 '16 at 18:47
1

When the implementation is proxied, you should declare the member variables with interfaces instead of implementations. See spring autowiring fails with @Transactional.

For example, instead of:

    @Autowired
    protected FolderService folderService;

You should use:

    @Autowired
    protected IFolderService folderService;

where IFolderService is the interface that FolderService implements.

johnlinp
  • 853
  • 6
  • 22
0

Move @Transaction from your DAO object to the Service object.

Gene
  • 10,819
  • 1
  • 66
  • 58