BeanPostProcessor
今天学习了一下Spring的BeanPostProcessor接口。
该接口作用是:如果我们需要在Spring容器完成Bean的实例化,可以定义一个BeanPostProcessor接口的实现
案例:通过某个参数不同,达到调用同一个接口的不同实现方法的目的,类似适配器模式
创建一个抽象类,作为servicePool
public abstract class YeweiServicePoolBase<Tp> implements BeanPostProcessor {
public YeweiServicePoolBase() {
super();
System.out.println("这是YeweiServicePoolBase实现类构造器!!");
}
protected final Map<String, Tp> servicePool = new ConcurrentHashMap<>();
private final Class<Tp> serviceClass =
(Class<Tp>) ((ParameterizedType) YeweiReflectionUtils.
getOriginalClassWithoutProxy(this).
getGenericSuperclass()).
getActualTypeArguments()[0];
public List<Tp> getAllServices(){
return new ArrayList<>(servicePool.values());
}
public List<String> getAllServiceKeys(){
return new ArrayList<>(servicePool.keySet());
}
public Tp getServiceFromPool(String tp) {
return servicePool.get(tp);
}
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
if (serviceClass.isAssignableFrom(bean.getClass())) {
Tp theServiceBean = (Tp) bean;
String theBeanName = getServiceBeanName(theServiceBean);
if (YeweiStringUtils.isNotEmpty(theBeanName)) {
servicePool.put(theBeanName, theServiceBean);
}
}
return bean;
}
public abstract String getServiceBeanName(Tp theServiceBean);
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
System.out.println("bean处理器:bean创建之后.."+beanName);
return bean;
}
}
枚举,作为接口实现类的区分。
public enum ServiceEnum {
wechat,
pingpp,
unionpay,
}
适配器接口。
public interface DepositVendorAdapter {
public ServiceEnum getDepositVendorType();
public void refund() throws Exception;
public void deposit() throws Exception;
}
具体业务接口
public interface DepositVendorService {
public void deposit(String str) throws Exception;
public void refund() throws Exception;
}
业务接口的三个实现:pingpp,wechat,unionpay,
Pingpp:
@Service("pingppPayService")
public class PingppPayServiceImpl implements DepositVendorAdapter {
@Override
@Transactional(propagation = Propagation.SUPPORTS)
public void deposit() throws Exception {
System.out.println("pingpp pay");
}
@Override
public ServiceEnum getDepositVendorType() {
// TODO Auto-generated method stub
return ServiceEnum.pingpp;
}
@Override
public void refund() throws Exception {
// TODO Auto-generated method stub
}
}
UnionPay
@Service("unionPayService")
public class UnionPayServiceImpl implements DepositVendorAdapter {
@Override
@Transactional(propagation = Propagation.SUPPORTS)
public void deposit() throws Exception {
System.out.println("unionpay pay");
}
@Override
public ServiceEnum getDepositVendorType() {
// TODO Auto-generated method stub
return ServiceEnum.unionpay;
}
@Override
public void refund() throws Exception {
// TODO Auto-generated method stub
}
}
@Service("wechatPayService")
public class WechatPayServiceImpl implements DepositVendorAdapter {
@Override
@Transactional(propagation = Propagation.SUPPORTS)
public void deposit() throws Exception {
System.out.println("wechat pay");
}
@Override
public ServiceEnum getDepositVendorType() {
// TODO Auto-generated method stub
return ServiceEnum.wechat;
}
@Override
public void refund() throws Exception {
// TODO Auto-generated method stub
}
}
适配器接口实现、
@Service("depositVendorService")
public class DepositVendorServiceImpl extends YeweiServicePoolBase<DepositVendorAdapter> implements DepositVendorService {
public DepositVendorServiceImpl() {
}
@Override
public String getServiceBeanName(DepositVendorAdapter theServiceBean) {
// TODO Auto-generated method stub
return theServiceBean.getDepositVendorType().name();
}
@Override
public void deposit(String str) throws Exception {
// TODO Auto-generated method stub
DepositVendorAdapter vendorAdapter = this.servicePool.get(str);
if (vendorAdapter == null) {
throw new Exception("Vendor Not Defined");
}
vendorAdapter.deposit();
}
@Override
public void refund() throws Exception {
// TODO Auto-generated method stub
}
}
Spring 容器在初始化的时候,会为servicePool 注入 service,类似 {“name”:"service"} 的键值对。
{
wechat=com.yewei.service.deposit.WechatPayServiceImpl@2cb75649,
unionpay=com.yewei.service.deposit.UnionPayServiceImpl@1c0ac42a,
pingpp=com.yewei.service.deposit.PingppPayServiceImpl@74c7a6b2
}
测试代码:
public static void main(String[] args) throws Exception {
ApplicationContext ctx=new FileSystemXmlApplicationContext("D:/eclipWorkspace/DepositServicePool/WebContent/WEB-INF/springmvc.xml");
DepositVendorService depositVendorService=(DepositVendorService) ctx.getBean("depositVendorService");
depositVendorService.deposit("wechat");
depositVendorService.deposit("pingpp");
depositVendorService.deposit("unionpay");
}
打印:
wechat pay pingpp pay unionpay pay

浙公网安备 33010602011771号