0

I have one "Hello word" application on spring AOP and configured by XML, it looks like this:

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();
    }
}

My spring configuration looks like this:

<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>

that doesn't work because of protected method, so I tried to use load time weaving with aop.xml like this:

<?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>

Source code for the aspect:

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("******");
    }
}

but it doesn't work, only if I change to annotation config. SOS PLZ

Nándor Előd Fekete
  • 6,988
  • 1
  • 22
  • 47
Saad Fch
  • 1
  • 4
  • Please add the source code for the aspect as well, and make sure your CustomerBoImpl class doesn't miss it's first line of the class declaration itself. – Nándor Előd Fekete Jan 11 '16 at 00:50
  • hi, I just add the code for aspect,   and add the first line of "CustomerBoImpl" cn you help me plz :) – Saad Fch Jan 11 '16 at 08:53

1 Answers1

0

removing the following code in your aop.xml, then run your jvm with args like this: -javaagent:"yourpath/spring-instrument-***.jar"

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