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 API。 Spring Framework 开发文档。
标题中的“maven仓库中org下的springframework”指的是在Maven的本地或远程仓库中,位于`org`组织下的`springframework`项目。Spring Framework是Java开发中的一个核心框架,由Pivotal Software公司维护,它为构建...
"Spring Framework 4 参考文档中文版" 以下是 Spring Framework 4 参考文档中文版的知识点总结: 一、Spring Framework 概览 * Spring Framework 是一个轻量级的解决方案,是一站式构建企业级应用的一种选择。 * ...
《Spring框架核心模块——org.springframework.core.jar深度解析》 在Java世界中,Spring框架以其卓越的灵活性、可扩展性和模块化设计,成为了企业级应用开发的首选。其中,`org.springframework.core.jar`是Spring...
spring framework 中文参考手册
Spring Framework 是一个广泛使用的开源Java应用框架,特别适用于企业级应用程序开发。它的最新版本v6.1.4为我们带来了许多新特性和改进,使得开发者能够更高效、更灵活地构建可维护的、高性能的应用程序。这个...
Spring Framework API文档。Spring是什么呢?首先它是一个开源的项目,而且非常活跃;它是一个基于IOC和AOP的构架多层j2ee系统的框架,但它不强迫你必须在每一层中必须使用Spring,因为它模块化的很好,允许你根据...
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....
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....
### Spring Framework 简介 #### 一、Spring Framework 的独特之处 在众多J2EE框架中,Spring Framework 自2003年发布以来一直备受瞩目。与其他框架相比,Spring Framework 的独特之处在于: 1. **专注于管理业务...
11. **测试支持**:Spring 提供了丰富的测试工具和API,如Spring Test、Spring Boot Test等,便于编写单元测试和集成测试。 在下载并解压"spring-framework-5.3.32"后,开发者通常会查看源码,了解其内部工作原理,...
Spring Framework 是Java开发中的核心框架,它以其强大的功能和易用性成为了许多开发者首选的工具。Spring Framework 6.0.8-SNAPSHOT 的中文文档是针对这一最新版本的详尽指南,旨在解决中文互联网上关于Spring框架...
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 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 ...
《Spring核心模块详解:org.springframework.core_3.1.1.RELEASE.jar》 在Java开发领域,Spring框架无疑是最重要的框架之一,它以其强大的依赖注入、面向切面编程以及丰富的功能扩展,深受开发者喜爱。其中,`org....
Spring Framework 是一个开源的Java/Java EE全功能栈(full-stack)的应用程序框架,以Apache许可证形式发布,也有.NET平台上的移植版本。 该框架基于 Expert One-on-One Java EE Design and Development(ISBN 0-...
org.springframework.aop-3.0.5.RELEASE.jar org.springframework.asm-3.0.5.RELEASE.jar org.springframework.aspects-3.0.5.RELEASE.jar org.springframework.beans-3.0.5.RELEASE.jar org.springframework....
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 中文翻译 Chinese translation of the Spring Framework 4.x Reference Documentation ...
在Java的Web开发领域,Spring框架是不可或缺的重要工具,其中`org.springframework.web.jar`文件是Spring框架的核心组成部分,主要用于处理Web应用程序的相关功能。这个JAR(Java Archive)文件包含了Spring Web模块...