首页 > 编程语言 > 详细

Spring面向切面编程(AOP)

时间:2015-06-20 19:40:32      阅读:341      评论:0      收藏:0      [点我收藏+]

技术分享

技术分享

技术分享


本质上就是代理,只是做成了配置文件形式。


基类

package _10SpringAOP;
public class Person {
	public int id;
	public String name;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
}
dao接口

package _10SpringAOP;
public interface personDao {
	public String savePerson(Person person);
}
dao接口实现

package _10SpringAOP;
public class PersonDaoImpl implements personDao {
	@Override
	public String savePerson(Person person) {
		// TODO Auto-generated method stub
		//测试springAop的错误处理
//		int a = 1/0;
		System.out.println("save person");
		return "savePerson";
	}
}
切面类

package _10SpringAOP;
import org.aspectj.lang.ProceedingJoinPoint;
public class MyTransaction {
	public void beginTransaction(){
		System.out.println("beginTransaction");
	}
	public void commit(Object var){
		System.out.println("commit");
		System.out.println("var is " + var);
	}
	public void finalMethod(){
		System.out.println("the finally method");
	}
	public void throwMethod(Throwable ex){
		System.out.println("ex is " + ex.toString());
	}
	public void aroundMethod(ProceedingJoinPoint point) throws Throwable{
		//获取目标方法的名称
		System.out.println("method is " + point.getSignature().getName());
		//调用目标方法
//		point.proceed();
		System.out.println("end");
	}
}
测试类

package _10SpringAOP;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class test {
	//	beginTransaction
	//	method is savePerson
	//	save person
	//	commit
	//	var is savePerson
	//	the finally method
	//	end
		
	//aroundMethod方法里面不调用的时候
	//	beginTransaction
	//	method is savePerson
	//	end
	//没有调用的话,那么目标方法和后面的后置方法等全部不调用
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ApplicationContext context = 
			new ClassPathXmlApplicationContext("applicationContext.xml");
		//如果是PersonDao,有实现接口   产生的是代理对象
		//如果是PersonDaoImpl,无实现接口   产生的是目标对象
		personDao persondao =  (_10SpringAOP.personDao) context.getBean("personDao");
		persondao.savePerson(new Person());
	}
}
配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:aop="http://www.springframework.org/schema/aop" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
	<!-- 
    	spring aop的命名空间需要自己加进来
    	http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
        spring aop还需要两个jar包
        aspectjrt.jar
        aspectjweaver.jar
    
    	导入目标类,导入切面
     -->       
	<bean id="personDao" class="_10SpringAOP.PersonDaoImpl"></bean>
	<bean id="myTransaction" class="_10SpringAOP.MyTransaction"></bean>
	<aop:config>
		<aop:pointcut expression="execution(* _10SpringAOP.PersonDaoImpl.*(..))" id="perform"/>
		<aop:aspect ref="myTransaction">
			<aop:before method="beginTransaction" pointcut-ref="perform"/>
			<!-- 记得加上returning -->
			<aop:after-returning method="commit" pointcut-ref="perform" returning="var"/>
			<aop:after method="finalMethod" pointcut-ref="perform"/>
			<!-- 记得加上throwsing -->
			<aop:after-throwing method="throwMethod" pointcut-ref="perform" throwing="ex"/>
			<aop:around method="aroundMethod" pointcut-ref="perform"/>
		</aop:aspect>
	</aop:config>
</beans>



Spring面向切面编程(AOP)

原文:http://my.oschina.net/u/2356176/blog/469141

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!