简体   繁体   中英

BeanNotOfRequiredTypeException upon enabling Spring AOP

I had my Spring MVC project running with Apache Shiro for web and api security. Life was good, until one day I had to do permission checks for authorization.

Using @RequiresPermissions annotation required me to enable Spring AOP with following code:

 <bean id="annotationProxy" class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" depends-on="lifecycleBeanPostProcessor"/>
<bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
    <property name="securityManager" ref="securityManager"/>
</bean> 

Now I get error when I run the project as follows:

[http-nio-8084-exec-66] WARN org.springframework.web.context.support.XmlWebApplicationContext - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'myController': Unsatisfied dependency expressed through field 'myService'; nested exception is org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'myService' is expected to be of type [com.service.myService] but was actually of type [com.sun.proxy.$Proxy594] [http-nio-8084-exec-66] ERROR org.springframework.web.context.ContextLoader - Context initialization failed org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'myController': Unsatisfied dependency expressed through field 'myService'; nested exception is org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'myService' is expected to be of type [com.service.myService] but was actually of type [com.sun.proxy.$Proxy594]

Please help.

Your question doesn't give that many clues to the actual scenario. But having just fought with a similar error myself last week while helping to debug a colleague's mystery error, here's a summary of what I learned.

When AOP is enabled, if your class implements an interface, Spring by default will create a proxy for the interface, not the actual class.

So, let's say your service is of type:

@Service
public class UserService implements Service {
  // ...
}

Then the proxy would be created for the interface Service and if you try to inject it into a variable or parameter that is of the type UserService , like for instance:

public class MyServiceFacade {
  @Autowired
  private UserService userService;

  // ...
}

this becomes a problem, as the proxy is of the wrong type. The error message is quite confusing, because it doesn't explicitly tell you what kind of a proxy has been created and why it resulted into a type mismatch.

To fix this, you can either use the interface in calling code or to change AOP configuration into using target class for proxies, which can be done by setting the proxy-target-class attribute as true .

See more from Spring instructions on Proxying mechanisms .

Add @EnableAspectJAutoProxy to your @Configuration class, or enable it in your XML configuration file.

Optionally, add dependency for aspectjweaver

<dependency>
  <groupId>org.aspectj</groupId>
  <artifactId>aspectjweaver</artifactId>
  <version>...</version>
</dependency> 
public class UnsatisfiedDependencyException extends BeanCreationException

Exception thrown when a bean depends on other beans or simple properties that were not specified in the bean factory definition, although dependency checking was enabled.

first, you need to check your 'myService'.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM