背景
文中对应的枚举类:Animal
抛出疑问:一个接口有多个实现类,如何正确调用到想要的实现类方法呢?
此处只是一个小案例,具体需求以实际为准。
接口和实现类的定义:
public interface Chinese{
String hangzhou();
}
import org.springframework.stereotype.Service;
@Service("catChinese")
public class xihuImpl implements Chinese {
@Override
public String hangzhou() {
System.out.println("xihu");
return "xihu";
}
}
import org.springframework.stereotype.Service;
@Service("dogChinese")
public class yuhangImpl implements Chinese {
@Override
public String hangzhou() {
System.out.println("yuhang");
return "yuhang";
}
}
说明:Chinese
接口同时被xihuImpl
和yuhangImpl
实现。并且重写了方法hangzhou()
。@Service("catChinese")
声明装配该类到 IOC
中并且命名为 catChinese
情景:
① 明确调用。
业务上知道自己要调用哪个实现类了,直接实例化对应实现类。
Chinese chinese=new xihuImpl();
chinese.hangzhou(); //xihu
② 通用化调用。
业务上希望通过一个方法,判断 传入的参数 决定调用的实现类。
现在就能根据对应的Bean
名称进行调用。
- 1.封装一个工具类。
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
/**
* @description: 对Spring容器进行各种上下文操作的工具类
* <p>
* 该工具类必须声明为Spring 容器里的一个Bean对象,否则无法自动注入ApplicationContext对象
* <p>
* 可使用@Component注解实例化,注意要开启包扫描并且所在包路径能被扫描到
* @author: pjx
* @date: 2022年4月21日 12:58:15
*/
@Component
public class ApplicationContextProvider implements ApplicationContextAware {
/**
* 上下文对象实例
*/
private static ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
ApplicationContextProvider.applicationContext = applicationContext;
}
/**
* 根据Bean名称获取Bean对象
*
* @param name Bean名称
* @return 对应名称的Bean对象
*/
public static Object getBean(String name) {
return applicationContext.getBean(name);
}
/**
* 根据Bean的类型获取对应的Bean
*
* @param requiredType Bean类型
* @return 对应类型的Bean对象
*/
public static <T> T getBean(Class<T> requiredType) {
return applicationContext.getBean(requiredType);
}
/**
* 根据Bean名称获取指定类型的Bean对象
*
* @param name Bean名称
* @param requiredType Bean类型(可为空)
* @return 获取对应Bean名称的指定类型Bean对象
*/
public static <T> T getBean(String name, Class<T> requiredType) {
return applicationContext.getBean(name, requiredType);
}
/**
* 判断是否包含对应名称的Bean对象
*
* @param name Bean名称
* @return 包含:返回true,否则返回false。
*/
public static boolean containsBean(String name) {
return applicationContext.containsBean(name);
}
/**
* 获取对应Bean名称的类型
*
* @param name Bean名称
* @return 返回对应的Bean类型
*/
public static Class<?> getType(String name) {
return applicationContext.getType(name);
}
/**
* 获取上下文对象,可进行各种Spring的上下文操作
*
* @return Spring上下文对象
*/
public static ApplicationContext getContext() {
return applicationContext;
}
}
- 2.定义方法调用。
@Test
void beanUse(){
System.out.println(beanIndex(Animal.CAT)); //xihu
}
@Test
String beanIndex(Animal animalEnum){
//可以把这个枚举类当做方法入参来判断具体走哪个实现类的方法。
String head = animalEnum == null ? Animal.DOG.getType() : animalEnum.getType();
Chinese chinese= ApplicationContextProvider.getBean( head+"Chinese", Chinese.class);
return chinese.hangzhou(); //xihu
}
ApplicationContextProvider.getBean( head+"Chinese", Chinese.class);
根据动态的入参进行调用,获取的实现类也不一样。