I've created an ContainerRequestFilter
implementation like this:
@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.
}
}
As you can see I'm trying to inject a MemcachedApplicationResources
object into memcachedResources
field.
MemcachedApplicationResources
is like:
@ApplicationScoped
public class MemcachedApplicationResources {}
Why is it null
?
EDIT
I've just created a beans.xml
file with this content:
<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>
However, it's still null
.
EDIT 2
I've also tried to create an Filter
instead of a 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!
}
}
Why using a Filter
class the field is injected?
The injection should work if:
beans.xml
file under WEB-INF
(not mandatory for CDI 1.2). Depending on your container, you may require an extra dependency to make CDI works with RESTEasy, but it should work out-of-the-box with WildFly. See the documentation for more details.
If it doesn't work, you still can try to get the instance programmaticaly using:
MemcachedApplicationResources bean =
CDI.current().select(MemcachedApplicationResources.class).get();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.