【问题标题】:How can i configure my spring aop xml with aop.xml an load time weaving?如何使用 aop.xml 配置我的 spring aop xml 加载时间编织?
【发布时间】:2019-07-11 12:39:46
【问题描述】:

我在 Spring AOP 上有一个“Hello word”应用程序,由 XML 配置,它看起来像这样:

public class CustomerBoImpl {

    public CustomerBoImpl() {
        super();
    }

    protected void addCustomer(){
        System.out.println("addCustomer() is running ");
    }
}


public class App {
    public static void main(String[] args) throws Exception {
        ApplicationContext appContext =
            new ClassPathXmlApplicationContext("Spring-Customer.xml");

        CustomerBoImpl customer = 
            (CustomerBoImpl) appContext.getBean("customerBo");

        customer.addCustomer();
    }
}

我的spring配置如下:

<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"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">

<aop:aspectj-autoproxy />
<!-- this switches on the load-time weaving -->
<context:load-time-weaver aspectj-weaving="on" />

<bean id="customerBo" class="com.mkyong.saad.CustomerBoImpl"
    scope="singleton" />

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

<aop:config>

    <aop:aspect id="aspectLoggging" ref="logAspect">

        <!-- @Before -->
        <aop:pointcut id="pointCutBefore"
            expression="execution(* com.mkyong.saad.CustomerBoImpl.addCustomer(..))" />

        <aop:before method="logBefore" pointcut-ref="pointCutBefore" /> 

        <!-- @After -->
        <aop:pointcut id="pointCutAfter"
            expression="execution(* com.mkyong.saad.CustomerBoImpl.addCustomer(..))" />
        <aop:after method="logAfter" pointcut-ref="pointCutAfter" />


    </aop:aspect>

</aop:config>

由于受保护的方法,这不起作用,所以我尝试像这样将加载时间编织与 aop.xml 一起使用:

<?xml version="1.0" encoding="UTF-8"?>
<aspectj>
    <weaver>
        <!-- only weave classes in our application-specific packages -->
        <include within="com.mkyong.saad.*"/>
    </weaver>

    <aspects>
        <!-- weave in just this aspect -->
        <aspect name="com.mkyong.saad.LoggingAspect"/>
    </aspects>

</aspectj>

方面的源代码:

public class LoggingAspect {

    public void logBefore(JoinPoint joinPoint) {
        System.out.println("logBefore() is running!");
        System.out.println("hijacked : " + joinPoint.getSignature().getName());
        System.out.println("******");
    }

    public void logAfter(JoinPoint joinPoint) {
        System.out.println("logAfter() is running!");
        System.out.println("hijacked : " + joinPoint.getSignature().getName());
        System.out.println("******");
    }
}

但它不起作用,只有当我更改为注释配置时。求救请

【问题讨论】:

  • 请同时添加方面的源代码,并确保您的 CustomerBoImpl 类不会错过它本身的类声明的第一行。
  • 你好,我只是添加了方面的代码,并添加了“CustomerBoImpl”cn的第一行你帮帮我:)

标签: spring aop aspectj load-time-weaving


【解决方案1】:

删除 aop.xml 中的以下代码,然后使用以下参数运行 jvm:-javaagent:"yourpath/spring-instrument-***.jar"

<weaver>
    <!-- only weave classes in our application-specific packages -->
    <include within="com.mkyong.saad.*"/>
</weaver>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-26
    • 1970-01-01
    • 2019-05-10
    • 2022-01-02
    • 2012-11-05
    相关资源
    最近更新 更多