如何自己实现spring aop相似的功能

    AOP(Aspect Oriented Programming),即面向切面编程,它利用一种称为"横切"的技术,把软件系统分为两个部分:核心关注点横切关注点,用来实现诸如权限认证、日志、事务等功能。简单讲就是在指定类的指定方法前后增加通用代码,减少冗余增强功能。

手动实现:

被代理的目标接口

package com.xxx.service;

public interface RunningService {

	public void running();
}

被代理的目标实现类

package com.xxx.service.impl;

import org.springframework.stereotype.Component;

import com.xxx.service.RunningService;

@Component
public class RunningServiceImpl implements RunningService {
	@Override
	public void running() {
		System.out.println("do something");
	}
}

需要增加的通用操作类

package com.xxx.proxy;

import org.springframework.stereotype.Component;

@Component
public class Aop {

	public void before(){
		System.out.println("before!");
	}
	
	public void after(){
		System.out.println("after!");
	}
}

代理工厂类

package com.xxx.proxy;

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){
		
		ClassLoader classLoader = target.getClass().getClassLoader();
		Class<?>[] interfaces = target.getClass().getInterfaces();
		InvocationHandler invocationHandler = new InvocationHandler() {
			
			@Override
			public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
				aop.before();
				Object result = method.invoke(target, args);
				aop.after();
				return result;
			}
		};
		
		return Proxy.newProxyInstance(classLoader, interfaces, invocationHandler);
	}
}

spring配置文件

<?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.xxx.proxy"></context:component-scan>
    <!-- 调用工厂方法,返回runningService的代理对象 -->
    <bean id="runningService_proxy" class="com.xxx.proxy.ProxyFactory" factory-method="newProxyInstance">
        <constructor-arg index="0" type="java.lang.Object" ref="runningServiceImpl"></constructor-arg>
        <constructor-arg index="1" type="com.xxx.proxy.Aop" ref="aop"></constructor-arg>
    </bean>
</beans>

测试类

package com.xxx.proxy;

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

import com.xxx.proxy.service.RunningService;

public class TestAop {
	@Test
	public void test(){
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
		RunningService run = (RunningService)context.getBean("runningService_proxy");
		run.running();
	}
}

运行结果

104805_y0Ns_992937.png

转载于:https://my.oschina.net/u/992937/blog/1647662

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值