Java注解(2)--根据自定义注解及接口获取支持的实现类

该方法适用于面向接口编程时,一个接口有多个实现类,需要根据实现类的自定义支持注解来获取该实现类,并执行实现类的具体方法。
一、自定义注解

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

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface SelfDefineAnnotaion {
    public String[] value();//注解只有一个变量时 变量名必须为value
}

二、编写测试接口及多个实现类

public interface TestInterface {
    public void sayTest();
}
@SelfDefineAnnotaion({"TEST_CODE_01"})
public class TestInterfaceImpl1 implements TestInterface {
    @Override
    public void sayTest() {
        System.out.println("TestInterfaceImpl1 sayTest...");
    }
}
@SelfDefineAnnotaion({"TEST_CODE_02"})
public class TestInterfaceImpl2 implements TestInterface {
    @Override
    public void sayTest() {
        System.out.println("TestInterfaceImpl2 sayTest...");
    }
}

三、Spring容器管理实现类bean
applicationContext-common.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:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">

    <bean id="testInterface1" class="com.dima.test.annotation.TestInterfaceImpl1"/>
    <bean id="testInterface2" class="com.dima.test.annotation.TestInterfaceImpl2"/>

</beans>

四、简单测试Demo

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

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

/**
 * @ClassName: TestAnnotation 
 * @Description 测试自定义注解:根据接口及注解获取对应的实现类,并执行实现类的方法
 */
public class TestAnnotation {
    private static Map<Class<?>, Map<String, Object>> serviceImplFactoryMap = new HashMap<Class<?>, Map<String, Object>>();
    @SuppressWarnings("resource")
    public static void main(String[] args) {
        // 初始化spring容器
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:applicationContext-common.xml");
        // 根据接口获取配置的所有实现类
        Map<String, TestInterface> beans = applicationContext.getBeansOfType(TestInterface.class);
        System.out.println(beans);
        Map<String, Object> serviceMap = new HashMap<String, Object>();
        // 遍历所有实现类
        Set<Entry<String, TestInterface>> entrySet = beans.entrySet();
        Iterator<Entry<String, TestInterface>> iterator = entrySet.iterator();
        while (iterator.hasNext()) {
            Object interfaceServiceImpl = iterator.next().getValue();
            // 获取实现类的所有注解
            SelfDefineAnnotaion annotationValue = interfaceServiceImpl.getClass().getAnnotation(SelfDefineAnnotaion.class);
            if (null != annotationValue) {
                for (String annotation : annotationValue.value()) {
                    // 将注解及实现类放入map集合中
                    serviceMap.put(annotation, interfaceServiceImpl);
                }
            }
        }
        serviceImplFactoryMap.put(TestInterface.class, serviceMap);
        String annotation = "TEST_CODE_02";
        // 根据接口及注解获取到对应的实现类
        TestInterface object = (TestInterface)serviceImplFactoryMap.get(TestInterface.class).get(annotation);
        // 执行实体类的方法
        object.sayTest();
    }
}
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值