schema的aop如下:
接口:
package chapter6.service;
public interface IHelloAroundService {
public void sayAround(String param);
}
接口的实现:
package chapter6.service.impl;
import chapter6.service.IHelloAroundService;
public class HelloAroundService implements IHelloAroundService {
public void sayAround(String param) {
// TODO Auto-generated method stub
System.out.println("========= say around param: " + param);
}
}
aop程序
package chapter6.aop;
import org.aspectj.lang.ProceedingJoinPoint;
public class HelloAroundAspect {
public Object sayAroundAdvice(ProceedingJoinPoint pjp) throws Throwable
{
System.out.println("============= before around advice");
Object retVal = pjp.proceed(new Object[] {"replace"});
System.out.println("============= after around advice");
return retVal;
}
}
配置文件说明:
<!-- 接口的实现 --> <bean id="HelloAroundService" class="chapter6.service.impl.HelloAroundService" /> <!-- 切面程序 --> <bean id="aspect" class="chapter6.aop.HelloAroundAspect" /> <aop:config> <!-- 定义插入点 --> <aop:pointcut expression="execution(* chapter6..*.sayAround(..))" id="pointcut"/> <!-- 通知 --> <aop:aspect ref="aspect"> <aop:around pointcut-ref="pointcut" method="sayAroundAdvice"/> </aop:aspect> </aop:config>
测试程序大同小异不做展示
Aspectj的aop如下:
接口 :
package chapter1.server;
public interface IHelloService {
public void sayAdvisorBefore(String param) ;
}
接口实现:
package chapter1.service.impl;
import chapter1.server.IHelloService;
public class HelloService implements IHelloService {
public void sayAdvisorBefore(String param) {
// TODO Auto-generated method stub
System.out.println("============say " + param);
}
}
aop程序:
package chapter1.aop;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
@Aspect
public class HelloAspect {
//定义切入点
@Pointcut(value="execution(* chapter1..*.sayAdvisorBefore(java.lang.String)) && args(param)", argNames = "param")
public void beforePointcut(String param) {}
//定义通知
@Before(value = "beforePointcut(param)", argNames = "param")
public void beforeAdvice(String param) {
System.out.println("===========before advice param:" + param);
}
}
配置文件:
<!-- 启动对Aspectj的支持 --> <aop:aspectj-autoproxy/> <bean id="helloService" class="chapter1.service.impl.HelloService" /> <bean id="aspect" class="chapter1.aop.HelloAspect"/>
测试程序:
@Test
public void testAspectj()
{
ApplicationContext context = new ClassPathXmlApplicationContext("chapter1/aspectj.xml");
IHelloService hello = context.getBean("helloService", IHelloService.class);
hello.sayAdvisorBefore("before");
}
结果:
===========before advice param:before
============say before