Class and method to get the instance of the class using the annotation affairs @Transactional

Today in doing the project, due to the many types, it turned into a similar strategy mode, do not use if else, to facilitate post expansion

All the implementation class inherit the same interface, and then an unrelated class implementation class all constructed

@Service
public class SendTemplateService {

    Map<String, WechatSendTemplateService> wechatSendTemplateServiceMap = new HashMap<>();

    // 构造函数,如果你是集合接口对象,那么就会把spring容器中所有关于该接口的子类,全部抓出来放入到集合中,如果子类使用了@Transactional事务注解,所获取的类都是代理实例,获取的类名也是附带代理实例信息
    public SendTemplateService(List<WechatSendTemplateService> wechatSendTemplateServices) {
        for (WechatSendTemplateService wechatSendTemplateService : wechatSendTemplateServices) {
            wechatSendTemplateServiceMap.put(wechatSendTemplateService.getClass().getSimpleName()
                    .replace("SendTemplateServiceImpl", "").toUpperCase(), wechatSendTemplateService);
        }
    }
}

The idea is to achieve all of the classes in map, the name of the convention into their own key. map of key put the time into the all caps, easy match

Using key match, I noticed a phenomenon, not a class with a name to match

Note the back of springcglib, with a look that is a proxy mode. There are two classes of information did not take the class name of the proxy instance, all other information with the proxy instance

@Transactional two classes with no annotation is no difference between the code band

Everything else is using the transaction @Transactional comment.

Suddenly remembered Spring's transaction with the AOP proxy mode is achieved. Therefore, the use of the class will be proxied @Transactional

Published 288 original articles · won praise 88 · views 430 000 +

Guess you like

Origin blog.csdn.net/ypp91zr/article/details/103618550