动态初始化类 InitializingBean 的 afterPropertiesSet方法
目录
学习了《动态初始化类 InitializingBean》,结合泛化参数一起使用,这样就更完整些,也更实用些。
添加的类
ShapeRequest<T>
@Data
@ToString
@ApiModel("请求数据")
public class ShapeRequest<T> {
@ApiModelProperty("类型 方法名")
private String type;
@ApiModelProperty("参数")
private T param;
}
不变的接口:
ShapeFactory
GenericUtils
变化的类:
代码:
Shape接口:
/**
* @param <I> 入参类型
*/
public interface ShapeFunc<I> {
void execute(ShapeRequest<I> shapeRequest);
}
AbstractShapeFunc
@Slf4j
public abstract class AbstractShapeFunc<I> implements ShapeFunc<I>, InitializingBean {
private static final ObjectMapper MAPPER = new ObjectMapper();
@Override
public void execute(ShapeRequest<I> shapeRequest) {
try {
I param = exchangeParam(shapeRequest.getParam());
doExecute(param);
} catch (Throwable ex) {
ex.printStackTrace();
}
}
protected abstract void doExecute(I requestParam);
private I exchangeParam(Object data) throws IOException {
Class requestGenricType = GenericUtils.getSuperClassGenricType(getClass(), 0);
if (!Map.class.isAssignableFrom(requestGenricType)) {
return (I) MAPPER.readValue(MAPPER.writeValueAsString(data), requestGenricType);
}
return (I) data;
}
protected abstract String supportType();
@Override
public void afterPropertiesSet(){
ShapeFactory.getInstance().register(supportType(), this);
}
}
添加了参数处理的方法,添加了一个doExcute方法,用于子类继承的时候用。
CircleService
@Component
public class CircleService extends AbstractShapeFunc<CircleRequest> {
@Override
protected void doExecute(CircleRequest requestParam) {
System.out.println("圆形: "+requestParam.getColor() + requestParam.getRadius());
}
@Override
protected String supportType() {
return "circle";
}
}
RectangleService
@Component
public class RectangleService extends AbstractShapeFunc<Map<String,Object>> {
@Override
protected void doExecute(Map<String,Object> requestParam){
System.out.println("正方形: "+requestParam.get("rec"));
}
@Override
protected String supportType() {
return "rectangle";
}
}
TriangleService
@Component
public class TriangleService extends AbstractShapeFunc<List<Map<String,Object>>> {
@Override
protected void doExecute(List<Map<String, Object>> requestParam){
System.out.println("三角形:"+requestParam.toString());
}
@Override
protected String supportType() {
return "triangle";
}
}
TrapezoidService
@Component
public class TrapezoidService extends AbstractShapeFunc<String> {
@Override
protected void doExecute(String requestParam) {
System.out.println("梯形: "+requestParam);
}
@Override
protected String supportType() {
return "trapezoid";
}
}
ShapeFuncsService
@Service
public class ShapeFuncsService {
public void execute(ShapeRequest shapeRequest) throws Exception {
ShapeFactory.getInstance().getShape(shapeRequest.getType()).execute(shapeRequest);
}
}
测试:
@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
public class TestBeanInitAdd {
@Resource
private ShapeFuncsService shapeFuncsService;
@Test
public void testInit2() throws Exception {
ShapeRequest shapeRequest = new ShapeRequest();
shapeRequest.setType("circle");
CircleRequest circleRequest = new CircleRequest();
circleRequest.setColor("blue");
circleRequest.setRadius("12.22f");
shapeRequest.setParam(circleRequest);
shapeFuncsService.execute(shapeRequest);
shapeRequest = new ShapeRequest();
shapeRequest.setType("rectangle");
Map<String,Object> single = new HashMap<>();
single.put("rec","yan");
shapeRequest.setParam(single);
shapeFuncsService.execute(shapeRequest);
shapeRequest = new ShapeRequest();
shapeRequest.setType("triangle");
List<Map<String,Object>> singles = new ArrayList<>();
single.put("dream","do things well");
singles.add(single);
shapeRequest.setParam(singles);
shapeFuncsService.execute(shapeRequest);
shapeRequest = new ShapeRequest();
shapeRequest.setType("trapezoid");
shapeRequest.setParam("画一个梯形");
shapeFuncsService.execute(shapeRequest);
}
}
结果:
圆形: blue12.22f
正方形: yan
三角形:[{rec=yan, dream=do things well}]
梯形: 画一个梯形
总结:
结合参数泛型化的获取,这样程序的兼容性就更好了,支持各种参数类型的传递。对于返回值的处理就简单些了。加上返回值就完整了。