医保动态库初始化失败,java数组动态初始化
动态初始化类InitializingBean的afterPropertiesSet方法目录
afterPropertiesSet方法动态初始化类初始化bean
多个初始化
形状界面:
Shape界面:
AbstractShapeFunc
CircleService
恢复服务
三重服务
trapezoid服务
ShapeFuncsService
测试:
总结:
《动态初始化类》学习如何调用时,需要事先确定要增加的类型,然后增加很多会很不方便。 如果动态增加呢?
此时,使用spring的初始化bean的afterPropertiesSet进行初始化。
afterPropertiesSet方法在初始化bean时执行,可以为特定bean配置。 afterPropertiesSet必须实现初始化bean接口。 要实现InitializingBean接口,必须实现afterPropertiesSet方法。
void afterPropertiesSet () throws Exception; 在所有属性初始化后调用此方法。 多个初始化
在多种情况下,例如使用map。 这几乎相同,不加@Resource注释
私有静态映射,shapefuncshapetypemap=newconcurrenthashmap (; ShapeFactory接口: @ sl F4 jpublicclassshapefactory { privatestaticclassshapefactoryhanlde { privatestaticshapefactoryinnerctory privateshapefactory { } publicstaticshapefactorygetinstance { } returnshapefactoryhanlde.inner class single; }私有静态映射,shapefuncshapetypemap=newconcurrenthashmap (; 公共语音注册器(字符串名称,ShapeFunc shapeFunc ) ) if (! shapetypemap.containskey(name ) ) shapetype map.put (name,shapeFunc ); }else{log.warn(shapefu
ncFactory中存在重复注册的[name=" + name + "]类!"); } } public ShapeFunc getShape(String type) throws Exception { if (StringUtils.isEmpty(type)) { throw new Exception("没有找到"+type+"类"); } return shapeTypeMap.getOrDefault(type, null); }}单例使用静态内部类
代码:
Shape接口: public interface ShapeFunc { void execute(String action);} AbstractShapeFunc @Slf4jpublic abstract class AbstractShapeFunc implements ShapeFunc, InitializingBean { protected abstract String supportType(); @Override public void afterPropertiesSet(){ ShapeFactory.getInstance().register(supportType(), this); }}
这个类是重点,要实现 InitializingBean 里面的afterPropertiesSet方法
CircleService @Componentpublic class CircleService extends AbstractShapeFunc { @Override public void execute(String action) { System.out.println("圆形: "+action); } @Override protected String supportType() { return "circle"; }}
RectangleService @Componentpublic class RectangleService extends AbstractShapeFunc { @Override public void execute(String action) { System.out.println("正方形: "+action); } @Override protected String supportType() { return "rectangle"; }} TriangleService @Componentpublic class TriangleService extends AbstractShapeFunc { @Override public void execute(String action) { System.out.println("三角形: "+action); } @Override protected String supportType() { return "triangle"; }} TrapezoidService @Componentpublic class TrapezoidService extends AbstractShapeFunc { @Override public void execute(String action) { System.out.println("梯形: "+action); } @Override protected String supportType() { return "trapezoid"; }}
ShapeFuncsService @Servicepublic class ShapeFuncsService { public void execute(String shapeType, String action) throws Exception { ShapeFactory.getInstance().getShape(shapeType).execute(action); }}
测试: import lombok.extern.slf4j.Slf4j;import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.test.context.junit4.SpringRunner;import javax.annotation.Resource;@RunWith(SpringRunner.class)@SpringBootTest@Slf4jpublic class TestBeanInitAdd { @Resource private ShapeFuncsService shapeFuncsService; @Test public void testInit() throws Exception { shapeFuncsService.execute("circle", "画一个圆形"); shapeFuncsService.execute("rectangle", "画一个正方形"); shapeFuncsService.execute("triangle", "画一个三角形"); shapeFuncsService.execute("trapezoid", "画一个梯形"); }}
结果:
圆形: 画一个圆形正方形: 画一个正方形三角形: 画一个三角形梯形: 画一个梯形总结:
在实例化的时候,类比较多的,相对固定的话,可以用static的方式。如果是不固定,不定期会加的话,使用 spring里面的 InitializingBean 的 afterPropertiesSet方法,根据方法名去注册和执行。