目录
笔者在实际生产中有时会遇到需要自己生成 serviceImpl的 方法,和需要用到工厂类的情况,下面先讲讲怎么用反射做工厂类,
抽象父类
动作被抽象出来
public interface PayStrategy {
void pay(double total);
}
实现类
public class EbankPay implements PayStrategy {
public void pay(double total) {
System.out.println("pay with ebankpay: " + total);
}
}
枚举类
public enum SectionEnum {
alipay("com.xx.install.calc.model.alipay"),
wechatpay("com.xx.install.calc.model.wechatpay"),
applepay("com.xx.install.calc.model.applepay");
SectionEnum(String className) {
this.setClassName(className);
}
private String className;
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
}
生产方法
利用反射获得父类
public class StrategyFactory {
public static PayStrategy getStrategy(String strategyType) throws Exception {
String className = SectionEnum .valueOf(strategyType).getClassName();
return (PayStrategy) Class.forName(className).newInstance();
}
}
当然以上只是在工具类中适用,真正的 实际中可能会有 导出报表时,实现类中有大量的 注入的service,所以工具类的这一套就需要稍微换一换
获取beans的类
/**
* Description: 工具类,用于普通类获取注册过的Bean
*
*/
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
@Component
public class SpringContextUtil implements ApplicationContextAware {
private final static Logger logger = LoggerFactory
.getLogger(SpringContextUtil.class);
private static ApplicationContext applicationContext; // Spring应用上下文环境
// 下面的这个方法上加了@Override注解,原因是继承ApplicationContextAware接口是必须实现的方法
@Override
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
this.applicationContext = applicationContext;
}
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
public static Object getBean(String name) throws BeansException {
return applicationContext.getBean(name);
}
public static Object getBean(String name, Class requiredType)
throws BeansException {
return applicationContext.getBean(name, requiredType);
}
public static Object getBean(Class requiredType)
throws BeansException {
return applicationContext.getBean(requiredType);
}
public static boolean containsBean(String name) {
return applicationContext.containsBean(name);
}
public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException {
return applicationContext.isSingleton(name);
}
public static Class getType(String name) throws NoSuchBeanDefinitionException {
return applicationContext.getType(name);
}
public static String[] getAliases(String name) throws NoSuchBeanDefinitionException {
return applicationContext.getAliases(name);
}
}
导出的父类
public interface Handle{
@SuppressWarnings("rawtypes")
public List<Map> handleData(Map<String, String> map);
}
实现的子类
可以使用拼接的名字 ,每一个 导出都会 代入自己的唯一的标识,所以可以 根据传入的标识不一样进行处理
@Component
public class M107000015Handle implements Handle{
private Logger logger = LoggerFactory.getLogger(getClass());
@Resource
private ContractService contractService;
@SuppressWarnings("rawtypes")
@Override
public List<Map> handleData(Map<String, String> map) {
return contractService.YFQbuildData(map);
}
}
实际使用
String platCode=dbsource.getname();
Handle handle = (Handle) SpringContextUtil.getBean(platCode+ "Handle");
handle.handleData(data);