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`压缩包中,应该包含了相关的XML配置文件、服务接口及其实现类、通知类等,通过这些文件可以更好地理解和学习如何在XML中配置和使用Spring AOP。通过深入学习和实践,你将能熟练掌握这一强大...
XML配置是Spring AOP早期常用的一种配置方式,虽然在Spring 4.3之后推荐使用注解式AOP,但理解XML配置对于深入学习AOP仍然很有帮助。下面我们将详细讨论如何通过XML配置实现Spring AOP。 首先,我们需要在Spring...
Spring AOP(面向切面编程)是Java开发中Spring框架的核心特性之一,它提供了一种在不修改源代码的情况下,对程序进行功能增强的技术。在SSH(Struts、Spring、Hibernate)经典企业级开发框架中,Spring AOP扮演着至...
Spring AOP通过两种代理机制实现: 1. JDK动态代理:只能代理实现了接口的目标对象,性能相对较低,需要指定代理接口。 2. CGLIB代理:可代理接口和非final的类,性能较高,通过生成字节码实现。 四、Spring AOP...
**Spring AOP XML配置**是Spring框架中一种重要的面向切面编程(Aspect-Oriented Programming,简称AOP)实现方式,允许开发者定义“横切关注点”,如日志、事务管理等,这些关注点可以独立于业务代码进行,提高代码...
Spring AOP提供了注解和XML两种方式来实现切面编程。注解方式更加简洁,易于理解和维护,适用于大多数情况。而XML配置方式则在复杂场景下更具灵活性,如需要动态调整切面配置时。在实际项目中,可以根据需求选择适合...
Spring AOP,全称Aspect-Oriented Programming(面向切面编程),是Spring框架的重要组成部分,它为应用程序提供了声明式的企业级服务,如日志、事务管理等。在本教程中,我们将深入探讨Spring AOP的基本概念、配置...
在Spring中,AOP主要通过两种方式实现:基于XML配置和基于注解。随着Java注解的普及,越来越多的开发者倾向于使用注解来简化配置。在"Spring_0200_Annotation_AOP"中,我们重点关注的是注解驱动的AOP。 1. **@...
本实践项目“Spring_aop_xml.zip”以XML配置方式展示了如何在实际应用中运用Spring AOP。 首先,让我们深入了解Spring AOP的核心概念。AOP通过切面(Aspect)来组织代码,切面是关注点的模块化,这些关注点通常涉及...
本教程将深入探讨Spring中的核心概念之一——面向切面编程(Aspect-Oriented Programming,简称AOP),并提供基于注解和XML配置的两种实现方式的简单实例。 **一、什么是AOP** 面向切面编程(AOP)是一种编程范式...
本示例“Around_AOP_Spring.zip_aop”主要讲解了Spring AOP中的环绕通知(Around Advice),这是AOP五种通知类型中最强大的一种。 1. **什么是AOP** - AOP的核心思想是将程序中的横切关注点(如日志、安全检查、...
- **XML配置的AOP**:在Spring配置文件中定义<aop:config>元素,使用<aop:aspect>定义切面,<aop:before>、<aop:after>等定义通知,<aop:pointcut>定义切入点。 **4. 应用示例** 假设我们有一个`UserService`,需要...
Spring提供了注解驱动的AOP,使得无需编写XML配置文件即可声明切面。 在“spring_aop1.rar”中,我们可以预期找到以下几个关键知识点: 1. **切点注解**:例如`@Before`、`@After`、`@Around`、`@AfterReturning`...
在Spring框架早期,AOP的配置主要通过XML来进行。以下是一个简单的示例: ```xml <aop:config> <aop:aspect id="loggingAspect" ref="loggingService"> <aop:before method="logBefore" pointcut="execution(* ...
在Spring框架中,面向切面编程(Aspect Oriented Programming,简称AOP)是一种重要的设计模式,它扩展了传统的面向对象编程(OOP),使得我们可以将关注点分离,特别是那些横切关注点,如日志、事务管理、权限检查...
文件名`spring_1600_AOP_xml`可能是指Spring AOP的示例代码,可能包含了如何在XML配置中定义和使用AOP的相关示例。这些示例通常会涵盖上述所有步骤,包括定义切面、切入点、通知和织入,帮助读者更好地理解和应用...
XML配置方式中,我们可以通过`<aop:config>`、`<aop:aspect>`等元素定义切面和通知;而在注解方式下,我们可以使用`@Aspect`、`@Before`、`@After`等注解来实现相同的功能。 在提供的压缩包文件中,...
**Spring AOP XML方式配置通知** 在Java世界中,Spring框架是广泛应用的IoC(Inversion of Control)和AOP(Aspect Oriented Programming)容器。AOP允许开发者定义“方面”,这些方面可以封装关注点,如日志、事务...
项目“maven-spring-aop”可能包含了使用Maven构建的Spring AOP示例项目,其中包含了完整的XML配置、Java类和测试用例,便于开发者深入理解和实践Spring AOP的使用。通过这个项目,你可以亲自动手创建、运行并观察...