LogBean.java
package com.brj.spring.aop.arroundadvice;
import org.aspectj.lang.ProceedingJoinPoint;
public class LogBean {
public Object arroundLog(ProceedingJoinPoint joinPoint) throws Throwable{
System.out.println("before..........");
Object obj = joinPoint.proceed();
System.out.println("after...........");
return null;
}
}
TargetBean.java
package com.brj.spring.aop.arroundadvice;
public class TargetBean {
public void printInformation(){
System.out.println("targetBean is worked...");
}
}
Test.java
package com.brj.spring.aop.arroundadvice;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
TargetBean proxyT = (TargetBean) ac.getBean("targetBean");
proxyT.printInformation();
}
}
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<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-2.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
<bean id="targetBean"
class="com.brj.spring.aop.arroundadvice.TargetBean">
</bean>
<bean id="logBean"
class="com.brj.spring.aop.arroundadvice.LogBean">
</bean>
<aop:config>
<aop:pointcut id="logPoint"
expression="execution(public * *(..)) and ! execution(* com.brj.spring.aop.arroundadvice.LogBean.*(..))" />
<aop:aspect id="logAspect" ref="logBean">
<aop:around pointcut-ref="logPoint" method="arroundLog" />
</aop:aspect>
</aop:config>
</beans>