spring 学习笔记 使用aspectj开发aop

本文是关于Spring AOP的学习笔记,重点介绍了如何使用AspectJ进行开发,包括@Before前置通知、@After后置通知和@Around环绕通知的使用方法,以及如何指定通知优先级和重用切入点定义。同时,提到了需要添加的类库aspectjrt.jar和aspectjweaver.jar,并给出了简单的代码示例。

1.添加类库:aspectjrt.jar和aspectjweaver.jar

com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar

com.springsource.org.aspectj.tools-1.6.6.RELEASE.jar

<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.7.1</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>1.7.1</version>
</dependency>

2.添加aop schema.

xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/aop
		http://www.springframework.org/schema/aop/spring-aop-3.1.xsd"

3.定义xml元素:<aop:aspectj-autoproxy>
4.编写java类,并用@Aspect注解成通知,AspectJ 支持 5 种类型的通知注解: 
  @Before: 前置通知, 在方法执行之前执行
  @After: 后置通知, 在方法执行之后执行 
  @AfterRunning: 返回通知, 在方法返回结果之后执行
  @AfterThrowing: 异常通知, 在方法抛出异常之后
  @Around: 环绕通知, 围绕着方法执行

配置成普通bean元素即可.

前置通知:@Before

@Aspectpublic class MyAspectjAdvice {
    @Before("execution(* WelcomeService.*(..))")
    public void sayHello(){..}
    @Before("execution(* WelcomeService.*(..))")

    public void sayHello(JoinPoint jp){..}

JoinPoint参数可访问连接点细节,入方法名和参数等.

jp.getTarget()
jp.getThis()
jp.getArgs()

jp.getSignature().getName()

后置通知:@After

@After("execution(* *..WelcomeService.*(..))")

public void sayBye(){..}

后置通知在目标方法执行完成之后执行.一个切面aspect包含很多通知.

@After 后置通知表明目标方法执行完之后,不论是否抛异常,都会织入该通知.

@AfterReturning 方法返回后通知只在目标方法返回以后执行,若抛异常不执行.

@AfterReturning(pointcut="",returning="res")
public void xxx(Joinput jp,Object res) 
在后置通知中可接收到返回值.res即是用来接收返回值的对象.

环绕通知:@Around

@Around("execution(* *..WelcomeService.*(..))")
public void around(ProceedingPointCut jp){..}
注意:可以控制目标方法是否调用,以及返回完全不同的对象,要慎用.

指定优先级:

@Aspect
@Order(0)
public class xxx{...}
加上@Order注解可以指定加入切面的优先级(先后顺序,值越小,优先级越高)

重用切入点定义:

将切入点注解到一个空的方法体上,其它地方引用即可.
//定义切入点
@Pointcut("execution(* *..WelcomeService.*(..))")
public void performPoint(){}
@Before("performPoint()")
@After("performPoint()")

demo

/**
 * 观众(切面)
 */
@Aspect
public class Audience {
	
	@Pointcut(value="execution(*  study.spring.aop.aspectj.Performer.*(..))")
	public void watch() {}
	// interface ModeifyDate  method set get
	@DeclareParents(value="study.spring.aop.aspectj.Singer",
					defaultImpl=ModifyDateImpl.class)
	public ModifyDate md ;
	
	@Before(value="watch()")
	public  void setting() {
		System.out.println("@Before大批观众正在接近,入场就坐");
	}
	@Before(value="watch()")
	public void turnOffCellphone() {
		System.out.println("@Before关闭手机");
	}
	@AfterThrowing(pointcut="watch()",throwing="e")
	public void hiss(Exception e) {
		System.out.println("@AfterThrowing出事了"+e.getMessage());
	}
	@AfterReturning(pointcut="watch()",returning="ret")
	public void applaud(Object ret) {
		if(ret instanceof Boolean) {
			if((Boolean)ret) {
				System.out.println("@AfterReturning演的太好了,鼓掌....");
			}
			else {
				System.out.println("@AfterReturning演的太差了,嘘声,扔苹果...");
			}
		}else{
			System.out.println("@AfterReturning观众正在观看节目...");
		}
	}
	@After("watch()")
	public  void  goHome() {
		System.out.println("@After节目演完,观众回家");
	}
}
/**
 * 演员
 */
public interface Performer {
	public boolean show();
}
public class Singer implements Performer{
	public boolean show() {
		int nextInt = new Random().nextInt(3);
		if(nextInt==2)throw new RuntimeException("歌手昏倒了 ");
		String str= nextInt==0?"看上去状态很好":"这TMD什么破嗓子!!!";
		System.out.println("唱歌中...啦啦啦啦..."+str);
		if(nextInt==0) return true;
		else return false;
	}
}
<?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:tx="http://www.springframework.org/schema/tx"
	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.1.xsd
				http://www.springframework.org/schema/context 
				http://www.springframework.org/schema/context/spring-context-3.1.xsd
				http://www.springframework.org/schema/tx 
				http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
				http://www.springframework.org/schema/aop
				http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">
	<!-- 观众 -->
	<bean id="audience" class="study.spring.aop.aspectj.Audience"></bean>
	<!-- 歌手 -->
	<bean id="singer" class="study.spring.aop.aspectj.Singer"></bean>
	<!-- 使用自动代理 -->
	<aop:aspectj-autoproxy/>
</beans>
public class AspectjAppDrive {
	public static void main(String[] args) {
		ApplicationContext ac = new ClassPathXmlApplicationContext("study/spring/aop/aspectj/aspectj.xml");
		Performer bean = (Performer)ac.getBean("singer");
		bean.show();
ModifyDate md = (ModifyDate)bean;
		md.setDate(new Date());
		System.out.println(md.getDate());
}
result:1
@Before大批观众正在接近,入场就坐
@Before关闭手机
唱歌中...啦啦啦啦...看上去状态很好
@AfterReturning演的太好了,鼓掌....
@After节目演完,观众回家
---------------------------------------------------
result:2
@Before大批观众正在接近,入场就坐
Exception in thread "main" java.lang.RuntimeException: 歌手昏倒了 
@Before关闭手机
@AfterThrowing出事了歌手昏倒了 
@After节目演完,观众回家
--------------------------------------------------
result:3
@Before大批观众正在接近,入场就坐
@Before关闭手机
唱歌中...啦啦啦啦...这TMD什么破嗓子!!!
@AfterReturning演的太差了,嘘声,扔苹果...
@After节目演完,观众回家



【3D应力敏感度分析拓扑优化】【基于p-范数全局应力衡量的3D敏感度分析】基于伴随方法的有限元分析和p-范数应力敏感度分析(Matlab代码实现)内容概要:本文档介绍了基于伴随方法的有限元分析与p-范数全局应力衡量的3D应力敏感度分析,并结合拓扑优化技术,提供了完整的Matlab代码实现方案。该方法通过有限元建模计算结构在载荷作用下的应力分布,采用p-范数对全局应力进行有效聚合,避免传统方法中应力约束过多的问题,进而利用伴随法高效求解设计变量对应力的敏感度,为结构优化提供关键梯度信息。整个流程涵盖了从有限元分析、应力评估到敏感度计算的核心环节,适用于复杂三维结构的轻量化与高强度设计。; 适合人群:具备有限元分析基础、拓扑优化背景及Matlab编程能力的研究生、科研人员与工程技术人员,尤其适合从事结构设计、力学仿真与多学科优化的相关从业者; 使用场景及目标:①用于实现高精度三维结构的应力约束拓扑优化;②帮助理解伴随法在敏感度分析中的应用原理与编程实现;③服务于科研复现、论文写作与工程项目中的结构性能提升需求; 阅读建议:建议读者结合有限元理论与优化算法知识,逐步调试Matlab代码,重点关注伴随方程的构建与p-范数的数值处理技巧,以深入掌握方法本质并实现个性化拓展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值