我需要一些有关 Spring AOP 的帮助。我有以下代码:
@Service
public class UserSecurityService implements UserDetailsService {
@Autowired
private UserService userService;
....
}
@Service
public class UserService extends CrudService<User, UserRepository> {
public UserService() {
super();
}
@Autowired
public UserService(UserRepository repository) {
super(repository);
this.repository = repository;
}
....
}
@Repository
interface UserRepository extends JpaRepository<User, String> {
...
}
应用程序上下文.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>
应用程序上下文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>
当我尝试在 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
我在 Web 层有相同的配置来记录我的应用程序,它工作正常,但是当我将 AOP 放在服务层时,我得到了这个异常。
我正在使用 Spring MVC,在 web.xml 中我配置为加载两个不同的上下文,一个仅加载 @Controller,另一个加载 @Repository 和 @Service。