学习笔记(05-03):轻松搞定Spring全家桶(初识篇)-面向切片编程AOP:使用AspectJ基于Annotation开发AOP

与前边学习笔记(04-05):轻松搞定Spring全家桶(初识篇)-控制反转IOC:基于Annotation装配Bean中的应用类似。
使用AspectJ基于Annotation开发AOP:
一、创建项目,导入相关jar包:(点击下载)

  1. spring项目所需基础jar包
  2. 使用AspectJ开发AOP额外需要的jar包

二、在配置文件中加入aop的命名空间配置文件中加入的命名空间

三、具体代码:

  1. 配置文件:/AOPProjectByAspectJAnnotation/resource/applicationContext.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"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">

	<!-- 配置需要自动扫描的包,使得注解起作用 -->
    <context:component-scan base-package="com.aop"></context:component-scan>
    <!-- 开启切面的自动代理:使得切面可以使用 
     	  让aspectj注解起作用: 自动为匹配的类生成代理对象 -->
    <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>
  1. 接口类:/AOPProjectByAspectJAnnotation/src/com/aop/UserDao.java
package com.aop;

public interface UserDao {
	public void addUser(String id,String name);
}
  1. 接口实现类:/AOPProjectByAspectJAnnotation/src/com/aop/UserDaoImpl.java
package com.aop;

import org.springframework.stereotype.Repository;

@Repository("userDao")
public class UserDaoImpl implements UserDao {

	@Override
	public void addUser(String id, String name) {
		// TODO Auto-generated method stub
		//人为制造异常
		//String a=null;a.charAt(0);
		System.out.println("执行逻辑:"+name+" 的id是:"+id);
		System.out.println("-----------方法逻辑结束-----------");
	}

}
  1. 切面类:/AOPProjectByAspectJAnnotation/src/com/aop/LogAspect.java
package com.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

//声明他是切面类
@Aspect
//声明他是一个bean(组件)
@Component
public class LogAspect {
	 /**
     * 定义一个方法,用于声明切入点表达式,一般该方法中不需要再添加代码。 
     * 使用@Pointcut来声明切入点表达式
     * 后面的其他通知直接使用方法名来引用当前的切入点表达式
     */
    @Pointcut("execution(* com.aop.UserDao.addUser(String,String))")
    public void myPointCut() {
    }
	//before
    //这里要填写切入点,填写内容就是声明为切入点的方法名
	@Before("myPointCut()")
	public void myBefore(JoinPoint jp) {
		System.out.println("===before-目标对象:"+jp.getTarget());
		System.out.println("------------before end-------------------");
	}
	//after
	@After("myPointCut()")
	public void myAfter(JoinPoint jp) {
		System.out.println("===after-目标对象:"+jp.getTarget());
		System.out.println("--------目标异常也会执行---------------");
	}
	//around
	@Around("myPointCut()")
	public Object myAround(ProceedingJoinPoint pjp) throws Throwable {
		System.out.println("=======around start::"+pjp.getTarget());
		System.out.println("---------around head end------------");
		Object object = pjp.proceed();
		System.out.println("=======around end::"+pjp.getTarget());
		System.out.println("---------around foot end------------");
		return object;		
	}
	//after-returning
	@AfterReturning("myPointCut()")
	public void myAfterReturning(JoinPoint jp) {
		System.out.println("===after-returning start=====");
		System.out.println("--------目标异常不执行---------------");
	}
	//throwing 
	@AfterThrowing(value="myPointCut()",throwing="e")
	public void myAfterThrowing(JoinPoint jp,Throwable e) {
		System.out.println("error:"+e);
	}
	
	
}
  1. 测试类:/AOPProjectByAspectJAnnotation/src/com/aop/TestUserDao.java
package com.aop;

import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

class TestUserDao {

	@Test
	void testAddUser() {
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		UserDao userDao = context.getBean("userDao",UserDao.class);
		userDao.addUser("T101", "Tom");
	}

}

四、测试结果:

  1. 目标对象异常异常结果
  2. 目标对象正常正常结果
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值