码迷,mamicode.com
首页 > 其他好文 > 详细

(5)手动实现AOP编程

时间:2016-07-23 07:39:24      阅读:229      评论:0      收藏:0      [点我收藏+]

标签:spring


1、面向对象、面向接口、面向切面编程

面向对象编程(OOP),是将现实的事物抽象为包含property和method的class,它是对面向过程编程的一种演变,能够实现代码的重用,它实现的是代码级别的抽象。

面向接口编程,它是以功能相近的方法组织到一个接口内,它实现的是功能级别的抽象。

面积切面编程,它实现业务功能和关注点的分离,它实现的是业务级别的抽象。


Difference Between AOP and OOP

http://www.differencebetween.com/difference-between-aop-and-vs-oop/



2、AOP的作用

AOP面向切面的编程,可以实现“业务代码”与“关注点代码”分离。

// 保存一个用户
public void add(User user) { 
	Session session = null; 
	Transaction trans = null; 
	try { 
		session = HibernateSessionFactoryUtils.getSession();   // 【关注点代码】
		trans = session.beginTransaction();    // 【关注点代码】
		 
		session.save(user);     // 核心业务代码
		 
		trans.commit();     //…【关注点代码】

	} catch (Exception e) {     
		e.printStackTrace(); 
		if(trans != null){ 
			trans.rollback();   //..【关注点代码】

		} 
	} finally{ 
		HibernateSessionFactoryUtils.closeSession(session);   ////..【关注点代码】

	} 
}


分析总结:

         关注点代码,就是指重复执行的代码。

         业务代码与关注点代码分离,好处?

               --à关注点代码写一次即可;

               --à开发者只需要关注核心业务;

                                                          --à运行时期,执行核心业务代码时候动态植入关注点代码;【代理】

3、AOP示例


3.1、示例一


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:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd"
        default-autowire="byType">

	<!-- 开启注解扫描 -->
	<context:component-scan base-package="com.rk.hibernate.d_aop_basic"></context:component-scan>

</beans>

Aop.java

package com.rk.hibernate.d_aop_basic;

import org.springframework.stereotype.Component;

/**
 * 重复执行代码形成的一个AOP类 *
 */
@Component // 加入IOC容器
public class Aop
{
	// 重复执行的代码
	public void begin()
	{
		System.out.println("事务开启");
	}
	
	// 重复执行的代码
	public void end()
	{
		System.out.println("事务结束");
	}
}

IUserDao.java

package com.rk.hibernate.d_aop_basic;

//接口
public interface IUserDao
{
	void save();
}

UserDao.java

package com.rk.hibernate.d_aop_basic;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

/**
 * 目标对象
 */
@Component // 加入IOC容器
public class UserDao implements IUserDao
{
	@Resource 
	private Aop aop; //注意:这里并没有设置setter方法,但是也能成功
	@Override
	public void save()
	{
		aop.begin();
		System.out.println("-----核心业务:保存!!!------");
		aop.end();
	}
}

App.java

package com.rk.hibernate.d_aop_basic;

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

public class App
{
	private ApplicationContext ac = new ClassPathXmlApplicationContext("/com/rk/hibernate/d_aop_basic/applicationContext.xml");
	@Test
	public void test()
	{
		IUserDao dao = (IUserDao) ac.getBean("userDao");
		dao.save();
	}
}



3.2、示例二


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:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd"
        default-autowire="byType">

	<!-- 开启注解扫描 -->
	<context:component-scan base-package="com.rk.hibernate.e_aop_basic"></context:component-scan>
	<!-- 调用工厂方法,返回UserDao的代理对象 -->
	<bean id="userDao_proxy" class="com.rk.hibernate.e_aop_basic.ProxyFactory" factory-method="newProxyInstance">
		<constructor-arg index="0" type="java.lang.Object" ref="userDao"></constructor-arg>
		<constructor-arg index="1" type="com.rk.hibernate.e_aop_basic.Aop" ref="aop"></constructor-arg>
	</bean>
</beans>

Aop.java

package com.rk.hibernate.e_aop_basic;

import org.springframework.stereotype.Component;

/**
 * 重复执行代码形成的一个AOP类 *
 */
@Component // 加入IOC容器
public class Aop
{
	// 重复执行的代码
	public void begin()
	{
		System.out.println("事务开启");
	}
	
	// 重复执行的代码
	public void end()
	{
		System.out.println("事务结束");
	}
}

IUserDao.java

package com.rk.hibernate.e_aop_basic;

//接口
public interface IUserDao
{
	void save();
}

UserDao.java

package com.rk.hibernate.e_aop_basic;

import org.springframework.stereotype.Component;

/**
 * 目标对象
 */
@Component // 加入IOC容器
public class UserDao implements IUserDao
{
	@Override
	public void save()
	{
		System.out.println("-----核心业务:保存!!!------");
	}
}

ProxyFactory.java

package com.rk.hibernate.e_aop_basic;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * 代理工厂 *
 */
public class ProxyFactory
{
	public static Object newProxyInstance(final Object target,final Aop aop)
	{
		// 生成代理对象的方法
		return Proxy.newProxyInstance(
										target.getClass().getClassLoader(), 
										target.getClass().getInterfaces(), 
										new InvocationHandler()
										{
											
											@Override
											public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
											{
												aop.begin();// 执行重复代码
												Object result = method.invoke(target, args);// 执行目标对象的方法
												aop.end();// 执行重复代码
												return result;
											}
										});
	}
}

App.java

package com.rk.hibernate.e_aop_basic;

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

public class App
{
	private ApplicationContext ac = new ClassPathXmlApplicationContext("/com/rk/hibernate/e_aop_basic/applicationContext.xml");
	@Test
	public void test()
	{
		IUserDao dao = (IUserDao) ac.getBean("userDao_proxy");
		dao.save();
	}
}















(5)手动实现AOP编程

标签:spring

原文地址:http://lsieun.blog.51cto.com/9210464/1828993

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!