自定义annotation 实现注入功能(笔记,以备后查)

1.Annotation class

package com.test;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target( { ElementType.FIELD,ElementType.METHOD })//允许在method 和 field 使用annotation
public @interface ServiceInject {
Class name();
}


2. service 接口,及2个实现方法

package com.test;

public interface ITestService {
public String save();
}



package com.test;

public class TestService_1 implements ITestService {

public String save() {
return this.getClass().getSimpleName() +" class was invoked";
}

}



package com.test;

public class TestService_2 implements ITestService {

public String save() {
return this.getClass().getSimpleName()+" class was invoked";
}

}


3. 模拟的拦截器

package com.test;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class SimilarToTheInterceptor {
public static void scanAllFieldsAndMethods(Object obj) {
Field[] fields = obj.getClass().getDeclaredFields();// 获取所有类型的field
Method[] methods = obj.getClass().getMethods(); // 方法一般是public
for (Field f : fields) {
if (f.isAnnotationPresent(ServiceInject.class)) {
ServiceInject ie = f.getAnnotation(ServiceInject.class);
Class name = ie.name();
boolean flag = f.isAccessible();//保存当前field的修饰类型
try {
f.setAccessible(true);//强制field 可以访问
f.set(obj, f.getType().cast(name.newInstance()));//使用cast 强制转换类型
f.setAccessible(flag);//还原当前field 的修饰类型,避免被不非法使用
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e1) {
e1.printStackTrace();
}

}
}
for (Method m : methods) {//set注入
if (m.isAnnotationPresent(ServiceInject.class) && m.getName().indexOf("set")!=-1) {
ServiceInject ie = m.getAnnotation(ServiceInject.class);
Class name = ie.name();
try {
m.setAccessible(true);
m.invoke(obj, name.newInstance());
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e1) {
e1.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}
}

}



4.模拟的Action

package com.test;


@SuppressWarnings("unused")
public class TestAction {
@ServiceInject(name = TestService_1.class)
public ITestService testService1;

private ITestService testService2;


public void saveDate1() {
System.out.println("==========Saved======"+testService1.save());
}
public void saveDate2() {
System.out.println("==========Saved======"+testService2.save());
}


@ServiceInject(name = TestService_2.class)
public void setTestService2(ITestService testService2) {
this.testService2 = testService2;
}

}



5. 测试类

package com.test;

public class Main {
public static void main(String[] args) {
TestAction action = new TestAction();
SimilarToTheInterceptor.scanAllFieldsAndMethods(action);// 模拟拦截器功能
action.saveDate1();
action.saveDate2();
}
}



6. 结果:
==========Saved======TestService_1 class was invoked
==========Saved======TestService_2 class was invoked
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值