写Intercept 类 继承InvocationHandler
public class Intercept implements InvocationHandler{
/**
* 要处理的对象(也就是我们要在方法的前后加上业务逻辑的对象,如例子中的Hello)
*/
private Object target; // 被代理的目标对象
/**
* 动态生成方法被处理过后的对象 (写法固定)
* target(需要关注的类方法)
*/
public Object bind(Object target) throws InstantiationException, IllegalAccessException
{
this.target = target;
return Proxy.newProxyInstance(this.target.getClass().getClassLoader(), this.target.getClass().getInterfaces(),
this);
}
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
Object result;
AOPModel model=null;
if(InterceptList.getInterceptMethod().containsKey(method.getName()))
{
model=InterceptList.getInterceptMethod().get(method.getName());
this.monitor(model.getBefore(), model.getBefore_aspectName(),this.target);
result = method.invoke(this.target, args);
this.monitor(model.getAfter(), model.getAfter_aspectName(),this.target);
}else {
result = method.invoke(this.target, args);
}
return result;
}
/**
* 依据是否注入横切关注点来决定before、after的调用
*/
private void monitor(Object clazz, String aspectName,Object target) throws Exception {
if (clazz != null) {
Method[] methods = clazz.getClass().getDeclaredMethods();
for (Method method : methods) {
if (method.getName().equals(aspectName)) {
method.invoke(clazz,target);
}
}
}
}
}
然后修改我们的IOC容器:当用户继承IOC容器后,自动查找用户配置的参数进行初始化
注意,此时使用this,new Intercept().bind()Load对象
public class IOC {
public IOC()
{
Init();
}
public void Init()
{
Field[] fields = getClass().getDeclaredFields();
for(Field field : fields){
Inject inject=field.getAnnotation(Inject.class);
try {
if(inject!=null)
{
field.setAccessible(true);
if(inject.Intercept())
{
field.set(this,new Intercept().bind(Class.forName(inject.ClassName().trim()).newInstance()));
}else {
field.set(this,Class.forName(inject.ClassName().trim()).newInstance());
}
}
} catch (Exception e) {
Logger.getLogger(getClass()).error("初始化容器异常:"+inject.ClassName().trim()+"初始化失败",e);
}
}
}
}
测试类
package com.metarnet.Main;
import com.metarnet.Injects.Inject;
import com.metarnet.Interfaces.BeforeAfter;
import com.metarnet.Interfaces.Interface1;
import com.metarnet.extend.AOPModel;
import com.metarnet.extend.IOC;
import com.metarnet.extend.IOCInit;
import com.metarnet.extend.InterceptList;
public class TestIOC extends IOC{
public TestIOC(){
super();
}
@Inject(ClassName = "com.metarnet.Interfaces.imps.Interface1Impl", Intercept = true)
private Interface1 interface1;
@Inject(ClassName = "com.metarnet.Interfaces.imps.Interface1Impl2", Intercept = false)
private Interface1 interface2;
@Inject(ClassName = "com.metarnet.Interfaces.imps.After", Intercept = false)
private BeforeAfter after;
@Inject(ClassName = "com.metarnet.Interfaces.imps.Before", Intercept = false)
private BeforeAfter before;
public Interface1 getInterface1() {
return interface1;
}
public void setInterface1(Interface1 interface1) {
this.interface1 = interface1;
}
/**
* 此方法可以修改为读取配置文件然后初始化
*/
public void init()
{
AOPModel model=new AOPModel();
model.setAfter(after);
model.setBefore(before);
model.setAfter_aspectName("SayAfter");
model.setBefore_aspectName("SayBefore");
InterceptList.getInterceptMethod().put("SayHello", model);
InterceptList.getInterceptList().add("interface1");
}
public static void main(String[] args) {
IOCInit.Init();
TestIOC ioc = new TestIOC();
ioc.init();
ioc.interface1.SayHello();
ioc.interface2.SayHello();
}
}
Model
/**
* 需要横切的列表
*/
public class AOPModel {
private Object before;// before 横切关注点 (之前调用)
private Object after; // after 横切关注点 (之后调用)
private String before_aspectName;//before横切调用的方法
private String after_aspectName;//after横切调用的方法
public Object getBefore() {
return before;
}
public void setBefore(Object before) {
this.before = before;
}
public Object getAfter() {
return after;
}
public void setAfter(Object after) {
this.after = after;
}
public String getBefore_aspectName() {
return before_aspectName;
}
public void setBefore_aspectName(String before_aspectName) {
this.before_aspectName = before_aspectName;
}
public String getAfter_aspectName() {
return after_aspectName;
}
public void setAfter_aspectName(String after_aspectName) {
this.after_aspectName = after_aspectName;
}
}
/**
* 需要监听的方法列表
*/
public final class InterceptList {
private static HashMap<String,AOPModel> map = new HashMap<String,AOPModel>();
private static ArrayList<String> list = new ArrayList<String>();
public static HashMap<String,AOPModel> getInterceptMethod() {
return map;
}
public static ArrayList<String> getInterceptList()
{
return list;
}
}
相关推荐
Spring5 框架 ---- IOC容器 ---- 代码 Spring5 框架 ---- IOC容器 ---- 代码 Spring5 框架 ---- IOC容器 ---- 代码 Spring5 框架 ---- IOC容器 ---- 代码 Spring5 框架 ---- IOC容器 ---- 代码 Spring5 框架 ---- ...
NULL 博文链接:https://incan.iteye.com/blog/296563
Spring.net二----初探IOC容器.rar
spring ioc容器初始化流程图 spring ioc容器依赖注入流程图 spring aop实现原理流程图
Java-Spring-SpringIoC容器-SpringIoC的学习 SpringIoC容器的学习笔记 主要介绍了IoC容器工作原理以及如何配置IoC容器 通过标签和注解配置容器
基于Cglib简单实现Spring体系(Ioc+Aop+Mvc)基于Cglib简单实现Spring体系(Ioc+Aop+Mvc)基于Cglib简单实现Spring体系(Ioc+Aop+Mvc)基于Cglib简单实现Spring体系(Ioc+Aop+Mvc)基于Cglib简单实现Spring体系(Ioc+Aop+Mvc)...
ISpring-IOC-AOP 自定义 IOC & AOP 实现 master 所有代码 feat-init 初始仓库 feat-ioc-aop 手动实现ioc&aop
一个简单的IOC容器的java实现,一个简单的IOC容器的java实现
unity ioc 和 aop 简单示例,适合初学者,包括XML的配置!
proxy,jdkproxy-transaction,jdkproxy-salary,day02-itheima11-spring-08-cglibproxy,day02-itheima11-spring-09-aop.xml,day02-itheima11-spring-10-aop.xml.advice,day02-itheima11-spring-11-aop.xml.exception等...
力求获得一种简洁实用的方法实现IOC和AOP相结合的使用方式。 查阅了多个技术资料。经过多次测试,基本达到目的。 IOC使用微软的 Microsoft.Practices.Unity,AOP 使用微软企业库的 Microsoft.Practices.Enterprise...
C#实现的IOC和AOP框架,供学习
spring version: 5.0.0; jdk: 1.8 IOC大致调用顺序(IOC调用的AOP标签解析)
B-spring-ioc-container-homework:IoC容器基础知识Spring课程
如何实现spring 的ioc 和 aop, 其中的具体流程图,主要流程
在本文中,作者通过一个WebService访问的实例,具体描述了SOA应用中所遇到的一系列具体问题,并描述如何利用IoC和AOP等技术进行代码重构,从而构建结构更加良好、灵活的SOA应用。 1.引言SOA是一种构造分布式系统的...
以注解方式模拟Spring IoC AOP,了解Spring是如何实现的.
手写简单实现ioc、aop事务demo,实现事务回滚,使用到了,工厂模式,动态代理
介绍下spring的ioc和aop的定义
SpringIOC和AOP实现机制模拟,来自与网络。