Spring AOP at Service Layer

后端 未结 2 665
滥情空心
滥情空心 2020-12-29 13:07

I need some help with Spring AOP. I\'ve the following code:


@Service
public class UserSecurityService implements UserDetailsService {

    @Autowired
          


        
相关标签:
2条回答
  • 2020-12-29 13:42

    You are not injecting an interface so you need to use CGLIB proxies, the spring reference manual states:

    Spring AOP defaults to using standard J2SE dynamic proxies for AOP proxies. This enables any interface (or set of interfaces) to be proxied.

    Spring AOP can also use CGLIB proxies. This is necessary to proxy classes, rather than interfaces. CGLIB is used by default if a business object does not implement an interface. As it is good practice to program to interfaces rather than classes, business classes normally will implement one or more business interfaces.

    Spring has decided to use a J2SE proxy (com.sun.proxy.$Proxy57) probably because CrudService implements an interface. To force the use of CGLIB you can tweak your XML:

    <aop:aspectj-autoproxy proxy-target-class="true"/>
    
    0 讨论(0)
  • 2020-12-29 13:49

    Spring has decided to use a J2SE proxy (com.sun.proxy.$Proxy57) probably because CrudService implements an interface.

    @samlewis: This sentence that you wrote pushed me to create interfaces to my Services and when I did that, LoggingAspect worked really fine. So, I'm not using proxy-target-class=true.

    Thanks a lot for your time.

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