Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 服务层的Spring AOP_Java_Spring Mvc_Proxy_Aop_Spring Aop - Fatal编程技术网

Java 服务层的Spring AOP

Java 服务层的Spring AOP,java,spring-mvc,proxy,aop,spring-aop,Java,Spring Mvc,Proxy,Aop,Spring Aop,我需要一些关于Spring AOP的帮助。 我有以下代码: 我在Web层使用相同的配置来记录我的应用程序,它工作得很好,但当我在服务层使用AOP时,我得到了这个异常 我使用的是Spring MVC,在web.xml上,我配置为加载两个不同的上下文,一个只加载@Controller,另一个加载@Repository和@Service。您没有注入接口,因此需要使用CGLIB代理,状态为: SpringAOP默认使用标准J2SE动态代理作为AOP代理。这允许代理任何接口(或接口集) Spring

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




我在Web层使用相同的配置来记录我的应用程序,它工作得很好,但当我在服务层使用AOP时,我得到了这个异常


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

您没有注入接口,因此需要使用CGLIB代理,状态为:

SpringAOP默认使用标准J2SE动态代理作为AOP代理。这允许代理任何接口(或接口集)

SpringAOP也可以使用CGLIB代理。这对于代理类而不是接口是必需的。如果业务对象未实现接口,则默认情况下使用CGLIB。由于编程到接口而不是类是一种很好的实践,所以业务类通常会实现一个或多个业务接口

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

<aop:aspectj-autoproxy proxy-target-class="true"/>

Spring决定使用J2SE代理(com.sun.proxy.$Proxy57),可能是因为CrudService实现了一个接口

@samlewis:你写的这句话促使我为我的服务创建接口,当我这样做时,LoggingAspect工作得非常好。因此,我没有使用代理目标class=true


非常感谢您抽出时间。

嗨@samlewis,谢谢您抽出时间。实际上我以前读过,但当我放置“proxy target class”时,我得到了另一个异常:由以下原因引起:org.springframework.aop.framework.aopconfigeException:无法生成类[class com.sun.proxy.$Proxy54]的CGLIB子类:此问题的常见原因包括使用最终类或不可见类;嵌套异常为java.lang.IllegalArgumentException:无法为final类com.sun.proxy.$Proxy54创建子类,但它们都未标记为final或non-visible。我将存储库更改为公共存储库。我不知道发生了什么事。谢谢。(*我不知道)当我使用默认访问修饰符离开存储库时,我得到了这个异常:原因:org.springframework.aop.framework.aopconfigeException:无法生成类[class com.xpto.user.service.$Proxy54]的CGLIB子类:这个问题的常见原因包括使用最终类或不可见类;嵌套的异常是java.lang.IllegalArgumentException:无法对最终类com.xpto.user.service进行子类化。$Proxy54我认为发生该异常是因为UserRepository接口的默认访问。谢谢
@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> {
     ...
}
<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: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>
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
<aop:aspectj-autoproxy proxy-target-class="true"/>