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成员
@Componentpublic class AuthUtil {@Autowiredprivate HttpServletRequest request;private static HttpServletRequest staticRequest;private static ThreadLocal<UserContext> userContextThreadLocal = new ThreadLocal();private static String TEAM_ID = "teamId";@PostConstructpublic void init(){staticRequest=request;}
4.策略模式,同类型多业务bean处理时的优化方案
/*** 描述:把继承了相同接口的处理类的beanName放入SchedulerMode枚举中* 使复杂的业务处理又统一的入口* 例如:接入各个银行支付系统,各个银行提供的支付接口都类似,通过策略模式不同的银行调取自己的接口bean** @author 刘维* @创建时间: 2019-08-02 11:26*/@Service@Slf4jpublic class SchedulerStrategyContext {private final Map<String, SimScheduler> strategyMap = new ConcurrentHashMap<>();public SimScheduler getStrategy(SchedulerMode schedulerMode) {Object bean = SpringBeanUtil.getBean(schedulerMode.getSchedulerClass());if (bean == null) {log.error("does not exists strategy :{}", schedulerMode.getSchedulerClass().getName());}return (SimScheduler)bean;}public List<SimApplication> doSchedule(SchedulerMode schedulerMode) {Assert.notNull(getStrategy(schedulerMode),"## getStrategy mode:" + schedulerMode.getSchedulerClass() + " is null");return getStrategy(schedulerMode).schedule();}}
根据springBeanName获取bean
/*** @author 刘维wb390092* @date 2019/05/10*/@Componentpublic class SpringBeanUtil implements ApplicationContextAware {private static ApplicationContext applicationContext;@Overridepublic void setApplicationContext(ApplicationContext applicationContext) throws BeansException {SpringBeanUtil.applicationContext=applicationContext;}public static Object getBean(String beanName){return applicationContext.getBean(beanName);}public static Object getBean(Class c){return applicationContext.getBean(c);}}
