AOP (面向切面编程)

软件由多个独立的模块组成,但有些功能分布于各个模块之中,比如记日志,这些功能叫做“横切关注点”,cross-cutting concerns。面向切面编程的目的就是——将横切关注点与模块的业务逻辑相分离

概念

通知,advice

切面要做的事情称作通知。五种类型的通知:
before:在方法运行之前。
after:在方法运行之后,无论方法运行是否成功。
after-returning:在方法成功运行之后通知。
after-throwwing:在方法运行期间抛出异常时通知。
around::类似于jsp的Filter,切点的执行由around()方法调用。

切点,pointCut
切点定义了通知发生在何处。

切面,aspect
切面是“通知”和“切点”的结合。

AOP工具
有三大框架——AspectJ、JBossAOP、SpringAOP。通常用AspectJ。

常用类

org.aspectj.lang. ProceedingJoinPoint
实现around通知的方法的形参。
org.aspectj.lang.ProceedingJoinPoint. proceed()
调用切点方法,类似于jsp的Filter的chain.doFilter(request, response)。

例子

演员表演前观众要入席,表演后观众要鼓掌。

App.java

package com.likeyichu.aop;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App {
	@SuppressWarnings("resource")
	public static void main(String[] args)
	{
		ApplicationContext ctx ;
		ctx=new ClassPathXmlApplicationContext("com/likeyichu/aop/beans.xml");
		Performer performer=ctx.getBean("performer", Performer.class);
		performer.perform();
	
	}
}
/*the audience is taking seats
the performer is performing
clap clap 
*/

Performer.java

package com.likeyichu.aop;

public class Performer{
	public void perform(){
		System.out.println("the performer is performing");
	}
}


Audience.java
package com.likeyichu.aop;

public class Audience {
	//表演之前要坐下
	public void takeSeats(){
		System.out.println("the audience is taking seats");
	}
	//表演成功,故障
	public void applause(){
		System.out.println("clap clap ");
	}
	//若表演失败 需要喝倒彩
	public void boo(){
		System.out.println("boo boo ");
	}
}

beans.xml

<?xml version="1.0" encoding="GBK"?>
<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" 
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context-4.0.xsd
	http://www.springframework.org/schema/aop
	http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"  >
	<bean id="performer" class="com.likeyichu.aop.Performer"/>
	<bean id="audience" class="com.likeyichu.aop.Audience"/>
 <aop:config>
	<aop:aspect ref="audience">
		<aop:pointcut id="performance" expression="execution (* com.likeyichu.aop.Performer.perform(..))"/>
		<aop:before pointcut-ref="performance" method="takeSeats"/>
		<aop:after-returning pointcut-ref="performance" method="applause"/>
		<aop:after-throwing pointcut-ref="performance" method="boo"/>
	</aop:aspect>
</aop:config>
</beans>

maven依赖

<dependencies>
	<dependency>
		<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
		<version>4.1.6.RELEASE</version>
	</dependency>
	<dependency>
			<groupId>org.aspectj</groupId>
		<artifactId>aspectjweaver</artifactId>
		<version>1.6.11</version>
	</dependency>
</dependencies>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的AOP面向切面编程的测试代码示例,使用Spring框架实现: 首先,创建一个切面类 `LoggingAspect`,用于定义切面逻辑: ```java import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.springframework.stereotype.Component; @Aspect @Component public class LoggingAspect { @Before("execution(* com.example.service.*.*(..))") public void beforeAdvice(JoinPoint joinPoint) { System.out.println("Before method: " + joinPoint.getSignature().getName()); } @After("execution(* com.example.service.*.*(..))") public void afterAdvice(JoinPoint joinPoint) { System.out.println("After method: " + joinPoint.getSignature().getName()); } } ``` 然后,创建一个测试服务类 `UserService`,用于演示AOP的应用: ```java import org.springframework.stereotype.Service; @Service public class UserService { public void createUser(String username) { System.out.println("Creating user: " + username); } public void deleteUser(String username) { System.out.println("Deleting user: " + username); } } ``` 最后,创建一个Spring Boot应用程序,并在启动类中进行配置: ```java import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.EnableAspectJAutoProxy; @SpringBootApplication @EnableAspectJAutoProxy public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); UserService userService = context.getBean(UserService.class); userService.createUser("John"); userService.deleteUser("John"); } } ``` 在上述示例中,`LoggingAspect` 切面类使用 `@Before` 和 `@After` 注解分别定义了在目标方法执行前和执行后的逻辑。切面逻辑会应用于 `UserService` 类中的所有方法。 当运行应用程序时,可以看到切面逻辑在方法执行前和执行后打印了相应的日志消息。 这是一个简单的AOP面向切面编程的示例,你可以根据实际需求进行更复杂的切面逻辑定义和应用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值