Spring AOP xml配置方法

本文介绍了一种软件设计模式——面向切面编程(AOP),并通过具体示例展示了如何使用Spring框架实现AOP的各种通知类型,包括前置通知、后置通知、环绕通知等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

面向切面编程


所有支持<aop:aspect>标签的功能只支持单例模式。

配置类截图

package com.test.xml;

import org.aspectj.lang.ProceedingJoinPoint;

public class MoocAspect {
 public void before(){
  System.out.println("*********before********");
 }
 public void after(){
  System.out.println("*********after*********");
 }
 public void afterReturn(){
  System.out.println("******afterReturn*****");
 }
 
 /*
  * <aop:around method="around" pointcut-ref="bizservice3"/>
  * 在执行around时候必须要有ProceedingJoinPoint类型的参数,并且返回一个Object值。
  *
  */
 public Object around(ProceedingJoinPoint pjp) {
  Object object = null;
  try {
    System.out.println("******around1*****");
    object = pjp.proceed();
    System.out.println("******around2*****");
   
  } catch (Throwable e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  return object;
 }
 public void afterThrowing(){
  System.out.println("**********afterThrowing*********");
 }
 
 public Object aroundParameter(ProceedingJoinPoint pjp,String name,String sex,int age){
  Object o = null;
  try {
   System.out.println("aroundParameter1:"+name+" "+sex+"  "+age);
   o = pjp.proceed();
   System.out.println("aroundParameter2:"+name+" "+sex+"  "+age);
  } catch (Throwable e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  return o;
  
 }

}



业务类:

package com.test.xml;

import java.io.File;

public class AspectBiz {
 public void bizservice(){
  System.out.println("*********执行bizservice()************");
 }
 
 public void bizreturn(){
  System.out.println("**********执行bizreturn()**************");
 }
 
 public void bizaround(){
  System.out.println("**********执行bizaround()**************");
 }
 public void bizafterthrow() throws Exception{
  System.out.println("**********执行bizAfterthrow()**************");
  File f =null;
  f.getName();
  
 }
 /*
  * <aop:pointcut id="bizservice5"  expression="execution(* com.test.xml.AspectBiz.*aroundParameter(String,String,int)) and args(name,sex,age)" />
  * 规定参数名字必须是name,sex,age
  */
 
 public void bizaroundParameter(String name,String sex,int age){
  System.out.println("*********bizaroundPararmeter*********");
 }

}

测试类:package com.test.xml;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;
import com.junit.test.UnitTestBase;
/*
 * 测试类
 */
@RunWith(BlockJUnit4ClassRunner.class)
public class TestXml extends UnitTestBase {
 public TestXml(){
  super("classpath:applicationContext.xml");
  //super("classpath:round.xml");
 }
 @Test
 public void test1() throws Exception {
  AspectBiz ab = super.getBean("aspectBiz");
//  ab.bizservice();
//  //从慕课网(用的spring4)看到的视频是after应该在afterreturn后执行,实验结果(用的spring3)是afterreturn在after之后  
//  ab.bizreturn();
//  ab.bizaround();
//  ab.bizafterthrow();
//  ab.bizaroundParameter("zhangfei", "man", 23);
  
 }

}

UnitTestBase类:

package com.junit.test;

import org.junit.After;
import org.junit.Before;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class UnitTestBase {
 private ClassPathXmlApplicationContext context;
 private String stringXmlPath;
 public UnitTestBase() {};
 public UnitTestBase(String stringXmlPath) {
  this.stringXmlPath = stringXmlPath;
 };
 @Before
 public void before(){
  
  if(stringXmlPath==null||stringXmlPath.equals("")){
   stringXmlPath = "classPath*:spring-*.xml";
  }
  try{
   context = new ClassPathXmlApplicationContext(stringXmlPath.split("[,\\s]+"));
   context.start();
  }catch(Exception e){
   e.printStackTrace();
  }
 }
 @After
 public void after(){
  context.destroy();
  
 }
 
 @SuppressWarnings("unchecked")
 protected <T extends Object>T getBean(String beanId){
  return (T)context.getBean(beanId);
 }
 
 protected <T extends Object>T getBean(Class<T> clazz){
  return (T)context.getBean(clazz);
 }

}


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.0.xsd
 http://www.springframework.org/schema/aop
 http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
 
 <bean id="mookAspect" class="com.test.xml.MoocAspect"/>
 <bean id="aspectBiz" class="com.test.xml.AspectBiz"/>
 
 <aop:config>
 <aop:aspect id="mookAspectAop" ref="mookAspect">
  <aop:pointcut id="bizservice1"  expression="execution(public * *(..))" />
 <aop:pointcut id="bizservice2"  expression="execution(* com.test.xml.AspectBiz.*return(..))" />
 <aop:pointcut id="bizservice3"  expression="execution(* com.test.xml.AspectBiz.*around(..))" />
 <aop:pointcut id="bizservice4"  expression="execution(* com.test.xml.AspectBiz.*afterthrow(..))" />
 <aop:pointcut id="bizservice11"  expression="execution(* com.test.xml.AspectBiz.*aroundParameter(String,String,int)) and args(name,sex,age)"/>
 <aop:before method="before" pointcut-ref="bizservice1"/>
 <aop:after method="after" pointcut-ref="bizservice1"/>
 <aop:after-returning method="afterReturn" pointcut-ref="bizservice2"/>
 
 <aop:after-throwing method="afterThrowing" pointcut-ref="bizservice4"/> 
 <aop:around method="around" pointcut-ref="bizservice3"/>
 <aop:around method="aroundParameter" pointcut-ref="bizservice11"/>

 
 </aop:aspect>
 </aop:config>


</beans>

其中applicationContext.xml中的<aop:around method="around" pointcut-ref="bizservice3"/>
 <aop:around method="aroundParameter" pointcut-ref="bizservice11"/>这两条配置需要挨着,否则实验测试时候总是包.xml文件初始化错误,原因未找到





再注意<aop:around method="around" pointcut-ref=""/>标签的配置,对应的method必须有一个ProceedingJoinPoint类型的参数,然后次参数对象的proceed()方法。




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值