业务多变的情况:一个接口,多个实现类的情况下动态初始化service实现类:
serviceImpl注解:
// 代表旧的实现类
@Service(“接口类名+Impl_old”)
// 代表新的实现类
@Service(“接口类名+Impl_new”)
// 其它实现类,在新的模式下扩展,方法传入逻辑处理标识等方式
@Service(“接口类名+Impl_new2019”)
@Service(“接口类名+Impl_new2020”)
@Service(“接口类名+Impl_new2021”)
…
// spring注入ApplicationContext
@Autowired
private ApplicationContext context;
/***
*动态初始化Service
* @return
*/
@SuppressWarnings("unchecked")
public <T> T getReportService(Class<T> clazz) {
try {
// 逻辑处理你需要实例化的类
boolean is_new = true;
String beanName = null;
if (is_new) {
beanName = clazz.getSimpleName() + "Impl_new";
} else {
beanName = clazz.getSimpleName() + "Impl_old";
}
return (T) context.getBean(beanName, clazz);
} catch (Exception e) {
System.out.println("请传入正确的ServiceID和参数");
e.printStackTrace();
}
return (T) clazz;
}
业务中遇到的问题:欢迎交流。可能描述不清楚。