springframework【11】

AOP之aop标签的支持

 

spring2.0提供了基于XML Schema的设置和基于Annotation的支持,这两种方式对于AOP在使用上的简化都大有帮助,这里我们只是介绍基于xml的设置。
1、定义一个Advice类,这个advice类不同于以往的,需要集成一些什么advice类。直接就是一个单独的advice类文件。
 例如:

package com.itcast.aop.xml;
//测试aop标签
import org.aspectj.lang.JoinPoint;;
public class TestAdvice {
	public void fff(JoinPoint jointPoint){
		System.out.println("测试aop标签:"+jointPoint.getSignature().getName());
	}
}

 其中这个JoinPoint可以省略掉
2)、在配置文件上引入新的xmlns
<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">
3、配置使用,配置文件中声明advice bean。同时使用aop标签来设定适用范围

 

<!-- 基于aop标签方式的配置 -->
<bean id="testTag" class="com.itcast.aop.xml.TestAdvice"></bean>
<aop:config>
  <aop:aspect id="logging" ref="testTag">
   <aop:before pointcut="execution(* com.itcast.aop.IHello.*(..))"
   method="fff"/>
  </aop:aspect>
</aop:config>
<bean id="helloSpeaker" class="com.itcast.aop.HelloSpeaker"></bean>

 

 

如上的pointcut中方法名称:
com.itcast.aop.IHello.*(..)表示Ihello方法所声明的任何方法都符合,参数类型设置“(..)”表示任何参数类型声明都符合
 Method方法:指明了Advice上要调用的方法名称,这里是TestAdvice中的fff方法
 Aop:before表示Advice将作为BeforeAdvice

 

4、测试类

package com.itcast.aop.xml;

//测试spring的aop配置
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.itcast.aop.IHello;
public class SpringAOPMain {
	public static void main(String[] args) throws Exception {
		ApplicationContext ctx = new ClassPathXmlApplicationContext("bean-config.xml");
        IHello helloSpeaker = (IHello)ctx.getBean("helloSpeaker");
        helloSpeaker.hello("aaaaaaaaaaaaaaa");
        helloSpeaker.helloAaa("bbbbbbbbbbbbbbb");
        helloSpeaker.helloBbb("ccccccccccccccc");
	}
}

 

5、输出结果

测试aop标签:hello
Hello aaaaaaaaaaaaaaa
测试aop标签:helloAaa
Hello in aaa bbbbbbbbbbbbbbb
测试aop标签:helloBbb
Hello in bbb ccccccccccccccc

 

透过控制台输出的结果可以看到,任何实现了IHello接口类中的方法调用都调用,都被我们声明的advice给拦截了。

相关的hello接口以及实现类,以及声明,参照如下:

 

package com.itcast.aop;
//要实现的接口
public interface IHello {
	public void hello(String name) throws Exception;
	public void helloAaa(String name);
	public void helloBbb(String name);
}

package com.itcast.aop;
import java.util.logging.*;
//实现方法1
public class HelloSpeaker implements IHello{
//	private Logger logger = Logger.getLogger(this.getClass().getName());
	public void hello(String name) throws Exception{
//		logger.info("hello method start");
		System.out.println("Hello "+ name);
//		throw new Exception("自定义的异常");
//		logger.info("hello method end");
	}
	public void helloAaa(String name) {
		System.out.println("Hello in aaa "+ name);
	}
	public void helloBbb(String name) {
		System.out.println("Hello in bbb "+ name);
	}
}

 

总结:spring的aop标签支持需要在xmlschemal上声明命名空间。并需要编写一个特定格式的advice类,最终使用aop标签完成advice对类的拦截。当我们调用bean的时候,如果符合了aop标签声明的监控范围,他将拦截我们的类。

note:不知道aop标签的写法,看一下spring的reference

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值