`
chimpp55
  • 浏览: 21854 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Spring_1600_AOP_XML Aspect 的两种配置方法

 
阅读更多
package com.bjsxt.aop;

import org.aspectj.lang.ProceedingJoinPoint;

//@Aspect
//@Component
public class LogInterceptor {
	//@Pointcut("execution(public * com.bjsxt.service..*.add(..))")
	public void myMethod(){};

	//@Before("myMethod()")
	public void before() {
		System.out.println("method before");
	}

	//@Around("myMethod()")
	public void aroundMethod(ProceedingJoinPoint pjp) throws Throwable {
		System.out.println("method around start");
		pjp.proceed();
		System.out.println("method around end");
	}

	//@After("myMethod()")
	public void afterMethod() throws Throwable {
		System.out.println("method after");
	}
}



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:context="http://www.springframework.org/schema/context"
	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.5.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context-2.5.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
	<context:annotation-config />
	<context:component-scan base-package="com.bjsxt" />

	<bean id="logInterceptor" class="com.bjsxt.aop.LogInterceptor"></bean>
	<aop:config>
		<aop:aspect id="logAspect" ref="logInterceptor">
			<aop:before method="before"
				pointcut="execution(public * com.bjsxt.service..*.add(..))" />

			<aop:around method="aroundMethod"
				pointcut="execution(public * com.bjsxt.service..*.add(..))" />

			<aop:after method="aroundMethod"
				pointcut="execution(public * com.bjsxt.service..*.add(..))" />

		</aop:aspect>


	</aop:config>




</beans>


当下面注掉时,需要把最上面的方法打开,这样才能调用
<!-- 	<bean id="logInterceptor" class="com.bjsxt.aop.LogInterceptor"></bean> -->
<!-- 	<aop:config> -->
<!-- 		<aop:aspect id="logAspect" ref="logInterceptor"> -->
<!-- 			<aop:before method="before" -->
<!-- 				pointcut="execution(public * com.bjsxt.service..*.add(..))" /> -->

<!-- 			<aop:around method="aroundMethod" -->
<!-- 				pointcut="execution(public * com.bjsxt.service..*.add(..))" /> -->

<!-- 			<aop:after method="aroundMethod" -->
<!-- 				pointcut="execution(public * com.bjsxt.service..*.add(..))" /> -->

<!-- 		</aop:aspect> -->


<!-- 	</aop:config> -->




总结:一种是在java 文件里,进行配置; 另一种是在xml 文件中进行配,都能实现相应的调用!

分享到:
评论

相关推荐

    spring_aop_xml.rar_java aop_xml aop

    在实际的`spring_aop_xml`压缩包中,应该包含了相关的XML配置文件、服务接口及其实现类、通知类等,通过这些文件可以更好地理解和学习如何在XML中配置和使用Spring AOP。通过深入学习和实践,你将能熟练掌握这一强大...

    spring_aop xml方式实现aop

    XML配置是Spring AOP早期常用的一种配置方式,虽然在Spring 4.3之后推荐使用注解式AOP,但理解XML配置对于深入学习AOP仍然很有帮助。下面我们将详细讨论如何通过XML配置实现Spring AOP。 首先,我们需要在Spring...

    spring_AOP.rar_876.aop_java aop_spring_aop

    Spring AOP(面向切面编程)是Java开发中Spring框架的核心特性之一,它提供了一种在不修改源代码的情况下,对程序进行功能增强的技术。在SSH(Struts、Spring、Hibernate)经典企业级开发框架中,Spring AOP扮演着至...

    Spring_AOP_学习小结 Spring_AOP_学习小结 Spring_AOP_学习小结

    Spring AOP通过两种代理机制实现: 1. JDK动态代理:只能代理实现了接口的目标对象,性能相对较低,需要指定代理接口。 2. CGLIB代理:可代理接口和非final的类,性能较高,通过生成字节码实现。 四、Spring AOP...

    Spring_AOP_XML配置

    **Spring AOP XML配置**是Spring框架中一种重要的面向切面编程(Aspect-Oriented Programming,简称AOP)实现方式,允许开发者定义“横切关注点”,如日志、事务管理等,这些关注点可以独立于业务代码进行,提高代码...

    spring aop注解方式、xml方式示例

    Spring AOP提供了注解和XML两种方式来实现切面编程。注解方式更加简洁,易于理解和维护,适用于大多数情况。而XML配置方式则在复杂场景下更具灵活性,如需要动态调整切面配置时。在实际项目中,可以根据需求选择适合...

    8_24_SpringAOP_springaop_

    Spring AOP,全称Aspect-Oriented Programming(面向切面编程),是Spring框架的重要组成部分,它为应用程序提供了声明式的企业级服务,如日志、事务管理等。在本教程中,我们将深入探讨Spring AOP的基本概念、配置...

    Spring_Annotation_AOP

    在Spring中,AOP主要通过两种方式实现:基于XML配置和基于注解。随着Java注解的普及,越来越多的开发者倾向于使用注解来简化配置。在"Spring_0200_Annotation_AOP"中,我们重点关注的是注解驱动的AOP。 1. **@...

    Spring_aop_xml.zip

    本实践项目“Spring_aop_xml.zip”以XML配置方式展示了如何在实际应用中运用Spring AOP。 首先,让我们深入了解Spring AOP的核心概念。AOP通过切面(Aspect)来组织代码,切面是关注点的模块化,这些关注点通常涉及...

    Spring_02_入门篇_AOP_简单实例

    本教程将深入探讨Spring中的核心概念之一——面向切面编程(Aspect-Oriented Programming,简称AOP),并提供基于注解和XML配置的两种实现方式的简单实例。 **一、什么是AOP** 面向切面编程(AOP)是一种编程范式...

    Around_AOP_Spring.zip_aop

    本示例“Around_AOP_Spring.zip_aop”主要讲解了Spring AOP中的环绕通知(Around Advice),这是AOP五种通知类型中最强大的一种。 1. **什么是AOP** - AOP的核心思想是将程序中的横切关注点(如日志、安全检查、...

    Spring_aop.rar_spring aop

    - **XML配置的AOP**:在Spring配置文件中定义&lt;aop:config&gt;元素,使用&lt;aop:aspect&gt;定义切面,&lt;aop:before&gt;、&lt;aop:after&gt;等定义通知,&lt;aop:pointcut&gt;定义切入点。 **4. 应用示例** 假设我们有一个`UserService`,需要...

    spring_aop1.rar_java aop_spring mvc 例子

    Spring提供了注解驱动的AOP,使得无需编写XML配置文件即可声明切面。 在“spring_aop1.rar”中,我们可以预期找到以下几个关键知识点: 1. **切点注解**:例如`@Before`、`@After`、`@Around`、`@AfterReturning`...

    spring_aop.rar

    在Spring框架早期,AOP的配置主要通过XML来进行。以下是一个简单的示例: ```xml &lt;aop:config&gt; &lt;aop:aspect id="loggingAspect" ref="loggingService"&gt; &lt;aop:before method="logBefore" pointcut="execution(* ...

    spring aop管理xml版

    在Spring框架中,面向切面编程(Aspect Oriented Programming,简称AOP)是一种重要的设计模式,它扩展了传统的面向对象编程(OOP),使得我们可以将关注点分离,特别是那些横切关注点,如日志、事务管理、权限检查...

    spring使用动态代理面向切面编程(AOP) xml

    文件名`spring_1600_AOP_xml`可能是指Spring AOP的示例代码,可能包含了如何在XML配置中定义和使用AOP的相关示例。这些示例通常会涵盖上述所有步骤,包括定义切面、切入点、通知和织入,帮助读者更好地理解和应用...

    springAop.rar_AOP java_cglib_spring aop

    XML配置方式中,我们可以通过`&lt;aop:config&gt;`、`&lt;aop:aspect&gt;`等元素定义切面和通知;而在注解方式下,我们可以使用`@Aspect`、`@Before`、`@After`等注解来实现相同的功能。 在提供的压缩包文件中,...

    SpringAop xml方式配置通知

    **Spring AOP XML方式配置通知** 在Java世界中,Spring框架是广泛应用的IoC(Inversion of Control)和AOP(Aspect Oriented Programming)容器。AOP允许开发者定义“方面”,这些方面可以封装关注点,如日志、事务...

    spring_aop

    项目“maven-spring-aop”可能包含了使用Maven构建的Spring AOP示例项目,其中包含了完整的XML配置、Java类和测试用例,便于开发者深入理解和实践Spring AOP的使用。通过这个项目,你可以亲自动手创建、运行并观察...

Global site tag (gtag.js) - Google Analytics