1. service类
package com.ssm.blog.aop;
public interface AopService {
public void sayAround(String param);
}
2. serviceImpl类
package com.ssm.blog.aop;
public class AopServiceImpl implements AopService{
public void sayAround(String param) {
System.out.println("============around param:" + param);
}
}
3. aop环绕通知类
package com.ssm.blog.aop;
import org.aspectj.lang.ProceedingJoinPoint;
public class StringAop{
public Object aroundAdvice(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("===========around before advice");
Object[] obj = pjp.getArgs() ;
Object retVal = null ;
for(int i=0;i<obj.length;++i){
System.out.println(i+"---->"+obj[i]);
if(obj[i].equals("haha")){
retVal = pjp.proceed(new Object[] {"replace12312321"});
}
}
//Object retVal = pjp.proceed(new Object[] {"replace"});
System.out.println("===========around after advice");
return retVal ;
}
}
<bean id="aspectSupportBean" class="com.ssm.blog.aop.StringAop"/>
<bean id="aopService" class="com.ssm.blog.aop.AopServiceImpl"/>
<aop:config>
<aop:aspect ref="aspectSupportBean">
<aop:around pointcut="execution(* com.ssm..*.sayAround(..))" method="aroundAdvice"/>
</aop:aspect>
</aop:config>
5. Junit测试
public class TestAop {
@Test
public void testSchemaAroundAdvice() {
System.out.println("======================================");
ApplicationContext ctx = new ClassPathXmlApplicationContext("aop.xml");
AopService aopService = ctx.getBean("aopService", AopService.class);
aopService.sayAround("haha");
System.out.println("======================================");
}
}