Spring AOP 一、AOP的概念与简单使用
2016-11-03 22:53
477 查看
一、什么是AOP
AOP(Aspect Oriented Programming)面向切面编程不同于OOP(Object Oriented Programming)面向对象编程,AOP是将程序的运行看成一个流程切面,其中可以在切面中的点嵌入程序。
举个例子,有一个People类,也有一个Servant仆人类,在People吃饭之前,Servant会准备饭,在People吃完饭之后,Servant会进行打扫,这就是典型的面向切面编程.
其流程图为:
![](https://img-blog.csdn.net/20161103225428433?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
二、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();
}
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();
}
相关文章推荐
- JavaEE---------简单使用JDK动态代理模拟Spring的AOP
- spring aop 使用xml方式的简单总结
- Spring aop 使用aspectjrt配置切面+ehcache简单配置
- Spring AOP学习(二) -- Spring AOP的简单使用
- Spring - Aop简单的mvc示例 和 Aop注解的使用
- Spring第九弹—使用CGLIIB实现AOP功能与AOP概念解释
- 探析Spring AOP(一):Spring AOP的概念和使用
- ITCAST视频-Spring学习笔记(使用CGLIB实现AOP功能与AOP概念解释)
- SpringAop切面的简单使用
- 8 -- 深入使用Spring -- 4...3 AOP的基本概念
- Spring使用 --- 基本概念(二):AOP,面向方面编程
- 使用AOP,在spring中实现简单的性能测试
- spring 4.0 AOP (使用AspectJ的注解方式 的aop实现)简单实例
- spring 4.0 AOP (使用AspectJ的注解方式 的aop实现)简单实例
- Spring AOP-->面向切面编程简单理解和简单使用
- 16_传智播客Spring2.5视频教程_使用CGLIB实现AOP功能与AOP概念解释
- spring中aop的简单使用(Annotation方式)
- [原创]java WEB学习笔记105:Spring学习---AOP介绍,相关概念,使用AOP,利用 方法签名 编写 AspectJ 切入点表达式
- spring 4.0 AOP (使用AspectJ的注解方式 的aop实现)简单实例
- Spring学习--IOC容器的概念及一个简单IOC容器使用案例