0

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.

Bibek Khadka
  • 180
  • 1
  • 17

3 Answers3

4

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.

t0mppa
  • 3,983
  • 5
  • 37
  • 48
  • The particular class mentioned in the error does not implement an interface, it just seems to be at the top when scanned by Spring. I tried to set `proxy-target-class` but maybe I am missing how to exactly do it. Do I just paste :` ` in my context.xml file at the top or should I keep any particular beans in place of__ . – Bibek Khadka Apr 02 '18 at 11:47
  • Ok, if it's not that clear cut, then you'll have to see if you can debug this. See [this answer](https://stackoverflow.com/a/5142042/136363) for details about what log entries to look out for. – t0mppa Apr 03 '18 at 04:25
  • I've been trying to solve this about 1 day! Google does nothing, I thought I'll never solve this, thank you, your answer should be one of the fundamental statements before using aspect :) – Frankie Drake Sep 26 '18 at 07:02
0

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> 
CInvt
  • 133
  • 8
0
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'.

0gam
  • 1,343
  • 1
  • 9
  • 21