1. springmvc接受数组

1.1 get 如果请求参数是地址栏&拼接

api/param?a=1&a=2,VO对象中的list可接受,或者直接函数参数表数组[]接收

1.2 get 前段发送json对象

此时浏览器调试栏可看到参数变成了a[]=1,Vo对象中的list无法接收
如果非要使用参数表数组[]接收,使用@RequestParam(“a[]”)
推荐:前端把数组转成逗号分隔的字符串,此时vo.list可直接接收

2.HttpServletRequest支持直接@Autowired注入了

此时注入的是动态代理类,不是真正的request,真正的request藏在代理类的ThreadLocal中

3.spring bean封装成util类

使用注解PostConstruct把注入的bean对象变成static成员

  1. @Component
  2. public class AuthUtil {
  3. @Autowired
  4. private HttpServletRequest request;
  5. private static HttpServletRequest staticRequest;
  6. private static ThreadLocal<UserContext> userContextThreadLocal = new ThreadLocal();
  7. private static String TEAM_ID = "teamId";
  8. @PostConstruct
  9. public void init(){
  10. staticRequest=request;
  11. }

4.策略模式,同类型多业务bean处理时的优化方案

  1. /**
  2. * 描述:把继承了相同接口的处理类的beanName放入SchedulerMode枚举中
  3. * 使复杂的业务处理又统一的入口
  4. * 例如:接入各个银行支付系统,各个银行提供的支付接口都类似,通过策略模式不同的银行调取自己的接口bean
  5. *
  6. * @author 刘维
  7. * @创建时间: 2019-08-02 11:26
  8. */
  9. @Service
  10. @Slf4j
  11. public class SchedulerStrategyContext {
  12. private final Map<String, SimScheduler> strategyMap = new ConcurrentHashMap<>();
  13. public SimScheduler getStrategy(SchedulerMode schedulerMode) {
  14. Object bean = SpringBeanUtil.getBean(schedulerMode.getSchedulerClass());
  15. if (bean == null) {
  16. log.error("does not exists strategy :{}", schedulerMode.getSchedulerClass().getName());
  17. }
  18. return (SimScheduler)bean;
  19. }
  20. public List<SimApplication> doSchedule(SchedulerMode schedulerMode) {
  21. Assert.notNull(getStrategy(schedulerMode),
  22. "## getStrategy mode:" + schedulerMode.getSchedulerClass() + " is null");
  23. return getStrategy(schedulerMode).schedule();
  24. }
  25. }

根据springBeanName获取bean

  1. /**
  2. * @author 刘维wb390092
  3. * @date 2019/05/10
  4. */
  5. @Component
  6. public class SpringBeanUtil implements ApplicationContextAware {
  7. private static ApplicationContext applicationContext;
  8. @Override
  9. public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
  10. SpringBeanUtil.applicationContext=applicationContext;
  11. }
  12. public static Object getBean(String beanName){
  13. return applicationContext.getBean(beanName);
  14. }
  15. public static Object getBean(Class c){
  16. return applicationContext.getBean(c);
  17. }
  18. }