spring3: schema的aop与Aspectj的aop的区别

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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值