目录
动态初始化类 InitializingBean 的 afterPropertiesSet方法
多个初始化
ShapeFactory接口:
Shape接口:
AbstractShapeFunc
CircleService
RectangleService
TriangleService
TrapezoidService
ShapeFuncsService
测试:
总结:
学习了《动态初始化类》在调用的方法的时候, 需要事先先确定增加的类型,如果后续增加很多,就变得不方便了。如果动态增加了呢?
这时候,要用到spring的 InitializingBean 的 afterPropertiesSet 来初始化。
afterPropertiesSet方法,初始化bean的时候执行,可以针对某个具体的bean进行配置。afterPropertiesSet 必须实现 InitializingBean接口。实现 InitializingBean接口必须实现afterPropertiesSet方法。
void afterPropertiesSet() throws Exception;
这个方法将在所有的属性被初始化后调用。多个初始化
多个的时候,比如使用map,这个差不多,不加@Resource注解了
private static MapShapeFactory接口:shapeTypeMap = new ConcurrentHashMap<>();
@Slf4j public class ShapeFactory { private static class ShapeFactoryHanlde { private static ShapeFactory innerClassSingle = new ShapeFactory(); }; private ShapeFactory() { } public static ShapeFactory getInstance() { return ShapeFactoryHanlde.innerClassSingle; } private static MapshapeTypeMap = new ConcurrentHashMap<>(); public void register(String name, ShapeFunc shapeFunc) { if (!shapeTypeMap.containsKey(name)) { shapeTypeMap.put(name, shapeFunc); } else { log.warn("ShapeFuncFactory中存在重复注册的[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
@Slf4j public abstract class AbstractShapeFunc implements ShapeFunc, InitializingBean { protected abstract String supportType(); @Override public void afterPropertiesSet(){ ShapeFactory.getInstance().register(supportType(), this); } }
这个类是重点,要实现 InitializingBean 里面的afterPropertiesSet方法
CircleService@Component public class CircleService extends AbstractShapeFunc { @Override public void execute(String action) { System.out.println("圆形: "+action); } @Override protected String supportType() { return "circle"; } }RectangleService
@Component public class RectangleService extends AbstractShapeFunc { @Override public void execute(String action) { System.out.println("正方形: "+action); } @Override protected String supportType() { return "rectangle"; } }TriangleService
@Component public class TriangleService extends AbstractShapeFunc { @Override public void execute(String action) { System.out.println("三角形: "+action); } @Override protected String supportType() { return "triangle"; } }TrapezoidService
@Component public class TrapezoidService extends AbstractShapeFunc { @Override public void execute(String action) { System.out.println("梯形: "+action); } @Override protected String supportType() { return "trapezoid"; } }
ShapeFuncsService
@Service public 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 @Slf4j public 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方法,根据方法名去注册和执行。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)