您的位置:首页 > 编程语言 > Java开发

Spring中的AOP简述

2015-06-20 13:31 579 查看
AOP是Spring的两大重要思想之一:切面编程思想。

切面编程的原理:

由于组件是在运行期间组装、调用的,因此Spring既可以执行完A组件后执行B组件,也可以执行完A组件后执行B组件之前执行组件C,也就是把C组件插入到A组件和B组件之间

如果把A、B、C看成切面的话,就成了AOP,面向切面编程。面向切面编程的思想就是在执行某些代码之前执行另外一些代码,使程序更灵活,扩展性更好,可以随便地添加,删除某些功能。

使用拦截器实例:

接口

package com.itf;

public interface IAopService {

public void withAOP() throws Exception;

public void withoutAOP()throws Exception;

}

接口实现

package com.impl;

import javax.security.auth.login.AccountException;

import com.itf.IAopService;

public class AopServiceImpl implements IAopService {

private String name;

@Override

public void withAOP() throws Exception {

// TODO Auto-generated method stub

System.out.println("有AOP的函数运行,name:"+this.name);

if(name.trim().length()==0){

throw new AccountException("name属性不能为空");

}

}

@Override

public void withoutAOP() throws Exception {

// TODO Auto-generated method stub

System.out.println("没有AOP的函数运行");

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

}

拦截器实现

package com.advice;

import java.lang.reflect.Method;

import org.springframework.aop.AfterReturningAdvice;

public class MethodAfterInterceptor implements AfterReturningAdvice {

@Override

public void afterReturning(Object value, Method method, Object[] arg2,

Object arg3) throws Throwable {

// TODO Auto-generated method stub

System.out.println("方法"+method.getName()+"运行完毕,返回值为:"+value);

}

}

package com.advice;

import java.lang.reflect.Method;

import org.springframework.aop.MethodBeforeAdvice;

import com.impl.AopServiceImpl;

public class MethodBeforeInterceptor implements MethodBeforeAdvice {

/**

* 方法前拦截器

*/

@Override

public void before(Method arg0, Object[] arg1, Object instance)

throws Throwable {

// TODO Auto-generated method stub

System.out.println("即将要执行的方法:"+arg0.getName());

if(instance instanceof AopServiceImpl){

String name = ((AopServiceImpl) instance).getName();

if(name == null){

throw new NullPointerException("name属性不能为空null");

}

}

}

}

package com.advice;

import java.lang.reflect.Method;

import javax.security.auth.login.AccountException;

import org.springframework.aop.ThrowsAdvice;

public class ThrowsInterceptor implements ThrowsAdvice {

public void afterThrowing(Method method,Object[] args[],Object instance,AccountException ex){

System.out.println("方法"+method.getName()+"抛出了异常:"+ex);

}

public void afterThrowing(NullPointerException ex){

System.out.println("抛出了异常:"+ex);

}

}

配置拦截器

<?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"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
<bean id="methodBeforeInterceptor" class="com.advice.MethodBeforeInterceptor"></bean>

<bean id="methodAfterInterceptor" class="com.advice.MethodAfterInterceptor"></bean>

<bean id="thorwsInterceptor" class="com.advice.ThrowsInterceptor"></bean>

<!-- 配置方法前拦截器 -->

<bean id="aopMethodBeforeInterceptor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">

<property name="advice" ref="methodBeforeInterceptor"></property>

<property name="mappedName" value="withAOP"></property>

</bean>

<!-- 配置运行后拦截器 -->

<bean id="aopMethodAfterInterceptor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">

<property name="advice" ref="methodAfterInterceptor"></property>

<property name="mappedName" value="withAOP"></property>

</bean>

<!-- 配置异常拦截器 -->

<bean id="aopThrowsInterceptor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">

<property name="advice" ref="thorwsInterceptor"></property>

<property name="mappedName" value="withAOP"></property>

</bean>

<!-- 将Service对象,安装到ProxyFactoryBeen对象中 -->

<bean id="aopService" class="org.springframework.aop.framework.ProxyFactoryBean">

<property name="interceptorNames">

<list>

<value>aopMethodBeforeInterceptor</value>

<value>aopMethodAfterInterceptor</value>

<value>aopThrowsInterceptor</value>

</list>

</property>

<!-- 被拦截对象 -->

<property name="target">

<bean class="com.impl.AopServiceImpl">

<property name="name" value="weifei"></property>

</bean>

</property>

</bean>

</beans>

测试方法

package com.testng;

import static org.junit.Assert.*;

import org.springframework.beans.factory.xml.XmlBeanFactory;

import org.springframework.core.io.ClassPathResource;

import com.itf.IAopService;

public class Test {

@org.junit.Test

public void test() throws Exception {

XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));

IAopService hello = (IAopService) factory.getBean("aopService");

hello.withAOP();

factory.destroySingletons();

}

}

拦截器与Filter的区别

适用范围不同:Filter是Servlet规范中规定的,只能用于Web程序中。而拦截器既可以用于web程序,也可以用于Application,Swing程序中。

规范不同:Filter是Servlet容器支持的,拦截器是Spring框架支持的

使用的资源不同:拦截器能够使用spring中的一切资源,对象。Filter则不能。

深度不同:Filter在且只在Servlet前后起作用。而拦截器能够深入到方法的前后,异常抛出前后灯,因此拦截器的使用具有更大的弹性。所以在Spring中优先使用拦截器

AOP的代理模式

代理模式是Spring中常用的设计模式。Spring中提供了几个常用的代理类,例如普通代理类,事务代理类

1 ProxyFactoryBeen代理工厂对象

2 TransactionProxyFactoryBeen事务代理工厂对象
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: