转-springAOP基于XML配置文件方式

springAOP基于XML配置文件方式

<aop:config.../>包含:poincut,advisor,aspect元素,通过XML配置。

springAOP的具体加载步骤: 
   1、当spring容器启动的时候,加载了spring的配置文件 
   2、为配置文件中所有的bean创建对象 
   3、spring容器会解析aop:config的配置  
       1、解析切入点表达式,用切入点表达式和纳入spring容器中的bean做匹配 
            如果匹配成功,则会为该bean创建代理对象,代理对象的方法=目标方法+通知 
            如果匹配不成功,不会创建代理对象 
   4、在客户端利用context.getBean获取对象时,如果该对象有代理对象则返回代理对象,如果代理对象,则返回目标对象 
说明:如果目标类没有实现接口,则spring容器会采用cglib的方式产生代理对象,如果实现了接口,会采用jdk的方式 

MyAspect.java 

package net.csdn.www.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.core.annotation.Order;

public class MyAspect {
	
	public void authority(JoinPoint jp) {
		System.out.println("被代理方法名字:"+jp.getSignature().getName());
		System.out.println("被代理方法参数:"+jp.getArgs());
		System.out.println("被代理对象:"+jp.getTarget());
		System.out.println("现在调用的是权限验证");
          
	}

	
	public  void log(JoinPoint jp,Object rvt){
		System.out.println("被代理方法名字:"+jp.getSignature().getName());
		System.out.println("被代理方法参数:"+jp.getArgs());
		System.out.println("被代理对象:"+jp.getTarget());
		System.out.println("被代理对象的返回值"+rvt);
		System.out.println("现在调用的是日志管理");
	}
	

	public Object processTx(ProceedingJoinPoint pjp) throws Throwable{
		System.out.println("现在调用的是事务开启");		
		//得到业务方法的参数
		Object[] args=pjp.getArgs();
		System.out.println("业务方法的参数:"+args);
		//被代理对象的业务方法
		Object result=pjp.proceed(args);
		System.out.println("现在调用的是事务提交或回滚");
	    return result;
	}
	
	
	public void release(){
		System.out.println("资源已经被释放!");		
	}
}
UserDao.java
package net.csdn.www.dao;

import java.util.Date;

import org.springframework.stereotype.Component;

@Component
public class UserDao {
    public String save(String name){
    	System.out.println(name+"被保存");
    	return "success";
    }
}
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:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
	http://www.springframework.org/schema/aop 
	http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context-4.0.xsd">
	<!-- 切面 -->
	<bean id="myAspect" class="net.csdn.www.aop.MyAspect"></bean>
	<bean id="userDao" class="net.csdn.www.dao.UserDao"></bean>

	<!-- 切入点 -->
	<aop:config>
		<aop:aspect id="asp1" ref="myAspect">
			<aop:before method="authority" pointcut="execution(* net.csdn.www.dao.*.*(..))" />
			<aop:after method="release" pointcut="execution(* net.csdn.www.dao.*.*(..))" />
			<aop:after-returning method="log"
				pointcut="execution(* net.csdn.www.dao.*.*(..))" returning="rvt" />
			<aop:around method="processTx" pointcut="execution(* net.csdn.www.dao.*.*(..))" />
		</aop:aspect>
	</aop:config>
</beans>

测试类Test.java

package net.csdn.www.text;

import java.util.Date;

import net.csdn.www.dao.UserDao;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Test {
   public static void main(String[] args) {
	ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
	UserDao  userDao=(UserDao) context.getBean("userDao");
     userDao.save("csdnccccccc");
   }
}

在UserDao类中声明一个带参数的方法

public String eat(Date date,String food){
    	System.out.println(date+"先准备:"+food);
    	return "好吃";
    }
MyAspect1.java 
package net.csdn.www.aop;

import java.util.Date;

import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.core.annotation.Order;

public class MyAspect1 {
	
	//注意:增加参数匹配,参数位置类型要和被代理方法一致	
     public void access(Date date, String food,Object rvt){
    	 System.out.println(date+"吃"+food+"-----");
    	 System.out.println("返回值为:"+rvt);
     }
	
}

在XML文件中配置切面

<bean id="myAspect1" class="net.csdn.www.aop.MyAspect1"></bean>

	<!-- 切入点 -->
	<aop:config>
		<aop:aspect id="asp1" ref="myAspect">
			<aop:before method="authority" pointcut="execution(* net.csdn.www.dao.*.*(..))" />
			<aop:after method="release" pointcut="execution(* net.csdn.www.dao.*.*(..))" />
			<aop:after-returning method="log"
				pointcut="execution(* net.csdn.www.dao.*.*(..))" returning="rvt" />
			<aop:around method="processTx" pointcut="execution(* net.csdn.www.dao.*.*(..))" />
		</aop:aspect>
		 <aop:aspect id="asp2" ref="myAspect1" order="1">
			<aop:after-returning method="access"
				pointcut="execution(* net.csdn.www.dao.*.*(..)) and args(date,food)"
				returning="rvt" />
		</aop:aspect>
	</aop:config>
调用userDao.eat(new Date(), "xiaochi");

设置切入点 

<aop:pointcut expression="execution(* csdn.dao.*.*(..))" id="mypoint"/> 
id 切入点的标示名 
expression 切入点表达式 
<aop:after method="release" pointcut-ref="mypoint"/> 

把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:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
	http://www.springframework.org/schema/aop 
	http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context-4.0.xsd">
	<!-- 切面 -->
	<bean id="myAspect" class="net.csdn.www.aop.MyAspect"></bean>
	<bean id="userDao" class="net.csdn.www.dao.UserDao"></bean>
	<bean id="myAspect1" class="net.csdn.www.aop.MyAspect1"></bean>

	<!-- 切入点 -->
	<aop:config>
	    <aop:pointcut expression="execution(* net.csdn.www.dao.*.*(..)) and args(date,food)" id="xxx"/> 
		<aop:aspect ref="myAspect1"> <aop:after-returning method="access" 
			pointcut-ref="xxx" returning="rvt"/> </aop:aspect> 
	        <aop:aspect id="asp1" ref="myAspect" order="2">
			<aop:before method="authority" pointcut="execution(* net.csdn.www.dao.*.*(..))" />
			<aop:after method="release" pointcut="execution(* net.csdn.www.dao.*.*(..))" />
			<aop:after-returning method="log"
				pointcut="execution(* net.csdn.www.dao.*.*(..))" returning="rvt" />
			<aop:around method="processTx" pointcut="execution(* net.csdn.www.dao.*.*(..))" />
		</aop:aspect>
	</aop:config>
</beans>

 

posted on 2016-10-28 14:47  anruy  阅读(411)  评论(0编辑  收藏  举报

导航