Spring在Java Filter注入Bean为Null的问题解决

在Spring的自动注入中普通的POJO类都可以使用@Autowired进行自动注入,但是除了两类:Filter和Servlet无法使用自动注入属性。(因为这两个归Web容器管理)可以用init(集承自HttpServlet后重写init方法)方法中实例化对象。

解决方法:

其中涉及到五种Spring实例化容器对象:

方法一(这种方式不符合Web工程,不要使用):在初始化时保存ApplicationContext对象

ApplicationContext ac = new FileSystemXmlApplicationContext("applicationContext.xml");
ac.getBean("beanId");

说明:这种方式适用于采用Spring框架的独立应用程序,需要程序通过配置文件手工初始化Spring的情况。

方法二(这种方式最简单):通过Spring提供的工具类获取ApplicationContext对象

import org.springframework.web.context.support.WebApplicationContextUtils;
ApplicationContext ac1
= WebApplicationContextUtils.getRequiredWebApplicationContext(ServletContext sc); ApplicationContext ac2 = WebApplicationContextUtils.getWebApplicationContext(ServletContext sc); ac1.getBean("beanId"); ac2.getBean("beanId");

说明:这种方式适合于采用Spring框架的B/S系统,通过ServletContext对象获取ApplicationContext对象,然后在通过它获取需要的类实例。上面两个工具方式的区别是,前者在获取失败时抛出异常,后者返回null。

实例:

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest)request;
        HttpServletResponse resp = (HttpServletResponse)response;
        ServletContext sc = req.getSession().getServletContext();
        XmlWebApplicationContext cxt = (XmlWebApplicationContext)WebApplicationContextUtils.getWebApplicationContext(sc);
        
        if(cxt != null && cxt.getBean("usersService") != null && usersService == null)
            usersService = (UsersService) cxt.getBean("usersService");
        
        Users users = this.usersService.queryByOpenid(openid);
public class WeiXinFilter implements Filter{
    
    private UsersService usersService;
    
    public void init(FilterConfig fConfig) throws ServletException {
        ServletContext sc = fConfig.getServletContext(); 
        XmlWebApplicationContext cxt = (XmlWebApplicationContext)WebApplicationContextUtils.getWebApplicationContext(sc);
        
        if(cxt != null && cxt.getBean("usersService") != null && usersService == null)
            usersService = (UsersService) cxt.getBean("usersService");        
    }

注意:如果在Spring Boot项目上XmlWebApplicationContext可以不用要,直接使用WebApplicationContext替代。

方法三:继承自抽象类ApplicationObjectSupport

说明:抽象类ApplicationObjectSupport提供getApplicationContext()方法,可以方便的获取到ApplicationContext。

Spring初始化时,会通过该抽象类的setApplicationContext(ApplicationContext context)方法将ApplicationContext对象注入。

方法四:继承自抽象类WebApplicationObjectSupport

说明:类似上面方法,调用getWebApplicationContext()获取WebApplicationContext

方法五:实现接口ApplicationContextAware

说明:实现该接口的setApplicationContext(ApplicationContext context)方法,并保存ApplicationContext 对象。Spring初始化时,会通过该方法将ApplicationContext对象注入。

 

参考:

http://blog.csdn.net/angel708884645/article/details/51148865

http://www.cnblogs.com/digdeep/p/4770004.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot中,配置过滤器可以通过以下三种方法实现: 方法一:实现Filter接口 首先,我们需要创建一个过滤器类,实现Filter接口,并重写其中的doFilter()方法。在该方法中,可以编写具体的过滤操作。例如,我们可以创建一个名为MyFilter的过滤器类,实现Filter接口,并在其中重写doFilter()方法。在doFilter()方法中,我们可以输出一句话,并调用FilterChain的doFilter()方法继续处理请求。 方法二:通过配置类注册过滤器 在Spring Boot中,可以通过实现WebMvcConfigurer或WebFluxConfigurer接口,并重写addFilter()方法来配置过滤器。在配置类中,我们可以使用@Bean注解来注册过滤器。例如,我们可以创建一个名为WebFilterConfig的配置类,并在其中注册MyFilter过滤器。在注册过程中,我们可以设置过滤器的URL模式和执行顺序。 方法三:使用@WebFilter注解 另一种方式是使用@WebFilter注解来配置过滤器。我们可以在过滤器类上添加@WebFilter注解,并设置相应的属性。在Spring Boot中,这种方式也是常用的配置过滤器的方法。 总结起来,Spring Boot中配置Filter过滤器的方法包括实现Filter接口、通过配置类注册过滤器以及使用@WebFilter注解。根据具体的需求和使用场景,选择合适的方法进行配置即可。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Springboot如何配置过滤器](https://blog.csdn.net/qq_60458298/article/details/129737940)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [SpringBoot 过滤器 filter 3种方法](https://blog.csdn.net/bzu_mei/article/details/126644963)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值