【发布时间】:2018-01-27 01:33:51
【问题描述】:
我创建了一个像这样的ContainerRequestFilter 实现:
@Provider
@PreMatching
@Secured
@Dependent
public class BearerFilter implements ContainerRequestFilter
{
@Inject protected MemcachedApplicationResources memcachedResources;
@Override
public void filter(ContainerRequestContext requestContext) throws IOException
{
//this.memcachedResources is null here.
}
}
如您所见,我正在尝试将MemcachedApplicationResources 对象注入memcachedResources 字段。
MemcachedApplicationResources 就像:
@ApplicationScoped
public class MemcachedApplicationResources {}
为什么是null?
编辑
我刚刚创建了一个包含以下内容的beans.xml 文件:
<beans xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
</beans>
不过,还是null。
编辑 2
我也尝试过创建Filter 而不是ContainerRequestFilter:
@WebFilter(
dispatcherTypes = {DispatcherType.REQUEST },
urlPatterns = { "/cmng/*" },
initParams = { @WebInitParam(name = "excludedPaths", value = "log") }
)
public class BearerWebFilter implements Filter
{
@Inject protected MemcachedResources memcachedResources;
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException
{
//here, this.memcachedResources is a injected proxy!
}
}
为什么使用Filter 类来注入字段?
【问题讨论】:
-
你的
beans.xml喜欢什么? -
我没有任何
beans.xml文件。 -
如果您不使用 CDI 1.2,
beans.xml是强制性的。否则注入将不起作用。 -
我正在使用与 JAX-RS 无关的实现。目前,我在 Wildfly 10.1.0.Final 上部署我的 Web 应用程序,该版本具有最新的 RestEasy 实现。
-
对于您的 Web 应用程序,在
WEB-INF文件夹下创建一个空的beans.xml文件。
标签: jakarta-ee jax-rs cdi