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

Spring AOP xml配置方法

2017-03-23 15:24 381 查看
面向切面编程

所有支持<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()方法。




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