动态初始化类 InitializingBean 的 afterPropertiesSet方法

 动态初始化类 InitializingBean 的 afterPropertiesSet方法

目录

 动态初始化类 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 Map<String, ShapeFunc> shapeTypeMap = new ConcurrentHashMap<>();

ShapeFactory接口:

@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 Map<String, ShapeFunc> shapeTypeMap = 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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天狼1222

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值