一、接口切入方式

实现类

package com.pb.entity;
/**
* 实体类
*/
public class Hello {
private String name;
private String password; public void show(){
System.out.println("姓名 :"+this.getName()+"密码: "+this.getPassword());
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} }

1.1、前置增强

package com.pb.aop;

import java.lang.reflect.Method;

import org.springframework.aop.MethodBeforeAdvice;
/**
* 前置增强的Bean
* @author Administrator
*实现MethodBeforeAdvice接口
*/
public class BeforeAdded implements MethodBeforeAdvice { @Override
public void before(Method arg0, Object[] arg1, Object arg2)
throws Throwable {
System.out.println("====前置增强!====="); } }

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:p="http://www.springframework.org/schema/p"
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.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">
<!--hello Bean -->
<bean id="hello" class="com.pb.entity.Hello" p:name="张三" p:password="123qwe"/>
<!--切入的Bean -->
<bean id="beforeAdd" class="com.pb.aop.BeforeAdded"/>
<!--前置增强切入的Bean -->
<aop:config>
<!-- 切入点 -->
<aop:pointcut expression="execution(* com.pb.entity.Hello.*(..))" id="myPoint"/>
<!-- 关联切入类和切入点 -->
<aop:advisor advice-ref="beforeAdd" pointcut-ref="myPoint"/>
</aop:config>
</beans>

1.2、后置增强

package com.pb.aop;

import java.lang.reflect.Method;

import org.springframework.aop.AfterReturningAdvice;
/**
* 后置增强
*/
public class AfterAdded implements AfterReturningAdvice { @Override
public void afterReturning(Object arg0, Method arg1, Object[] arg2,
Object arg3) throws Throwable {
System.out.println("====这里是后置增强!===="); } }

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:p="http://www.springframework.org/schema/p"
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.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">
<!--hello Bean -->
<bean id="hello" class="com.pb.entity.Hello" p:name="张三" p:password="123qwe"/>
<!--切入的Bean --> <!--后置增强切入的Bean -->
<bean id="afterAdd" class="com.pb.aop.AfterAdded"/>
<aop:config>
<!-- 切入点 -->
<aop:pointcut expression="execution(* com.pb.entity.Hello.*(..))" id="myPoint"/>
<!-- 关联后置增强切入类和切入点 -->
<aop:advisor advice-ref="afterAdd" pointcut-ref="myPoint"/>
</aop:config>
<!--后置增强切入的Bean --> </beans>

1.3、异常增强

实体类中增加异常

package com.pb.entity;
/**
* 实体类
*/
public class Hello {
private String name;
private String password; public void show(){
System.out.println("姓名 :"+this.getName()+"密码: "+this.getPassword());
//加入异常
System.out.println(1/0);
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} }
package com.pb.aop;

import java.lang.reflect.Method;

import org.springframework.aop.ThrowsAdvice;
/**
* 异常增强类
* @author Administrator
*
*/
public class ThrowingAdded implements ThrowsAdvice {
//第一种写法
public void afterThrowing(Exception ex){
System.out.println("我是异常增强!,,没处理异常,有问题就找我"); }
//第二种写法
public void afterThrowing(Method arg0, Object[] arg1, Object arg2,Exception ex){
System.out.println("我是异常增强!,,没处理异常,有问题就找我"); } }

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:p="http://www.springframework.org/schema/p"
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.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">
<!--hello Bean -->
<bean id="hello" class="com.pb.entity.Hello" p:name="张三" p:password="123qwe"/>
<!--切入的Bean --> <!--异常增强切入的Bean -->
<bean id="throwAdd" class="com.pb.aop.ThrowingAdded"/>
<aop:config>
<!-- 切入点 -->
<aop:pointcut expression="execution(* com.pb.entity.Hello.*(..))" id="myPoint"/>
<!-- 关联异常增强切入类和切入点 -->
<aop:advisor advice-ref="throwAdd" pointcut-ref="myPoint"/>
</aop:config> </beans>

1.4、以上综合

<?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:p="http://www.springframework.org/schema/p"
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.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">
<!--hello Bean -->
<bean id="hello" class="com.pb.entity.Hello" p:name="张三" p:password="123qwe"/>
<!--切入的Bean -->
<!--前置增强切入的Bean -->
<bean id="beforeAdd" class="com.pb.aop.BeforeAdded"/> <!--后置增强切入的Bean -->
<bean id="afterAdd" class="com.pb.aop.AfterAdded"/>
<!--异常增强切入的Bean -->
<bean id="throwAdd" class="com.pb.aop.ThrowingAdded"/>
<aop:config>
<!-- 切入点 -->
<aop:pointcut expression="execution(* com.pb.entity.Hello.*(..))" id="myPoint"/>
<!-- 关联前置增强切入类和切入点 -->
<aop:advisor advice-ref="beforeAdd" pointcut-ref="myPoint"/>
<!-- 关联后置增强切入类和切入点 -->
<aop:advisor advice-ref="afterAdd" pointcut-ref="myPoint"/>
<!-- 关联异常增强切入类和切入点 -->
<aop:advisor advice-ref="throwAdd" pointcut-ref="myPoint"/>
</aop:config> </beans>

1.5、环绕增强

package com.pb.entity;
/**
* 实体类
*/
public class Hello {
private String name;
private String password; public void show(){
System.out.println("姓名 :"+this.getName()+"密码: "+this.getPassword());
//加入异常
System.out.println(1/0);
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} }

环绕增加Bean

package com.pb.aop;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
/**
* 环绕增强
* @author Administrator
*实现接口MethodIntercepor
*/
public class AroundAdded implements MethodInterceptor { @Override
public Object invoke(MethodInvocation arg0) throws Throwable {
Object result=null;
try {
System.out.println("环绕增强开始!");
result=arg0.proceed();
System.out.println("环绕增强结束!");
} catch (Exception e) {
System.out.println("环绕增强异常!");
}finally{
System.out.println("环绕增强最终增强!");
} return result;
} }

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:p="http://www.springframework.org/schema/p"
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.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">
<!-- 关联Bean Hello -->
<bean id="hello" class="com.pb.entity.Hello" p:name="张三" p:password="qewr"/>
<!--环绕增强的Bean -->
<bean id="aroundAdded" class="com.pb.aop.AroundAdded"></bean> <aop:config>
<!-- 切入点-->
<aop:pointcut expression="execution(* com.pb.entity.*.*(..))" id="myPoint"/>
<!--关联切入点 -->
<aop:advisor advice-ref="aroundAdded" pointcut-ref="myPoint"/>
</aop:config>
</beans>

二、注解方式

package com.pb.entity;
/**
* 实体类
*/
public class Hello {
private String name;
private String password; public void show(){
System.out.println("姓名 :"+this.getName()+"密码: "+this.getPassword());
//加入异常
System.out.println(1/0);
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} }

aop的类

package com.pb.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before; /**
* 注解的方式现实AOP
* @author Administrator
*
*/
@Aspect
public class MyAnnotionAOP { /*
* 前置
*/
@Before(value="execution(* com.pb.entity.*.*(..))")
public void before(JoinPoint point){ System.out.println("前置增强");
System.out.println(point.getClass());
}
/*
* 后置
*/
@AfterReturning(value="execution(* com.pb.entity.*.*(..))")
public void after(JoinPoint point){
System.out.println("后置增强");
//参数个数
System.out.println(point.getArgs().length);
}
/*
*异常
*/
@AfterThrowing(value="execution(* com.pb.entity.*.*(..))")
public void afterThrowing(JoinPoint point){
System.out.println("我是异常增强");
System.out.println(point.getSignature().getName());
}
/**
* 环绕
*/
@Around(value="execution(* com.pb.entity.*.*(..))")
public Object myAround(ProceedingJoinPoint point){
Object result=null; try {
System.out.println("环绕增强开始了");
System.out.println(point.getKind()+point.getArgs());
point.proceed();
System.out.println("环绕增强后置增强了");
System.out.println(point.getTarget()+""+point.getClass());
} catch (Throwable e) {
System.out.println("环绕增强,异常增强处理");
e.printStackTrace();
}finally{
System.out.println("环绕增强最终增强");
} return result; }
}

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:p="http://www.springframework.org/schema/p"
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.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">
<!-- 关联Bean Hello -->
<bean id="hello" class="com.pb.entity.Hello" p:name="张三" p:password="qewr"/>
<!-- 切入 类 -->
<bean id="myAnnAOP" class="com.pb.aop.MyAnnotionAOP" />
<!--开启自动代理 -->
<aop:aspectj-autoproxy/> </beans>

三、Schema方式(推荐)

Hello类同上

package com.pb.entity;
/**
* 实体类
*/
public class Hello {
private String name;
private String password; public void show(){
System.out.println("姓名 :"+this.getName()+"密码: "+this.getPassword());
//加入异常
System.out.println(1/0);
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} }

aop类

package com.pb.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint; /**
* SCHEMA方式切入类
*
* @author Administrator
*
*/
public class MySchemaAOP { /**
* 前置切入 可以有参数但是固定写法
*/
public void before(JoinPoint point){
System.out.println("这里是前置增强切入");
System.out.println(point.getKind()+point.getArgs().toString());
}
/**
* 后置切入
*/
public void after(JoinPoint point){
System.out.println("这里是后置增强切入");
System.out.println(point.getTarget()+point.getSignature().getName());
}
/**
* 异常切入
*/
public void myException(JoinPoint point){
System.out.println("这里是异常增强切入");
System.out.println(point.getSourceLocation());
}
/**
* 环绕增强切入
*/
public Object myAround(ProceedingJoinPoint point){
Object resut=null;
try {
System.out.println("环绕增强---前置增强");
resut=point.proceed();
System.out.println("环绕增强---后置增强");
} catch (Throwable e) {
System.out.println("环绕增强---异常增强");
e.printStackTrace();
}finally{
System.out.println("环绕增强---最终增强");
}
return resut;
}
}

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:p="http://www.springframework.org/schema/p"
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.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd">
<!-- 关联Bean Hello -->
<bean id="hello" class="com.pb.entity.Hello" p:name="张三" p:password="qewr"/>
<!-- 切入 类 -->
<bean id="mySchemaAOP" class="com.pb.aop.MySchemaAOP"/> <aop:config>
<!-- 切入点-->
<aop:pointcut expression="execution(* com.pb.entity.*.*(..))" id="myPoint"/>
<!--关联切入类、方法和切入点 -->
<aop:aspect ref="mySchemaAOP">
<!-- 切入 前置方法和切入点-->
<aop:before method="before" pointcut-ref="myPoint"/>
<!-- 切入 后置方法和切入点-->
<aop:after method="after" pointcut-ref="myPoint"/>
<!-- 切入 异常方法和切入点-->
<aop:after method="myException" pointcut-ref="myPoint"/>
<!-- 切入 环绕增加方法和切入点-->
<aop:around method="myAround" pointcut-ref="myPoint"/>
</aop:aspect>
</aop:config>
</beans>

Spring(六)AOP切入方式的更多相关文章

  1. 【Spring】AOP注解方式实现机制

    一.概述 二.@EnableAspectJAutoProxy 注解分析 三.分析AnnotationAwareAspectJAutoProxyCreator 四.执行流程 1. registerBea ...

  2. 【Spring】 AOP Base

    1. AOP概述 2. AOP的术语: 3. AOP底层原理 4. Spring 中的AOP 4.1 概述 4.2 分类 4.3 Spring的传统AOP 针对所有方法的增强:(不带有切点的切面) 带 ...

  3. Spring实现AOP的多种方式

    转载自:https://www.cnblogs.com/best/p/5736422.html:加了一些自己的注释: AOP(Aspect Oriented Programming)面向切面编程,通过 ...

  4. Spring之AOP原理、代码、使用详解(XML配置方式)

    Spring 的两大核心,一是IOC,另一个是AOP,本博客从原理.AOP代码以及AOP使用三个方向来讲AOP.先给出一张AOP相关的结构图,可以放大查看. 一.Spring AOP 接口设计 1.P ...

  5. 【学习】Spring 的 AOP :基于Annotation 的“零配置”方式

    转自:http://www.cnblogs.com/jbelial/archive/2012/07/20/2539123.html AOP(Aspect Orient Programming ) , ...

  6. spring(六):spring中AOP的基本使用

    AOP:面向切面编程[底层使用动态代理实现],就是在运行期间动态的将某段代码切入到方法的指定位置进行运行的编程方式 基本使用 使用AOP功能需要引入spring的aop以及aspects相关包 < ...

  7. Spring系列之aAOP AOP是什么?+xml方式实现aop+注解方式实现aop

    Spring系列之aop aop是什么?+xml方式实现aop+注解方式实现aop 什么是AOP? AOP为Aspect Oriented Programming 的缩写,意识为面向切面的编程,是通过 ...

  8. Spring实现AOP的4种方式

    了解AOP的相关术语:1.通知(Advice):通知定义了切面是什么以及何时使用.描述了切面要完成的工作和何时需要执行这个工作.2.连接点(Joinpoint):程序能够应用通知的一个“时机”,这些“ ...

  9. (转)Spring AOP实现方式(转)

    我们可以通过三种方式来使用Spring AOP,它们分别是:@Aspect-based(Annotation),Schema-based(XML),以及底层的Spring AOP API 底层的Spr ...

随机推荐

  1. Rendering Path

    Rendering Path:渲染路径 设置:1.Player Setting,2.Camera(会覆盖PlayerSetting中的设置) 选择:根据渲染内容和目标平台来选择合适的Rendering ...

  2. redis实现有序的消息队列

    redis是什么东西就不多说了,网上文章一搜一大堆. 首先来说一下我要实现的功能: 类似一个消息中转站吧,如果有人要发送消息,先将消息发到我这里来,然后我这边进行转发,为的就是有一个统一的管理和修改时 ...

  3. Hya.io – 基于 Web 的数字音频工作站

    Hya.io 是基于 Web 的音频应用程序,通过 Web MIDI ,音频合成器,音序以及大量的插件来支持硬件 MIDI .您可以添加插件到工作区,将其连接到路由音频,进行播放和实验. HYA 支持 ...

  4. python反射机制深入分析

    对编程语言比较熟悉的朋友,应该知道“反射”这个机制.Python作为一门动态语言,当然不会缺少这一重要功能.然而,在网络上却很少见到有详细或者深刻的剖析论文.下面结合一个web路由的实例来阐述pyth ...

  5. css-盒子模型

    css-盒子模型 一.padding(内边距)    元素的内边距在边框和内容区之间.控制该区域最简单的属性是 padding 属性.CSS padding 属性定义元素边框与元素内容之间的空白区域. ...

  6. ADO.NET---ExcuteScalar()方法复习

    ExcuteScalar(),返回的是查询结果的第一行第一列,返回值是object类型,一般用来查询表中有多少条数据,求最大值等 现在我们用ExcuteScalar()做个测试,需求:我要查询学生表里 ...

  7. Linq专题之匿名对象

    匿名对象是c#3.0的一个新的机制,使用new关键字和一个对象的初始化器,就能创建一个匿名对象.顾名思义,创建的时候这个对象是一个匿名类型的对象,没有具体的类型.说到匿名对象,那么我们前面讲过的var ...

  8. 重新想象 Windows 8 Store Apps (71) - 其它: C# 调用 C++

    [源码下载] 重新想象 Windows 8 Store Apps (71) - 其它: C# 调用 C++ 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之 其它 C# ...

  9. Discuz API的延伸

    作为社交平台来使用Discuz的话,UC_Server提供的那些数据接口是不够用的,我们还需要访问及操作用户的扩展数据. 基于UXF框架的rest_controller,很容易就可以实现API接口. ...

  10. mfc110.dll丢失,解决方法

    mfc110.dll下载_附文件使用方法 mfc110.dll是存放在windows系统中的一个重要dll文件,缺少它可能会造成部分软件或游戏无法正常运行.当系统提示“没有找到mfc110.dll”或 ...