Spring(十):Spring AOP XML

Spring AOP的XML方式和注解方式大同小异,除了把配置信息移到XML之外并没有太大区别,我们先贴上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.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.3.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">
	<context:annotation-config/>
    <context:component-scan base-package="com.castiel"/>
    
    <!-- 初始化logInterceptor -->
    <bean id="logInterceptor" class="com.castiel.aop.LogInterceptor"></bean>
    <aop:config>
    	<!-- 全局 -->
    	<aop:pointcut id="DAOPointcut" expression="execution(public * com.castiel.dao..*.save(..))"/>
    	<aop:pointcut id="servicePointcut" expression="execution(public * com.castiel.service..*.add(..))"/>
    	
    	<aop:aspect id="logAspect_DAO" ref="logInterceptor">
    		<!-- 当前类示例 -->
    		<!-- <aop:pointcut expression="execution(public * com.castiel.service..*.add(..))" id="servicePointcut"/> -->
    		<aop:before method="before" pointcut-ref="DAOPointcut"/>
    		<aop:after-returning method="afterReturning" pointcut-ref="DAOPointcut"/>
    		<aop:after-throwing method="afterThrowing" pointcut-ref="DAOPointcut"/>
    		<aop:after method="afterMethod" pointcut-ref="DAOPointcut"/>
    		<aop:around method="aroundMethod" pointcut-ref="DAOPointcut"/>
    	</aop:aspect>
    	
    	<!-- 示例第一种写法 -->
    	<!-- <aop:aspect id="logAspect_service" ref="logInterceptor">
    		<aop:before method="userServiceAddBefore" pointcut-ref="servicePointcut"/>
    	</aop:aspect> -->
    	
    	<!-- 示例第二种写法,不用pointcut-ref,直接指定 pointcut -->
    	<aop:aspect id="logAspect_service" ref="logInterceptor">
    		<aop:before method="userServiceAddBefore" pointcut="execution(public * com.castiel.service..*.add(..))"/>
    	</aop:aspect>
    </aop:config>
</beans>

再贴上'LogInterceptor'这个类的代码:

package com.castiel.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
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;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

public class LogInterceptor {
	/**
	 * 如果类实现了接口,例如'UserDAOImpl'实现了'UserDAO',可以用这种方式生成代理,但是
	 * 如果像'UserService'这种没有实现接口的类,必须要用直接操作二进制代码的类库来产生代理,例如cglib
	 * 加入cglib的jar包即可解决这种问题,但是Spring 4.0之后已经封装了cglib(在spring-core-4.x.x.RELEASE.jar中),所以4.0之后的Spring不用导入
	 * 此包
	 */
	
	public void before(){
		System.out.println("before...");
	}
	
	public void afterReturning(){
		System.out.println("after returning...");
	}
	
	public void afterThrowing(){
		System.out.println("after throwing...");
	}
	
	public void afterMethod(){
		System.out.println("finally...");
	}
	
	public void aroundMethod(ProceedingJoinPoint pdp) throws Throwable{
		System.out.println("around start...");
		pdp.proceed();
		System.out.println("around end...");
	}
	
	
	public void userServiceAddBefore(){
		System.out.println("userService add before...");
	}
}

从上面的beans.xml配置文件可以看出:

(1):首先,我们初始化了一个id为'logInterceptor'的bean,这里就是我们的剖面类,由Spring容器管理。

(2):由于我们很多方法使用的是同一'expression',所以我们用'<aop:pointcut/>'定义了两个全局的pointcut,避免在下面重复的配置'expression'属性值,直接使用pointcut-ref='xxx'即可指向全局pointcut。

(3):我们配置了两个<aop:ascept>切面,一个是针对'UserDAOImpl'的save()方法的,一个是针对'UserService'的Add方法的,因为Spring生成动态代理对象时,不实现接口的类和实现接口的类生成方式不一样,不实现接口的类是直接用'cglib'这个jar包来操作二进制数来完成的,也由于'UserDAOImpl'实现了'UserDAO'接口,'UserService'没有实现任何接口,所以'UserService'是主要用来测试'cglib'生成代理对象的。

(4):在<aop:aspect>标签中,我们配置id属性及ref属性,ref属性指向我们配置的剖面类'logInterceptor',这样Spring就能根据上面的'expression'属性拦截并且执行拦截器的一系列方法,和注解方式一样,也提供<aop:before/>、<aop:after/>、<aop:around>等配置,来支持不同的业务场景。


配置完毕,我们就可以编写一个测试类来测试一下是否配置正确了,代码如下:

package com.castiel.service;

import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.castiel.model.User;
import com.castiel.service.UserService;

public class UserServiceTest {
	/**测试类生成方式**/
	/**
	 * 1.选中src下com.castiel.service包中的UserService
	 * 2.右键new jUnit Test Case
	 * 3.点击NEXT
	 * 4.选择需要测试的方法
	 * 5.点击Finish
	 * 6.把生成出来的*Test.java文件拖到test下com.castiel.service包中
	 * @throws Exception 
	 */
	@Test
	public void testAdd() throws Exception {
		ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
		System.out.println("******************************");
		UserService userService = (UserService)ctx.getBean("userService");
		System.out.println(userService.getClass());
		userService.add(new User());
		/**
		 * 在web框架中,一般自动管理生命周期,自动调用destroy方法,这里我们模拟一下
		 * 注意:ApplicationContext中没有destroy方法,所以我们要调用ApplicationContext的实现类
		 * ClassPathXmlApplicationContext中的destroy方法
		 */
		/**
		 * 如果我们设置bean的scope为'prototype',是不会执行destroy方法,spring无法自动管理多例的整个生命周期
		 * 
		 * bean的构造函数执行完毕才会执行init方法
		 */
		ctx.destroy();
	}

}

运行结果如下:



最后说两句,对于Spring AOP的配置,个人建议使用XML方式,其他Service层、Dao层等使用注解方式,这样即可提高效率也使项目的易扩展、易维护性大大提升!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值