background

  • Recently, I have a special urgent demand, which is to be developed today and demonstrated tomorrow, and the demand meeting is to be held at 16:00. I heard that the amount of money involved is over 100 million (MMP, please share it with me).

Demand for content

  • Had developed a new version of the basic information, contains the school information, information, grade, class information, student information, such as 13 categories, now want to do a statistical page in the department of education, statistical bureau of education school related information below, related to web side you peace, is a word to all night tonight, tomorrow morning to demonstrate (hearing mentality are collapse, f * *k!) .

Decomposition of demand

  • Considering the urgency, it is not necessary to think about whether expansion or maintenance is good or not. The 13 categories are divided into 4 modules according to similar functions, namely 4 tables, which greatly reduces the time of table building.

  • Since the request and pass parameters are basically the same, an interface is provided externally, using the policy pattern and uniform interface implementation

  • The data in the table, it is impossible to write the relevant logic to achieve, to write the relevant logic, not half a month to do, but also how to do, manual filling, when the page is not displayed on the emergency ability of the product or let him bypass.

Interface implementation

  • I’ve written about the integration of the strategy pattern with Spring before, and those of you who are interested can check it out
  • interface
public interface IBaseStatisticalService {

    StatisticalRespDTO query(StatisticalReqDTO dto);
}
Copy the code
  • controller
@postMapping ("/query") @description (" query") public resultresptrespdto Query (@requestBody StatisticalReqDTO dto) {return Rets.success(this.handler.query(dto)); }Copy the code
  • Policy Pattern invocation (core)
``` @Autowired private Map<String, IBaseStatisticalService> baseStatisticalServiceMap ; public StatisticalRespDTO query(StatisticalReqDTO dto) { logger.info("BaseStatisticalHandler.query,dto:{}",dto); If (Objects. IsNull (dto)) {ExceptionEnum. ThrowException (ExceptionEnum FAILED, "parameter is null"); } String key = BaseStatisticalConstance.BASE_STATISTICAL + dto.getType(); IBaseStatisticalService baseStatisticalService = baseStatisticalServiceMap.get(key); If (Objects. IsNull (baseStatisticalService)) {ExceptionEnum. ThrowException (ExceptionEnum FAILED, "corresponding to the type does not exist"); } return baseStatisticalService.query(dto); }Copy the code
  • As long as the related classification implements the interface to realize its own business, it can be managed in a unified manner and can be extended better. When I was proud of myself, other colleagues said there was a problem. Now we are developing according to 4 tables, that is, 4 service classes should contain 13 types. This one I designed is based on beanName, and spring only has one beanName by default, which means 13 classes are required. This kind of development is disgusting. There is no retreat!
  • Spring’s beanName supports a bean with multiple names. Spring’s beanName supports a bean with multiple names. Spring’s beanName supports a bean with multiple names.
  • Custom annotation
@Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited public @interface BaseTypeMapping  { String[] types() default {}; }Copy the code
  • Implementation class uses
@Service @BaseTypeMapping(types = {BaseStatisticalConstance.CLASS,BaseStatisticalConstance.GRADE,BaseStatisticalConstance.DEPARTMENT}) public class BaseStatisticalGroupServiceImpl extends ServiceImpl<BaseStatisticalGroupMapper, BaseStatisticalGroup> implements BaseStatisticalGroupService , IBaseStatisticalService { @Autowired private BaseStatisticalGroupMapper baseStatisticalGroupMapper; @override public StatisticalRespDTO Query (StatisticalReqDTO dto) {// Business code}}Copy the code
  • To implement ApplicationContextAware, the complete code is as follows
@Component public class BaseStatisticalHandler implements ApplicationContextAware { private final static Logger logger =  LoggerFactory.getLogger(BaseStatisticalHandler.class); private Map<String, IBaseStatisticalService> baseStatisticalServiceMap = new ConcurrentHashMap<>(); public StatisticalRespDTO query(StatisticalReqDTO dto) { logger.info("BaseStatisticalHandler.query,dto:{}",dto); If (Objects. IsNull (dto)) {ExceptionEnum. ThrowException (ExceptionEnum FAILED, "parameter is null"); } String key = BaseStatisticalConstance.BASE_STATISTICAL + dto.getType(); IBaseStatisticalService baseStatisticalService = baseStatisticalServiceMap.get(key); If (Objects. IsNull (baseStatisticalService)) {ExceptionEnum. ThrowException (ExceptionEnum FAILED, "corresponding to the type does not exist"); } return baseStatisticalService.query(dto); } @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { Map<String,  IBaseStatisticalService> map = applicationContext.getBeansOfType(IBaseStatisticalService.class); for (Map.Entry<String, IBaseStatisticalService> entry : map.entrySet()) { IBaseStatisticalService value = entry.getValue(); String key = entry.getKey(); logger.info("{}:{}:{}", key, value.getClass(), value.getClass().isAnnotationPresent(BaseTypeMapping.class)); BaseTypeMapping annotation = value.getClass().getAnnotation(BaseTypeMapping.class); if (annotation == null) { continue; } String[] types = annotation.types(); for (String type : types) { logger.info("types:{},beanName:{},class:{}", types, key, value); String basetKey = BaseStatisticalConstance.BASE_STATISTICAL+ type; If (baseStatisticalServiceMap. Either containsKey (key)) {/ / class already exists IBaseStatisticalService baseStatisticalService = baseStatisticalServiceMap.get(key); Logger. The error (" IBaseStatisticalService, type: {}, beanName: {}, class: {} has defined ", the type, the key, baseStatisticalService); throw new BeanCurrentlyInCreationException(key); } baseStatisticalServiceMap.put(basetKey, value); }}}}Copy the code

My local tried, no problem, happy! Promote to colleagues, according to this set of standards to achieve, in the process of joint adjustment did not find problems, knock happy!

conclusion

Above is my emergency needs in the relevant treatment, may be a lot of not in place, there are also unreasonable places, I hope we point out together, learn from each other and mutual achievements!