Springboot:过滤器中的依赖注入不起作用

huangapple 未分类评论47阅读模式
英文:

Springboot : Dependency injection in the filter does not work

问题

以下是翻译好的内容:

我正在开发一个Spring Boot应用程序。其中包含多个过滤器。对于新增的一个过滤器,我想要注入一个Bean。然而,在尝试注入该Bean时,我遇到了以下错误:

方法引发了'java.lang.UnsupportedOperationException'异常。无法评估com.test.api.filter.RContext$$EnhancerBySpringCGLIB$$4bbc6342.toString()

过滤器:

@Component
public class ContextFilter implements Filter {
    private final static Logger LOG = LoggerFactory.getLogger(ContextFilter.class);

    @Autowired
    RContext rContext;

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        LOG.info("正在处理上下文过滤器");
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) request;
        LOG.info("开始...", 
        rContext.setContextData("测试数据");
        LOG.info("完成...", req.getRequestURI());
    }
    
    @Override
    public void destroy() {

    }
}

RContext类:

@Component
@Scope(value="request", proxyMode=ScopedProxyMode.TARGET_CLASS)
public class RContext {

    String user;
    String contextData;
    String activeProfile;

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public String getContextData() {
        return contextData;
    }

    public void setContextData(String contextData) {
        this.contextData = contextData;
    }

    public String getActiveProfile() {
        return activeProfile;
    }

    public void setActiveProfile(String activeProfile) {
        this.activeProfile = activeProfile;
    }
}
英文:

I am working on a springboot application. This contains multiple filters. For one of the newly introduced filter, I want to inject a bean.However I am getting following error while trying to inject the bean:

Method threw 'java.lang.UnsupportedOperationException' exception. Cannot evaluate com.test.api.filter.RContext$$EnhancerBySpringCGLIB$$4bbc6342.toString()

Filter:

@Component
public class ContextFilter implements Filter {
    private final static Logger LOG = LoggerFactory.getLogger(ContextFilter.class);

    @Autowired
    RContext rContext;

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        LOG.info("Processing context filter");
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest req = (HttpServletRequest) request;
        LOG.info("Start..", 
        rContext.setContextData("Test data");
        LOG.info("Done..", req.getRequestURI());
    }
    
    @Override
    public void destroy() {

    }
}

RContext class:

@Scope(value="request", proxyMode= ScopedProxyMode.TARGET_CLASS)
public class RContext {

    String user;
    String contextData;
    String activeProfile;

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public String getContextData() {
        return contextData;
    }

    public void setContextData(String contextData) {
        this.contextData = contextData;
    }

    public String getActiveProfile() {
        return activeProfile;
    }

    public void setActiveProfile(String activeProfile) {
        this.activeProfile = activeProfile;
    }
}




</details>


# 答案1
**得分**: 0

如果您可以使用@Service对RContext类进行注解,那么在doFilter方法中可能可以使用以下解决方案:

```java
RContext rContext;

ServletContext servletContext = request.getServletContext();
WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(servletContext);
rContext = wac.getBean(RContext.class);
英文:

If you could annotate the RContext class with @Service, then the following solution might work in the doFilter method:

RContext rContext;

ServletContext servletContext = request.getServletContext();
WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(servletContext);
rContext = wac.getBean(RContext.class);

huangapple
  • 本文由 发表于 2020年5月29日 19:20:44
  • 转载请务必保留本文链接:https://java.coder-hub.com/62084755.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定