深入探索spring技术内幕(七): 配置Spring AOP面向切面编程

时间:2014-05-22 10:26:06   收藏:0   阅读:337

一. AOP一些概念

Aspect( 切面 ): 指横切性关注点的抽象即为切面, 它与类相似, 只是两者的关注点不一样, 类是对物体特征的抽象, 而切面横切性关注点的抽象.
joinpoint( 连接点 ): 指那些被拦截到的点. 在spring中, 这些点指的是方法, 因为spring只支持方法类型的连接点, 实际上joinpoint还可以是field或类构造器)
Pointcut( 切入点 ): 指我们要对那些joinpoint进行拦截的定义.
Advice( 通知 ): 指拦截到joinpoint之后所要做的事情就是通知. 通知分为前置通知、后置通知、异常通知、最终通知、环绕通知.
Target( 目标对象 ): 代理的目标对象。
Weave( 织入 ): 指将aspects应用到target对象并导致proxy对象创建的过程称为织入.
Introduction( 引入 ): 在不修改类代码的前提下, Introduction可以在运行期为类动态地添加一些方法或Field.


二. 基于注解声明切面 

使用注解配置aop会是代码非常简洁:

① 引入Jar包

aspectjrt.jar

aspectjweaver.jar

cglib-nodep-2.1_3.jar

commons-logging.jar

spring.jar


② 业务类: PersonServiceImpl

@Service("personServiceImpl") 
public class PersonServiceImpl {

	public String save(String name) {
		// throw new RuntimeException("我爱例外");
		System.out.println("我是save()方法");
		return "success";
	}

}

③ AOP切面类

@Aspect
@Component
public class MyInterceptor {

	@SuppressWarnings("unused")
	@Pointcut("execution (* com.zdp.service.impl.PersonServiceImpl.*(..))")   // 拦截PersonServiceImpl类下的所有方法
	private void anyMethod() {
	} // 声明一个切入点

	@Before("anyMethod() && args(name)")
	public void doAccessCheck(String name) {
		System.out.println("前置通知:" + name);
	}

	@AfterReturning(pointcut = "anyMethod()", returning = "result")
	public void doAfterReturning(String result) {
		System.out.println("后置通知:" + result);
	}

	@After("anyMethod()")
	public void doAfter() {
		System.out.println("最终通知");
	}

	@AfterThrowing(pointcut = "anyMethod()", throwing = "e")
	public void doAfterThrowing(Exception e) {
		System.out.println("例外通知:" + e);
	}

	@Around("anyMethod()")
	public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable {
		System.out.println("进入方法");
		Object result = pjp.proceed();
		System.out.println("退出方法");
		return result;
	}
}

④ 配置文件beans.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.zdp"/>
        <aop:aspectj-autoproxy/> 
</beans>

⑤ 测试类

public class SpringAOPTest {
	@Test
	public void testSave() {
		ApplicationContext cxt = new ClassPathXmlApplicationContext("beans.xml");
		PersonServiceImpl psi = (PersonServiceImpl) cxt.getBean("personServiceImpl");
		psi.save("zhangsan");
	}
}

测试结果:

        前置通知:zhangsan
        进入方法
        我是save()方法
        后置通知:success
        最终通知
        退出方法


注意: 以上只是一个测试AOP注解, 没有写接口PersonService, 所以无法使用动态代理, 所以要引入CGLIB包.


三. 基于配置声明切面 

① 引入jar包

aspectjrt.jar

aspectjweaver.jar

cglib-nodep-2.1_3.jar

commons-logging.jar

spring.jar


② 业务类: PersonServiceImpl

public class PersonServiceImpl {
	
	public String getPersonName(Integer id) {
		System.out.println("我是getPersonName()方法");
		return "xxx";
	}
	
}

③ AOP切面类

public class MyInterceptor {
	public void doAccessCheck() {
		System.out.println("前置通知");
	}

	public void doAfterReturning() {
		System.out.println("后置通知");
	}

	public void doAfter() {
		System.out.println("最终通知");
	}

	public void doAfterThrowing() {
		System.out.println("例外通知");
	}

	public Object doBasicProfiling(ProceedingJoinPoint pjp) throws Throwable {
		System.out.println("进入方法");
		Object result = pjp.proceed();
		System.out.println("退出方法");
		return result;
	}
}

④ 配置文件beans.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">
        <aop:aspectj-autoproxy/> 
        <bean id="personServiceImpl" class="com.zdp.service.impl.PersonServiceImpl" />
        <bean id="aspetbean" class="com.zdp.spring.MyInterceptor" />
        <aop:config>
        	<aop:aspect id="asp" ref="aspetbean">
        		<aop:pointcut id="mycut" expression="execution(* com.zdp.service..*.*(..))"/>
        		<aop:before pointcut-ref="mycut" method="doAccessCheck"/>
        		<aop:after-returning pointcut-ref="mycut" method="doAfterReturning"/>
			  	<aop:after-throwing pointcut-ref="mycut" method="doAfterThrowing"/>
			  	<aop:after pointcut-ref="mycut" method="doAfter"/>
			  	<aop:around pointcut-ref="mycut" method="doBasicProfiling"/>
        	</aop:aspect>
        </aop:config>
</beans>

⑤ 测试类

public class SpringAOPTest {

	@Test 
	public void interceptorTest(){
		ApplicationContext cxt = new ClassPathXmlApplicationContext("beans.xml");
		PersonServiceImpl psi = (PersonServiceImpl)cxt.getBean("personServiceImpl");
		psi.getPersonName(1);
	}
}

测试结果:

        前置通知
        进入方法
        我是getPersonName()方法
        后置通知
        最终通知
        退出方法


四. 切入点表达式

表达式格式:

* execution(
                       * 1. modifiers-pattern?         修饰符模式
                                                                         * 方法使用的修饰符
                       * 2. ret-type-pattern            返回类型模式(必须) 
                                                                         * 返回类型模式决定了方法的返回类型必须一次匹配一个连接点
                                                                         * 你会使用的最频繁的返回类型模式是*,它代表匹配任意的返回类型
                       * 3. declaring-type-pattern? 声明类型模式
                                                                         * 方法所在类的全路径
                       * 4. name-pattern                  名字模式(必须)
                                                                         * 名字模式匹配的是方法名,你可以使用*通配符作为所有或者部分命名模式
                                                                            saveUser  -- 方法必须是saveUser
                                                                            save* -- 方法以save开头
                       * 5. param-pattern                 参数模式(必须)
                                                                         * ()匹配一个不接受任意参数的方法
                                                                         * (..)匹配一个接受任意数量参数的方法(零个或者更多)
                                                                         * (*)匹配一个接受任何类型的参数的方法
                                                                         * 模式(*,String)匹配一个接受两个参数的方法,第一个可以是任意类型,第二个则必须是String类型
                       * 6. throws-pattern?              异常模式
)


表达式例子:

1. 任意公共方法的执行: execution(public * *(..))
2. 任何一个名字以“set”开始的方法的执行: execution(* set*(..))
3. AccountService接口定义的任意方法的执行:execution(* com.xyz.service.AccountService.*(..))
4. 在service包中定义的任意方法的执行:execution(* com.xyz.service.*.*(..))
5. 在service包或其子包中定义的任意方法的执行:execution(* com.xyz.service..*.*(..))



深入探索spring技术内幕(七): 配置Spring AOP面向切面编程,布布扣,bubuko.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!