] [Aspect Oriented Programming AOP based configuration and use of Spring AOP (the annotation mode)

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/weixin_43548748/article/details/95531882

She: "Well look forward to my birthday ..."
thinking to myself:? ! Her birthday is coming up? When? Year goes by so fast? Miserable, and forget the date ...
(For Survival dictates) and forced a smile: "Ok, back to you ready to give you time to surprise."
Hold the phone face her, sitting on the sofa thumb high frequency turned over the album, hoping to find a picture of her birthday last year, that date will be able to find ... (it now appears that this is the only way to save himself)
"What are you doing? you find no? is not forget my birthday? "
this comic asked presage a dangerous gets in my face, which will undoubtedly speed up the operation of the brain, thinking speed almost to the extreme ...
pretending to be calm touched the head .
and many more! AOP not have it? Last year she was a wish, blow out the candles, cake, take pictures when, AOP have to cut into the action to do these actions before she had made a record, generated logs that record the date ah! There is, after her birthday, AOP also in the same way to upload photos to the cloud disk day after she made these actions ...

Contents
1. First sort out the idea of it!
2. programming begins (AOP-based programming annotation mode)


1. First sort out the idea of ​​it!

AOP too conceptual thing here is not to say, Baidu lot.
A project down, we carry out certain operations when the need to automatically perform some additional operations, such as generating a log. So AOP can easily achieve this case, as:

2. programming begins (AOP-based programming annotation mode)

① new java project
② the introduction of the corresponding jar package, downloaded from Baidu. (Or I remember after writing the article, then re-upload)
③ New Project class file  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拍完照");
	}
}

④ new section class file  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;
	}

}

⑤ New Profile  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 class 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();
		
	}

}

⑦ execution Test.java



Any questions, comments, see! Late, I want to sleep and have time to write AOP-based programming configuration mode.

 

Guess you like

Origin blog.csdn.net/weixin_43548748/article/details/95531882