Injection fails in rewrite processor. Injected components are always null

Splash Forums PrettyFaces Users Injection fails in rewrite processor. Injected components are always null

This topic contains 2 replies, has 2 voices, and was last updated by  Christian Kaltepoth 5 years, 7 months ago.

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #18197

    marsand
    Participant

    I’m having trouble using injection inside of a pretty faces rewrite processor and could use some help. Whenever the component I am trying to inject is always null. I’m using the following:

    prettyfaces-jsf2 3.3.1.20110902.070731.20

    cdi 1.0

    jsf 2.1.2

    pretty-config.xml

    <pretty-config
    xmlns="http://ocpsoft.com/prettyfaces/3.2.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://ocpsoft.com/prettyfaces/3.2.0 http://ocpsoft.com/xml/ns/prettyfaces/ocpsoft-pretty-faces-3.2.0.xsd">
    <rewrite match="^/redirect/myRedirect"
    processor="com.mypackage.MyRewriteProcessor" redirect="302" />
    </pretty-config>

    public class MyRewriteProcessor implements Processor {

    @Inject
    private MyService myService;

    public String processInbound(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse,
    RewriteRule rewriteRule, String s) {
    myService.doSomething();
    }
    }

    @ApplicationScoped
    @Named
    @Retention(RUNTIME)
    @Stereotype
    @Target(TYPE)
    public class MyService {

    public void doSomething(){...}
    }

    myService.doSomething() always produces a NPE in the processor. Am I approaching this from the wrong angle, or is there some issue with this not happening in the correct lifecycle phase?

    Any guidance is appreciated.

    Thanks!

    #22221

    The problem is that PrettyFaces creates an instance of the rewrite processor itself. So CDI isn’t involved here. Therefore @Inject won’t have any effect. Your only option is to manually obtain the BeanManager and lookup the service for yourself. I did something similar in the past and created a simple helper class for this. Unfortunately I currently don’t have access to the code. But I will post it later today as soon as I’m in the office.

    #22222

    I think the easiest way for you is to use some library for this. Deltaspike for example contains a helper class called BeanProvider that can be used to manually look up beans like this:

    MyService myService = BeanProvider.getContextualReference(MyService.class, false);

    To include Deltaspike in your project just add this to your pom:

    <dependency>
    <groupId>org.apache.deltaspike.core</groupId>
    <artifactId>deltaspike-core-impl</artifactId>
    <version>0.1-incubating</version>
    </dependency>

    I think other libraries like Seam Solder and MyFaces CODI provide similar functionality.

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.

Comments are closed.