AOP编程的简单实现

AOP是什么

AOP是一种编程范式,提供从还有一个角度来考虑程序结构以完好面向对象编程(OOP)。

AOP为开发人员提供了一种描写叙述横切关注点的机制,并可以自己主动将横切关注点织入到面向对象的软件系统中。从而实现了横切关注点的模块化。

AOP可以将那些与业务无关,却为业务模块所共同调用的逻辑或责任。比如事务处理、日志管理、权限控制等。封装起来,便于降低系统的反复代码,降低模块间的耦合度,并有利于未来的可操作性和可维护性。

AOP能干什么,也是AOP带来的优点

减少模块的耦合度 使系统easy扩展 设计决定的迟绑定:使用AOP,设计师能够推迟为将来的需求作决定,由于它
能够把这样的需求作为独立的方面非常easy的实现。 更好的代码复用性

定义一个接口

package com.zhu.jdk;

public interface UserService {
		public void addUser();
		public void updateUser();
		public void delectUser();
}

定义一个接口的实现类方法

package com.zhu.jdk;

public class UserServiceImpl implements UserService {
	public void addUser() {
		System.out.println("addUser a_jdk");
	}
	public void updateUser() {
		System.out.println("updateUser a_jdk");
	};
	public void delectUser() {
		System.out.println("delectUser a_jdk");
	};
}

定义一个切面类
package com.zhu.jdk;

public class Myzhu {
	public void before() {
		System.out.println("前面");
	}
	public void after() {
		System.out.println("后面");
	}
}

用一个工厂把接口和切面类进行整合在一起

package com.zhu.jdk;

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

public class MyBeanFactory {
	public static UserService createService() {
		//目标类
		final UserService userService=new UserServiceImpl();
		//切面类
		Myzhu myAspect=new Myzhu();
		/*
		 * getLoaderloader 类加载器,动态代理类运行时创建,任何类都需要类加载器将其加载到内存
		 * getClass.getInterface 加载自己的接口
		 * InvocationHandler 处理类,接口,
		 * Object proxy:代理对象
		 * Method method:代理对象当前执行的方法的描述对象
		 * method.getName 执行方法名
		 * */
		UserService proxService=(UserService)Proxy.newProxyInstance(MyBeanFactory.class.getClassLoader(), 
				userService.getClass().getInterfaces(), new InvocationHandler() {
			
			@Override
			public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
				myAspect.before();
				//执行接口的方法
				Object obj=method.invoke(userService, args);
				myAspect.after();
				return null;
			}
		});
		return proxService;
		
	}
}

测试类

package com.zhu.jdk;

import org.junit.Test;

public class TestJDK {
		@Test
		public void demo01() {
			UserService userService= MyBeanFactory.createService();
			userService.addUser();
			userService.updateUser();
			userService.delectUser();
		}
}

二、使用bean.xml进行管理
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: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">
    <bean id="userServiceId" class="com.zhu.jdk.UserServiceImpl"></bean>
    <bean id="myAspectId" class="com.zhu.jdk.Myzhu"></bean>
    <!--
    	optimize,强制使用cglib,interface,使用接口, target,确定目标类,interceptorNames,确定切面类
    	optimize可选,true代表使用CGLib, false代表使用jdk proxy
    -->
    <bean id="proxyServiceId" class="org.springframework.aop.framework.ProxyFactoryBean">
    	<property name="interfaces" value="com.zhu.jdk.UserService"></property>
    	<property name="target" ref="userServiceId"></property>
    	<property name="interceptorNames" value="myAspectId"></property>
    	<property name="optimize" value="true"></property>
    </bean>
</beans>

测试类的代码如下:

package com.zhu.jdk;
import org.springframework.aop.framework.ProxyFactoryBean;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestJDK {
		@Test
		public void demo01() {
			//加载配置文件,通过id找到类
			String xmlPath="com/zhu/jdk/beans.xml";
			ApplicationContext application=new ClassPathXmlApplicationContext(xmlPath);
			UserService userService=(UserService) application.getBean("proxyServiceId");
			userService.addUser();
			userService.updateUser();
			userService.delectUser();
		}
}

基本流程图如下:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值