BeanNotOfRequiredTypeException due to autowired fields

前端 未结 9 1225
长发绾君心
长发绾君心 2020-12-02 12:07

I\'m still new to Spring MVC and while building my test project, I got this message from Tomcat logs:

SEVERE: Exception sending context initialized event to          


        
相关标签:
9条回答
  • 2020-12-02 12:54

    I just experienced this issue when a test on an existing class started failing with 'expected to be of type...but was actually of type 'com.sun.proxy.$Proxy115'.

    What it turned out to be was that I had used Intellij's 'pull method up [to interface]' on an @Transactional method that I had added to the interface's implementation class, and Intellij decided to add @Transactional to the interface method declaration. If there is just one @Transactional method in an interface then it seems to screw things up, resulting in this error.

    0 讨论(0)
  • 2020-12-02 13:01

    Somewhere in your code you must be autowiring AdminServiceImpl like this:

    @Autowired
    private AdminServiceImpl adminService;
    

    Either depend barely on interface:

    @Autowired
    private AdminService adminService;
    

    or enabled CGLIB proxies.

    Similar problems

    • Autowired spring bean is not a proxy
    • Fixing BeanNotOfRequiredTypeException on Spring proxy cast on a non-singleton bean?
    • Getting Spring Error "Bean named 'x' must be of type [y], but was actually of type [$Proxy]" in Jenkins
    • Original interface is lost in Spring AOP introduction
    0 讨论(0)
  • 2020-12-02 13:03

    I found lots of questions similar to this one, when searching google for my problem. However, in my case I already used an interface. So I thought this might be helpful for others:

    This exception could also appear, if you have two beans with the same name!

    In my case I had additional bean configuration in my applicationContext.xml. The problem appeared after I merged two applications. The second one defined a @Resource and its member-variable name matched the bean name of the first application, mentioned above. Of course, the bean configuration of the first application didn't fit for the bean included via @Resource by the second application.

    0 讨论(0)
提交回复
热议问题