`

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

 

 

 

分享到:
评论

相关推荐

    spring framework 中文参考手册

    spring framework 中文参考手册

    org.springframework.flex-1.0.3.RELEASE.jar.zip

    org.springframework.flex-1.0.3.RELEASE.jar.zip用于JAR包,org.springframework.flex-1.0.3.RELEASE.jar.zip用于JAR包org.springframework.flex-1.0.3.RELEASE.jar.zip用于JAR包org.springframework.flex-1.0.3....

    Spring Framework API文档

    Spring Framework API文档。Spring是什么呢?首先它是一个开源的项目,而且非常活跃;它是一个基于IOC和AOP的构架多层j2ee系统的框架,但它不强迫你必须在每一层中必须使用Spring,因为它模块化的很好,允许你根据...

    org.spring-framework-3.0.4. 所有jar

    org.springframework.aop-3.0.4.RELEASE.jar org.springframework.asm-3.0.4.RELEASE.jar org.springframework.aspects-3.0.4.RELEASE.jar org.springframework.beans-3.0.4.RELEASE.jar org.springframework....

    Getting started with Spring Framework: covers Spring 5(epub)

    Getting started with Spring Framework (4th Edition) is a hands-on guide to begin developing applications using Spring Framework 5. The examples (consisting of 88 sample projects) that accompany this ...

    spring-framework-2.0.2

    org\springframework\aop org\springframework\beans org\springframework\cache org\springframework\context org\springframework\core org\springframework\dao org\springframework\ejb org\spring...

    Spring Framework 5.1.0源码

    Spring Framework 5.1.0源码 The Spring Framework provides a comprehensive programming and configuration model for modern Java-based enterprise applications - on any kind of deployment platform. A key ...

    SpringFramework_v5.0.7.zip

    Spring Framework 是一个开源的Java/Java EE全功能栈(full-stack)的应用程序框架,以Apache许可证形式发布,也有.NET平台上的移植版本。 该框架基于 Expert One-on-One Java EE Design and Development(ISBN 0-...

    Spring Framework v5.0.11

    Spring Framework 是一个开源的Java/Java EE全功能栈(full-stack)的应用程序框架,以Apache许可证形式发布,也有.NET平台上的移植版本。 该框架基于 Expert One-on-One Java EE Design and Development(ISBN 0-...

    Spring Framework 4.x Reference Documentation 中文翻译

    Spring Framework 4.x Reference Documentation 中文翻译 Chinese translation of the Spring Framework 4.x Reference Documentation ...

    Spring Framework 4.0.2.RELEASE API

    Spring Framework 4.0.2.RELEASE API,该文档来自官网经本人制作而成方便使用的chm格式。

    org.springframework.core.jar

    org.springframework.core.jar

    Apress.Introducing.Spring.Framework.A.Primer.

    This book is an introduction to the well-known Spring Framework that offers an inversion of control container for the Java platform. The Spring Framework is an open source application framework that ...

    org.springframework.web.jar

    org.springframework.web.jar,没问题的,最新的。

    org.springframework.web的jar包.zip

    import org.springframework.web.socket.server.standard.ServerEndpointExporter;

    springframework2.5源代码

    springframework2.5源代码

    spring framework 简介

    spring framework 简介

    spring-mock.jar

    org.springframework.mock.jndi.ExpectedLookupTemplate.class org.springframework.mock.jndi.SimpleNamingContext.class org.springframework.mock.jndi.SimpleNamingContextBuilder.class org.springframework....

    spring framework离线文档

    spring framework4.1的英文离线文档和spring framework2.5中文文档

    springframework.zip

    springframework.zip

Global site tag (gtag.js) - Google Analytics