`
wuyaweiwude
  • 浏览: 121740 次
  • 性别: Icon_minigender_1
  • 来自: 大连
社区版块
存档分类

Spring中基于Schema的AOP配置

阅读更多
本文用于知识点的简单总结。

对于Spring中AOP的配置,一种方式是基于Schema,也就是在xml文件中进行配置,相关配置项在aop命名空间下定义。以下主要通过代码说明基本配置流程。

(1)定义需要实现的业务接口:
package com.aop.service;

public interface MyService {
    
    public void sayHello(String word);
    
}

(2)编写实现接口的业务类:
package com.aop.service;

public class MyServiceImpl implements MyService {

    @Override
    public void sayHello(String word) {
	System.out.println("Say :" + word);
    }

}

(3)定义切面支持类,包含所需的通知方法:
package com.aop.aspect;

import org.aspectj.lang.JoinPoint;

public class MyAspect {
    //前置通知
    public void beforeAdvice(JoinPoint joinPoint) {
	System.out.println("--before advice,参数:" + joinPoint.getArgs()[0]);
    }

    //后置最终通知   
    public void afterFinallyAdvice() {
	System.out.println("------after finally advice");
    }
}

(4)beans.xml配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<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-3.0.xsd   
           http://www.springframework.org/schema/aop   
           http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

	<!-- 接口实现业务类 -->
	<bean id="MyService" class="com.aop.service.MyServiceImpl" />
	
	<!-- 切面支持类,包含所需的通知方法 -->
	<bean id="aspect" class="com.aop.aspect.MyAspect" />
	
	<aop:config>
		<!-- 切点配置,匹配com.aop.service包及子包下的任何方法执行 -->
		<aop:pointcut id="pointcut" expression="execution(* com.aop.service..*.*(..))" />
		<!-- 切面配置, ref 属性用来引用切面支持类 -->
		<aop:aspect ref="aspect">
			<!-- 前置通知,pointcut-ref 引用切点配置bean,method指定通知方法 -->
			<aop:before pointcut-ref="pointcut" method="beforeAdvice" />
			<aop:after pointcut-ref="pointcut" method="afterFinallyAdvice" />
		</aop:aspect>
	</aop:config>

</beans> 

(5)测试类:
package com.spring.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import com.aop.service.MyService;

public class TestAop {

    public static void main(String[] args) {
	
	ApplicationContext context = new FileSystemXmlApplicationContext("aopBeans.xml");	
	MyService service = (MyService) context.getBean("MyService");
	
	service.sayHello("Spring aop");
    }
}

(6)输出结果:
--before advice,参数:Spring aop
Say :Spring aop
------after finally advice
分享到:
评论

相关推荐

    Spring AOP之基于Schema配置总结与案例

    本文将深入探讨Spring AOP中的基于Schema配置方式,并通过实例来阐述其应用。 首先,让我们了解什么是Spring AOP。AOP的核心思想是将那些分散在各个类中的、与业务逻辑无关但对多个类都有影响的代码(如日志、事务...

    Spring aop 基于schema的AOP支持及JoinPoint的使用、如何使用CGLIB代理

    在基于Schema的AOP中,通常采用的是运行时织入,即在应用程序启动时动态地将切面应用到目标对象。 **JoinPoint的使用** JoinPoint是Spring AOP中的一个重要概念,它代表了程序执行中的一个点,比如方法的执行。在...

    Spring之AOP配置文件详解

    在给定的配置文件中,我们看到了一个典型的Spring AOP配置实例。接下来我们将对这段配置进行详细的分析与解读。 ##### 2.1 配置文件头 ```xml ``` 这是XML文档的声明部分,指明了文档采用的XML版本为1.0,并且...

    基于注解配置和使用spring AOP(spring mvc框架)

    在Spring AOP中,切面可以是Java类或@Aspect注解的类。 2. **通知(Advice)**:通知是指在特定连接点执行的代码块,如方法调用前、后或异常发生时。Spring支持五种不同类型的通知:前置通知(before)、后置通知...

    征服Spring AOP—— Schema

    通过Spring AOP的Schema配置,我们可以清晰地看到程序中的切面逻辑,这有助于理解和维护代码。同时,这种方式虽然相对直观,但在大型项目中,可能会导致XML配置文件变得庞大且难以管理。因此,在实际开发中,通常会...

    SpringAop xml方式配置通知

    在Spring AOP中,切面由`@Aspect`注解的类定义。 2. **连接点(Join Point)**:程序执行过程中的某个特定点,如方法调用、异常处理等。 3. **通知(Advice)**:在特定连接点执行的代码,即切面要实施的行为,...

    Spring Aop Schema实现

    在Spring AOP中,切面由通知(Advice)和切点(Pointcut)定义。 2. 通知(Advice):通知是在特定连接点(Join Point)执行的代码,比如方法调用前、后或异常时。Spring支持五种不同类型的Advice:Before、After、...

    spring-aop实例demo

    1. **配置AOP**:在Spring配置文件中启用AOP代理。这可以通过设置`&lt;aop:aspectj-autoproxy&gt;`或`&lt;aop:config&gt;`元素来完成。例如: ```xml &lt;beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi=...

    spring入门学习-6、AOP几种配置方式详解.pdf

    Spring 提供了多种方式来支持 AOP 的实现,主要包括基于代理的经典 AOP、Schema-based 的 AOP 和 @AspectJ 注解驱动的 AOP。本文将详细介绍这三种类型的配置方式。 #### 二、基于代理的经典 AOP ##### 2.1 ...

    spring-aop.xsd

    `spring-aop.xsd`文件是Spring AOP配置的XML Schema定义文件,用于指导和验证XML配置中的元素和属性,确保其符合规范,同时也为IDE提供代码提示和自动补全功能,提高开发效率。 在Java开发中,AOP(Aspect Oriented...

    spring aop的两种配置方式.docx

    在传统的Spring AOP配置中,我们通常会在`applicationContext.xml`或类似的配置文件中定义切面、通知(advice)、切入点(pointcut)等元素。以下是一个简单的示例: ```xml &lt;beans xmlns="http://www.spring...

    Spring学习笔记(16)----使用Spring配置文件实现AOP

    在本篇Spring学习笔记中,我们将深入探讨如何利用Spring配置文件来实现面向切面编程(AOP)。面向切面编程是Spring框架的核心特性之一,它允许我们把关注点分离,将横切关注点(如日志、事务管理、权限控制等)与...

    spring4.2.0的schema约束

    在Spring框架中,schema约束是XML配置文件中的一个重要组成部分,它们定义了如何正确地配置Spring容器中的各种组件,如bean、property、aop等。Spring 4.2.0是该框架的一个版本,引入了一些新的特性和改进,同时也对...

    spring1.x使用AOP实例

    在Spring 1.x中,我们首先需要配置AOP支持。这通常在XML配置文件中完成,例如`applicationContext.xml`。我们需要引入AOP命名空间,并声明一个`&lt;aop:config&gt;`元素来启用AOP功能: ```xml &lt;beans xmlns="http://www....

    利用SPring AOP配置切面的一个例子

    ### Spring AOP 配置切面实例解析 #### 一、引言 本文将通过一个具体的例子来介绍如何在Spring框架中使用面向切面编程(Aspect Oriented Programming, AOP)。我们将关注如何定义一个切面(Aspect),以及如何在...

    Spring AOP配置源码

    @Component("userService")等价于在spring配置文件中定义一个&lt;bean id="userService"/&gt; @Resource(name="userDAO")将userDA注入进来 写一个拦截器的类 package com.spring.aop; import org.springframework....

    spring-framework-4.2.5-schema

    在4.2.5版本中,Spring引入了对各种XML Schema的支持,极大地简化了配置过程,提高了代码的可读性和可维护性。本文将深入探讨这些schema,以及它们如何帮助开发者更好地理解和使用Spring Framework。 首先,我们来...

    spring-aop-4.2.xsd.zip

    XSD文件在Spring框架中主要用来验证和解析基于XML的配置文件,例如`beans.xml`,这些文件定义了bean的实例化、依赖注入、AOP切面等配置。 压缩包内的子文件"spring-aop-4.2.xsd.txt"可能是XSD文件的文本版本,方便...

Global site tag (gtag.js) - Google Analytics