现在我们开始聊聊AOP
各位应该有看过盗梦空间。
影片开始的时候,莱昂纳多(简称LEO)要盗取日本人斋藤(简称日本人)的信息。
在这里,日本人睡觉的流程和LEO的流程是相互独立的,LEO是需要无声无息(没有发生显式调用)的侵入到日本人的睡觉流程中。
1、使用AOP还需要导入更多的包,这里我们总共需要6个包
aspectjweaver aspectjrt spring spring-aspects common-annotations commons-logging cglib-nodep
见图
2、新建类Person,包com.spring.service
代码如下
package com.spring.service;
import org.springframework.stereotype.Component;
@Component
public class Person {
public void haveSleep()
{
System.out.println(this.getClass().getName());
System.out.println("睡着了");
System.out.println("睡醒了");
}
}
这里我们使用注释的方式进行自动装配,所以Person类上需要做Component注释,我们之后会在haveSleep方法上做拦截。
3、新建类LeoIncept用于表示LEO的盗梦空间
package com.spring.aop;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class LeoIncept {
@Before("execution(public void com.spring.service.Person.haveSleep())")
public void beforeSleep()
{
System.out.println(this.getClass().getName());
System.out.println("马上要睡着了,开始编织梦境");
}
@After("execution(public void com.spring.service.Person.haveSleep())")
public void afterSleep()
{
System.out.println(this.getClass().getName());
System.out.println("醒了,赶快圆梦撤退");
}
}
首先LEOIncept类需要加Aspect注释,表示这是一个切面,用于向其他的方法插入本类。
Component注释表示使用Spring装配为一个类。
方法beforeSleep是一个切入的业务,切入点在Person类的haveSleep方法。@Before表示在haveSleep方法之前执行。
afterSleep方法同理。
这样实现的效果是
1执行LeoIncept beforeSleep
2执行Person haveSleep
3执行 afterSleep
4、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-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"
>
<context:component-scan base-package="com.spring"></context:component-scan>
<context:annotation-config></context:annotation-config>
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>
xml中多了很多AOP的内容。
5、测试类PersonTest包com.spring.service.test
package com.spring.service.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.spring.service.Person;
public class PersonTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext ctx = new ClassPathXmlApplicationContext("springbeans.xml");
Person person = (Person) ctx.getBean("person");
person.haveSleep();
}
}
所有的代码中并没有显示的调用beforeSleep和afterSleep
最后看看执行结果吧
com.spring.aop.LeoIncept
马上要睡着了,开始编织梦境
com.spring.service.Person
睡着了
睡醒了
com.spring.aop.LeoIncept
醒了,赶快圆梦撤退
- 大小: 23.9 KB
分享到:
相关推荐
NULL 博文链接:https://garrincha.iteye.com/blog/2112027
NULL 博文链接:https://garrincha.iteye.com/blog/2111779
NULL 博文链接:https://garrincha.iteye.com/blog/2111957
死磕Spring之AOP篇 - Spring AOP两种代理对象的拦截处理(csdn)————程序
spring aop spring aop spring aop spring aop spring aop spring aop spring aop spring aop spring aop
spring aop jar 包
spring-aop-1.1.1.jar spring-aop-1.2.6.jar spring-aop-1.2.9.jar spring-aop-2.0.2.jar spring-aop-2.0.6.jar spring-aop-2.0.7.jar spring-aop-2.0.8.jar spring-aop-2.0.jar spring-aop-2.5.1.jar spring-aop-...
在Spring AOP中关注点(concern)和横切关注点(cross-cutting concern)有什么不同? AOP有哪些可用的实现? Spring中有哪些不同的通知类型(advice types)? Spring AOP 代理是什么? 引介(Introduction)是什么? 连接...
基于注解实现SpringAop基于注解实现SpringAop基于注解实现SpringAop
一、适合人群 1、具备一定Java编程基础,初级开发者 2、对springboot,mybatis,mysql有基本认识 3、对spring aop认识模糊的,不清楚如何实现Java 自定义注解的 4、想看spring aop 注解实现记录系统日志并入库等 二...
Spring框架的关键组件之一是面向方面编程(AOP)框架。 面向方面的编程需要将程序逻辑分解成不同的部分。 此教程将通过简单实用的方法来学习Spring框架提供的AOP/面向方面编程。
spring aop的demo spring aop的demo
开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE开发工具 spring-aop-4.3.6.RELEASE...
springaop依赖的jar包,spring版本2.5.6,如果需要,可以下载使用,欢迎各位评论指出不足
spring aop
spring之AOP(动态代理),包括jdk动态代理和CGLib动态代理
Spring aop Spring aop
spring aop切面拦截指定类和方法实现流程日志跟踪 一般情况下,在不侵入业务代码的情况下,需要做流程日志跟踪是比较合理的 采用springaop切面思想
springAOP详解