【面向切面编程AOP】基于Spring的AOP的配置和使用(注解方式)

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/weixin_43548748/article/details/95531882

她:“好期待我的生日礼物...”
心中暗想:?!她的生日快到了?什么时候?一年过得这么快吗?惨了,忘记日期了...
(求生欲使然)挤出一丝微笑:“好好好,早给你准备好了,到时候给你惊喜。”
拿着手机面对着她,坐在沙发上拇指高频率地翻着相册,希望能找出一张去年她过生日的照片,那便可以找到日期...(现在看来,这是唯一的拯救自己的方法)
“你在干嘛?你找到没有?是不是忘记我的生日了?”
这连环问预示着一股危险的袭面而来,这也无疑加快了大脑的运转,思考速度几乎达到了极致...
假装镇定地摸了一下头。
等等!不是有AOP吗?去年她许愿、吹蜡烛、吃蛋糕、拍照的时候,AOP都以切入动作的方式 在她做这些动作之前就已经做出了记录,生成了日志,这些日志记录有日期呀!还有就是,她的生日过后,AOP也以同样的方式 在她做这些动作之后上传当天的照片到云盘...

目录
1.先来整理一下思路吧!
2.编程开始(基于注解方式的AOP编程)


1.先来整理一下思路吧!

AOP太多概念性的东西这里就不说了,百度一大堆。
一个项目下来,我们进行某些操作的时候,需要自动执行一些额外的操作,如生成日志。那么AOP就可以很方便地实现这种情况,如图:

2.编程开始(基于注解方式的AOP编程)

①新建java项目
②将相应的jar包引入,在百度下载。(或者我写完文章后记得的话再上传)
③新建项目类文件 Wish_Eat_Takephoto_method.java

package items;

public class Wish_Eat_Takephoto_method {
	public void Birthday_method() {
		System.out.println("许愿、吹蜡烛、吃蛋糕、拍照...");
	}
	public void TookPhoto_method() {
		System.out.println("\n拍完照");
	}
}

④新建切面类文件 Birthday_Log1.java

package aop1;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

@Aspect // aspect:切面----标识将这个Birthday_Log1类注册为一个切面
public class Birthday_Log1 {
	SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
	
	// 下面将自定义各个切点,即找到一个条件作为切点(切点有什么用?切点就是到了某个状态下,将本切面切进去执行本切面的某些方法)
	
	// 定义前置通知
//	@Before("execution(* items..*.*(..))")
	@Before("execution(* Birth*(..)) ")//这个就是切点。Before的意思:在  Birth开头的动作执行之前,要先执行下面的doAccessCheck()方法---即"许愿"之前生成日志
	public void doAccessCheck() {
		System.out.println("-----" + df.format(new Date()) + "-----");// new Date()为获取当前系统时间

	}

	// 定义后置通知
	@After("execution(* Took*(..))")//这个也是切点。After的意思:在 Took开头的动作执行之后,就马上执行下面的doReturnCheck()方法
	public void doReturnCheck() {
		System.out.println("-----照片正在上传...-----");
		System.out.println("-----照片上传成功!-----");
	}

	// 定义例外通知
	public void doExceptionAction() {
		System.out.println("-----****-----");
	}

	// 定义最终通知
	public void doReleaseAction() {
		System.out.println("-----****-----");
	}

	// 环绕通知
	public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable {
		System.out.println("-----****------");
		Object proceed = pjp.proceed();
		System.out.println("-----****------");
		return proceed;
	}

}

⑤新建配置文件 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: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.xsd
		http://www.springframework.org/schema/context 
                http://www.springframework.org/schema/context/spring-context.xsd
                http://www.springframework.org/schema/aop         
                http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
                <!-- 注解方式AOP -->
  		<aop:aspectj-autoproxy/>
  		<!-- 注册切面类(生成日志、上传照片等): -->
		<bean id="log" class="aop1.Birthday_Log1" />
		<!-- 被切入的类/方法(许愿、吹蜡烛、吃蛋糕、、、) -->
  		<bean id="item_m" class="items.Wish_Eat_Takephoto_method"/>

</beans>

⑥测试类Test.java

package items;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {

	@SuppressWarnings("resource")
	public static void main(String[] args) {
		BeanFactory beanFactory = new ClassPathXmlApplicationContext("ApplicationContext.xml");//大容器(bean工厂,用于进口<beans>,即获取xml文件的beans。)
		Wish_Eat_Takephoto_method girlf = (Wish_Eat_Takephoto_method)beanFactory.getBean("item_m");
		girlf.Birthday_method();
		girlf.TookPhoto_method();
		
	}

}

⑦执行Test.java



还有什么问题,评论见!晚了,要睡了,有时间再写基于配置方式的AOP编程。

猜你喜欢

转载自blog.csdn.net/weixin_43548748/article/details/95531882