spring学习笔记8--使用spring进行面向切面的(AOP)编程(2)XML配置方式


前言:最近在复习spring特记录于此,欢迎大家交流指正 QQ:767872620

 

主要是对xml文件中的<aop:config>标签配置,这样原来的代理类(拦截类)所有的注解就个全部干掉了!

例子如下:

继续“万恶”的person相关类

public interface PersonService {
	public void save(String name);
	public void update(String name,Integer id);
	public String getPersonName();
	public String getPersonName(Integer id);
}

person实现类:
 

package cn.itcast.service.imp;

import cn.itcast.service.PersonService;

public class PersonServiceBean implements PersonService {


	public String getPersonName() {
		System.out.println("我是getPersonName()");
		return "xxx";
	}

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


	public void save(String name) {
		throw new RuntimeException("我爱意外"); //用于测试意外通知
//		System.out.println("我是save()");		
	}


	public void update(String name, Integer id) {
		System.out.println("我是update()");		
	}

}

拦截类:

这是一个纯粹的类,一个不矫揉造作的类,一个没有认识注解修饰的类

/**
 * 通过xml配置aop切面
 * @author Mars
 *是一个纯粹的类,没有一点注解的修饰
 */

public class MyInterceptorXml {
	

	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{
		//if(){//判断用户是否有权限
		System.out.println("进入环绕通知方法");
		Object result = pjp.proceed();
		System.out.println("退出环绕通知方法");
		//}
		return result;
	}
} 

最为关键的就是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.0.xsd
 http://www.springframework.org/schema/aop
 http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">

 
 <!-- xml配置方式实现aop -->
 <!--1.将各类配置入bean,将类交给spring管理  -->

 <bean id="PersonService" class="cn.itcast.service.imp.PersonServiceBean"></bean>
 <bean id="myInterceptorXML" class="cn.itcast.service.MyInterceptorXml"></bean>
 <!--2.设置切入点  -->
 <aop:config>
  <!--(1).配置切面  -->
  <aop:aspect id="asp" ref="myInterceptorXML">
   <!--(2).配置切入点  -->
   <aop:pointcut id="mycut" expression="execution(* cn.itcast.service.imp.PersonServiceBean.*(..))" ></aop:pointcut>
   <!--(3).配置前置通知  -->             
   <aop:before pointcut-ref="mycut" method="doAccessCheck"></aop:before>
   <!--(4).配置后置通知  -->  
   <aop:after-returning pointcut-ref="mycut" method="doAfterReturning"/>
   <!--(5).配置例外通知  -->  
   <aop:after-throwing pointcut-ref="mycut" method="doAfterThrowing"/>
   <!--(6).配置最终通知  -->  
   <aop:after  pointcut-ref="mycut"  method="doAfter"/>
   <!--(7).配置环绕通知  -->
   <aop:around  pointcut-ref="mycut" method="doBasicProfiling"/>
  </aop:aspect>
 </aop:config>
</beans>

还有简单粗暴的测试类:

package junit.test;


import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.itcast.service.PersonService;


public class springXmlAopTest {
	@BeforeClass
	public static void setUpBeforeClass() throws Exception{
		
	}
	@Test public void interceptorTest(){
		ApplicationContext cxt = new ClassPathXmlApplicationContext("applicationContext.xml");
		PersonService  personservice =  (PersonService)cxt.getBean("PersonService");
		personservice.save("xxx");
//		personservice.getPersonName(2);
	}
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值