服务层的Spring AOP

Cel*_*ues 9 java proxy aop spring-mvc spring-aop

我需要一些Spring AOP的帮助.我有以下代码:


@Service
public class UserSecurityService implements UserDetailsService {

    @Autowired
    private UserService userService;
    ....
}
Run Code Online (Sandbox Code Playgroud)
@Service
public class UserService extends CrudService<User, UserRepository> {

    public UserService() {
        super();
    }

    @Autowired
    public UserService(UserRepository repository) {
        super(repository);
        this.repository = repository;
    }
    ....
}
Run Code Online (Sandbox Code Playgroud)
@Repository
interface UserRepository extends JpaRepository<User, String> {
     ...
}
Run Code Online (Sandbox Code Playgroud)

应用程序的context.xml

<import resource="classpath*:spring/application-context-db.xml" />
<import resource="classpath*:spring/application-context-aop.xml" />
<import resource="classpath*:spring/application-context-mail.xml" />
<import resource="application-context-security.xml" />

<context:component-scan base-package="com.xpto">
    <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
    <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository" />
</context:component-scan>
Run Code Online (Sandbox Code Playgroud)

应用程序上下文aop.xml文件

<aop:aspectj-autoproxy />
<aop:config>
    <aop:aspect id="serviceLoggingAspect" ref="serviceLoggingAspectBean">
        <aop:pointcut id="servicePointcut"
                expression="@within(org.springframework.stereotype.Service)" />

        <aop:before method="before" pointcut-ref="servicePointcut" />
        <aop:after-returning method="afterReturning" pointcut-ref="servicePointcut" returning="result" />
        <aop:after-throwing method="afterThrowing" pointcut-ref="servicePointcut" throwing="exception" />
    </aop:aspect>
</aop:config>
Run Code Online (Sandbox Code Playgroud)

当我尝试在Tomcat上加载我的应用程序时,我得到以下异常:

Caused by: java.lang.IllegalArgumentException: Can not set com.xpto.user.service.UserService field com.xpto.user.security.service.UserSecurityService.userService to com.sun.proxy.$Proxy57
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:164)
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:168)
at sun.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:81)
at java.lang.reflect.Field.set(Field.java:680)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:510)
... 35 more
Run Code Online (Sandbox Code Playgroud)

我在Web层上使用相同的配置来记录我的应用程序并且它工作正常,但是当我将AOP放在服务层时,我得到了这个异常.

我正在使用Spring MVC,在web.xml中,我配置为加载两个不同的上下文,一个只加载@Controller,另一个加载@Repository和@Service.

sam*_*wis 16

您没有注入接口,因此您需要使用CGLIB代理,弹簧参考手册指出:

Spring AOP默认使用AOP代理的标准J2SE动态代理.这使得任何接口(或接口集)都可以被代理.

Spring AOP也可以使用CGLIB代理.这是代理类而不是接口所必需的.如果业务对象未实现接口,则默认使用CGLIB.由于编程接口而不是类是一种好的做法,业务类通常会实现一个或多个业务接口.

Spring决定使用J2SE代理(com.sun.proxy.$Proxy57)可能是因为CrudService实现了一个接口.要强制使用CGLIB,您可以调整XML:

<aop:aspectj-autoproxy proxy-target-class="true"/>
Run Code Online (Sandbox Code Playgroud)