java - 编织一个没有接口(interface)和非公共(public)方法的类时关于理解Spring AOP的问题

标签 java spring aop spring-aop

我正在学习 Spring AOP,我创建了一个简单的项目以了解它的工作原理。请在下面找到我项目的主要部分:

我的 Spring-Customer.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
    http://www.springframework.org/schema/aop 
    http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">

    <aop:aspectj-autoproxy />



    <bean id="customerBoNoIterface" class="com.core.CustomerBoNoIterface" />


    <!-- Aspect -->
    <bean id="logAspect" class="com.aspect.LoggingAspect" />

</beans>

请在下面找到我的 AOP 切入点:

@Before("execution(* com.core.CustomerBoNoIterface.addCustmer*(..))")
    public void logBeforeDummy(JoinPoint joinPoint) {

        System.out.println("hijacked : " + joinPoint.getSignature().getName());
        System.out.println("******");
    }

请在下面找到我的类 CustomerBoNoIterface:

   public class CustomerBoNoIterface {

    private void addCustmerPrivate() {
        System.out.println("calling addCustmerPrivate()");
    }

    public void addCustmerPublic() {

        System.out.println("calling addCustmerPublic()");

    }

    protected void addCustmerProtected() {

        System.out.println("calling addCustmerProtected()");

    }

    void addCustmerDefault() {

        System.out.println("calling addCustmerDefault()");

    }



    public String addCustmerReturnString() {

        System.out.println("calling addCustmerPublic return string");
        return "";

    }

}

最后是我的 Main 方法:

public class App {
    public static void main(String[] args) throws Exception {

        ApplicationContext appContext = new ClassPathXmlApplicationContext("Spring-Customer.xml");

        CustomerBoNoIterface customerBoNoIterfaceBean =appContext.getBean("customerBoNoIterface", CustomerBoNoIterface.class);
        customerBoNoIterfaceBean.addCustmerPublic();
        customerBoNoIterfaceBean.addCustmerDefault();
        customerBoNoIterfaceBean.addCustmerProtected();
        customerBoNoIterfaceBean.addCustmerReturnString();



    }

根据在线引用,Spring AOP 是通过代理完成的,类需要实现一个接口(interface),以便 Spring 能够编织切面,而且 Spring 只会将切面编织到公共(public)方法。

但是在我的例子中,我有一个没有接口(interface)的类,如上例所示,我可以在以下方法中应用 AOP:

            customerBoNoIterfaceBean.addCustmerPublic();
        customerBoNoIterfaceBean.addCustmerDefault();
        customerBoNoIterfaceBean.addCustmerProtected();
        customerBoNoIterfaceBean.addCustmerReturnString();

因此,当我运行 Main 方法时,我在 eclipse 的控制台中得到以下输出:

hijacked : addCustmerPublic
******
calling addCustmerPublic()
hijacked : addCustmerDefault
******
calling addCustmerDefault()
hijacked : addCustmerProtected
******
calling addCustmerProtected()
hijacked : addCustmerReturnString
******
calling addCustmerPublic return string

所以我无法理解 Spring 如何能够在 public 以外的方法和没有接口(interface)的类中编织切面?

提前致谢

最佳答案

在编写方法时,Spring AOP 通过使用反射API 处理每个被注解的类,不会直接调用您的类的方法。因此,您建议的方法的可见性在这里并不重要。

Spring AOP提供了两种代理 1)JDK 代理:如果你的类至少实现了一个接口(interface),这个代理就会出现。

2)CGLIB 代理:当你的类没有实现任何接口(interface)时,这个代理就会出现。

在您的情况下,您的类没有实现任何接口(interface),因此您的类将使用 CGLIB 代理进行代理。

关于java - 编织一个没有接口(interface)和非公共(public)方法的类时关于理解Spring AOP的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20296883/

相关文章:

java - gwt 中的 session 管理

java - 在 OSGI 中嵌入传递依赖

java - android如何在 Canvas 中设置自定义字体?

java - Spring 3.1 + Hibernate 4.1 Propagation.Supports 问题

c# - 通过 IoC 影响带有属性的 AOP;代码味还是优雅?

java - 从 InputStream 读取数据包 - 正确的设计

java - 在 Spring Boot 中更改了 Rest Service 输入参数

java - 为什么 WebMvcConfigurer 重写方法不起作用?

java - Java包结构中的模块与层

actionscript-3 - Actionscript 3 的面向方面的编程库/框架?