0

Spring AOP に「Hello word」アプリケーションが 1 つあり、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();
    }
}

私の春の設定は次のようになります。

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

しかし、アノテーション構成に変更した場合にのみ機能しません。SOS PLZ

4

1 に答える 1