首页 技术 正文
技术 2022年11月9日
0 收藏 565 点赞 3,081 浏览 2540 个字

回见Java框架spring Boot学习笔记(十三):aop实例操作,这里介绍注解aop操作

首先编写一个切入点HelloWorld.java

 package com.example.spring; public class HelloWorld {
public void printHello(){
System.out.println("Hello Aop.");
}
}

编写切面TimeHandler.java

 package com.example.spring; import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before; //使用@Aspect注解轻松定义切面
@Aspect
public class TimeHandler { // 在方法上面使用注解完成前置增强配置
@Before(value = "execution(* com.example.spring.HelloWorld.*(..))")
public void beforTime()
{
System.out.println("前置增强:CurrentTime = " + System.currentTimeMillis());
} // 在方法上面使用注解完成后置增强配置
@After(value = "execution(* com.example.spring.HelloWorld.*(..))")
public void afterTime()
{
System.out.println("后置增强:CurrentTime = " + System.currentTimeMillis());
} // 在方法上面使用注解完成环绕增强配置
@Around(value = "execution(* com.example.spring.HelloWorld.*(..))")
public void aroundTime(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
//方法之前
System.out.println("环绕增强:CurrentTime = " + System.currentTimeMillis()); //执行被增强的方法
proceedingJoinPoint.proceed(); //方法之后
System.out.println("环绕增强:CurrentTime = " + System.currentTimeMillis());
}
}

编写配置文件aop.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd"> <!-- bean definition & AOP specific configuration -->
<!-- 1 配置对象-->
<bean id="helloWorld" class="com.example.spring.HelloWorld"/>
<bean id="timeHandler" class="com.example.spring.TimeHandler"/> <!-- 2 开启aop操作-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy> </beans>

编写运行文件Application.java

 package com.example.spring; import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Application {
public static void main(String[] args) {
//bean配置文件所在位置 D:\\IdeaProjects\\spring\\src\\Beans.xml
//使用AbstractApplicationContext容器
AbstractApplicationContext context = new ClassPathXmlApplicationContext("file:D:\\IdeaProjects\\spring\\src\\aop.xml");
//得到配置创建的对象
HelloWorld helloWorld = (HelloWorld)context.getBean("helloWorld");
helloWorld.printHello();
}
}

运行输出

环绕增强:CurrentTime = 1510208830742
前置增强:CurrentTime = 1510208830742
Hello Aop.
环绕增强:CurrentTime = 1510208830750
后置增强:CurrentTime = 1510208830750
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,093
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,570
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,417
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,190
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,825
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,908