该方法适用于面向接口编程时,一个接口有多个实现类,需要根据实现类的自定义支持注解来获取该实现类,并执行实现类的具体方法。
一、自定义注解
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();
}
}