在SpringBoot项目中,因为省去了spring原始的xml的配置,导致大家对项目各部分的加载顺序不清楚怎么控制,
最近在写项目时遇到了一个小问题,因为filter进行过滤时,需要使用到数据库中的白名单列表,就要求在filter中使用,
但是通过@Autowired注入的dao在filter中使用的时候会报空,因为还没有注入bean容器,这时就要求我们提前手动加载,
在filter的init方法中简单实现下即可达到目的,
@Override public void init(FilterConfig config) throws ServletException { // TODO Auto-generated method stub ServletContext servletContext = config.getServletContext(); ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext); dspUserWhiteListDAO = (DspUserWhiteListDAO) ctx.getBean("dspUserWhiteListDAO"); }