Spring AOP 一、AOP的概念与简单使用
来源:互联网 发布:nba2konline球队数据 编辑:程序博客网 时间:2024/05/09 08:12
一、什么是AOP
AOP(Aspect Oriented Programming)面向切面编程不同于OOP(Object Oriented Programming)面向对象编程,AOP是将程序的运行看成一个流程切面,其中可以在切面中的点嵌入程序。
举个例子,有一个People类,也有一个Servant仆人类,在People吃饭之前,Servant会准备饭,在People吃完饭之后,Servant会进行打扫,这就是典型的面向切面编程.
其流程图为:
二、Spring AOP实现:
1、
People类:
public class People {
public void eat() {
System.out.println(“happyheng开始吃饭啦");
}
public void play(){
}
}
Servant类:
@Aspect
public class Servant {
/**
* 在吃饭之前
*/
@Before("execution(** com.happyheng.entity.People.eat(..))")
public void prepareFood(){
System.out.println("准备食物");
}
/**
* 在吃饭之后
*/
@After("execution(** com.happyheng.entity.People.eat(..))")
public void clean(){
System.out.println("打扫");
}
}
其中的 @Before是指执行前,@After是指执行方法后获取方法抛出异常后,@AfterReturning是指在执行方法后调用,@AfterThrowing是指方法抛出异常后调用。
2、在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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd"
xmlns:context="http://www.springframework.org/schema/context">
<context:component-scan base-package="com.happyheng" />
<aop:aspectj-autoproxy />
<!--注意Aspect的bean必须在Spring中注册,否则不会生效,Spring会用这个bean进行拦截-->
<bean class="com.happyheng.aop.Servant"></bean>
<bean id="happyheng" class="com.happyheng.entity.People"></bean>
</beans>
3、在main中使用:
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext(APPLICATION_XML);
People happyheng = (People)ctx.getBean("happyheng");
happyheng.eat();
}
1、
People类:
public class People {
public void eat() {
System.out.println(“happyheng开始吃饭啦");
}
public void play(){
}
}
Servant类:
@Aspect
public class Servant {
/**
* 在吃饭之前
*/
@Before("execution(** com.happyheng.entity.People.eat(..))")
public void prepareFood(){
System.out.println("准备食物");
}
/**
* 在吃饭之后
*/
@After("execution(** com.happyheng.entity.People.eat(..))")
public void clean(){
System.out.println("打扫");
}
}
其中的 @Before是指执行前,@After是指执行方法后获取方法抛出异常后,@AfterReturning是指在执行方法后调用,@AfterThrowing是指方法抛出异常后调用。
2、在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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd"
xmlns:context="http://www.springframework.org/schema/context">
<context:component-scan base-package="com.happyheng" />
<aop:aspectj-autoproxy />
<!--注意Aspect的bean必须在Spring中注册,否则不会生效,Spring会用这个bean进行拦截-->
<bean class="com.happyheng.aop.Servant"></bean>
<bean id="happyheng" class="com.happyheng.entity.People"></bean>
</beans>
3、在main中使用:
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext(APPLICATION_XML);
People happyheng = (People)ctx.getBean("happyheng");
happyheng.eat();
}
1 0
- Spring AOP 一、AOP的概念与简单使用
- spring(AOP)Aop的概念
- 探析Spring AOP(一):Spring AOP的概念和使用
- Spring的AOP概念
- spring与AOP的使用
- Spring 一 AOP(概念)
- Spring AOP 简单使用
- Spring AOP 概念与术语
- 简单谈谈Spring的IOC与AOP
- Spring AOP的几个概念
- Spring AOP 的关键概念
- spring aop的一些概念
- spring aop 的概念配置
- Spring Aop的那些概念
- spring的AOP概念理解
- 理解Spring AOP 原理(一)Spring AOP的Demo应用和相关概念
- Spring的AOP(一):什么是AOP
- Spring AOP的使用
- Kylin清理数据
- 解决编译Apache出现的问题:configure: error: APR not found . Please read the documentation
- 动态规划,递推,多段图(Ingenuous Cubrency,uva 11137)
- Android上使用camera拍照,把获取的照片上传到远程服务器
- Java字节码中的Stack Map Frames特性简介
- Spring AOP 一、AOP的概念与简单使用
- 用多线程实现生产者与消费者的原理
- 线程互斥
- 20161103学习心得
- 微信小程序正式开放公测(附申请地址和开发文档http://www.bcoder.cn/16277.html)
- Java学习之Jar包class
- 再谈RecyclerView
- UEditor 使用, 选择图片上传卡顿处理
- java.util.concurrent翻译----Executor框架--接口ScheduledExecutorService