7

Is there a way to use @Inject or @EJB in a CXF interceptor? I know I can still do a JNDI lookup but I'd rather avoid it.

I find it weird that JAX-WS handlers are managed but CXF interceptors are not. Is it possible to make them managed? I'm using annotations to add my interceptors to the endpoint (@org.apache.cxf.interceptor.InInterceptors and @org.apache.cxf.interceptor.InInterceptors), could it be handled with a configuration file?

Configuration:

  • Java 6
  • JBoss EAP 6.1 (AS 7.2)
  • CXF 2.6.6
MWiesner
  • 8,868
  • 11
  • 36
  • 70
Virginie
  • 909
  • 3
  • 12
  • 32
  • has there been any update in 2 years ? Do I start bounty ? – ulab Nov 17 '17 at 09:08
  • 1
    @ulab No, no update as far as I know, CXF interceptors are still not managed by the container. But if you use CDI 1.1+, you can use CDI.current(), see https://stackoverflow.com/questions/24798529/how-to-programmatically-inject-a-java-cdi-managed-bean-into-a-local-variable-in – Virginie Nov 17 '17 at 15:20
  • Thank you for the response. I had just figured it and got it working that way. And yes I use Wildfly which supports JEE 7. – ulab Nov 17 '17 at 15:28
  • @ulab: I'm quite curious how you got this to work.. If I use the constructor of the interceptor to lookup by means of CDI.current() I get: WFLYWELD0039: Singleton not set for org.jboss.ws.common.utils.DelegateClassLoader – Sjaak May 28 '19 at 06:36
  • @Sjaak my use case was simple. I tried to get in `handleMessge()` and not in the constructor. Please see my answer below. – ulab May 29 '19 at 08:55
  • @ulab. Thanks. That would be very similar to having a request scoped lookup. Since there is a jndi lookup behind the object I factorise I was looking for a lookup with the same scope as the bean, which implements the webservice. But I realize I can possibly work around this with an application scoped bean producing the remote bean I actually need in order to be more efficient (save jndi lookup) – Sjaak May 30 '19 at 09:32
  • It all feels a bit clunky. Would be so much better if I could directly inject on the constructor of the interceptor – Sjaak May 30 '19 at 09:33

1 Answers1

2

I have injected with the help of CDI 1.1 as below.

beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<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>

cxf-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cxf="http://cxf.apache.org/core"
    xsi:schemaLocation="
http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="callerInfoInterceptor" class="my.CallerInfoInterceptor" />

    <cxf:bus>
        <cxf:inInterceptors>
            <ref bean="callerInfoInterceptor" />
        </cxf:inInterceptors>
        <cxf:properties>
            ......
            .....
        </cxf:properties>
    </cxf:bus>

</beans>

CallerInfoInterceptor.java (CXF Interceptor)

public class CallerInfoInterceptor extends AbstractPhaseInterceptor<Message>  {

    @Inject CallerInfoBean callerInfo; // bean

    public CallerInfoInterceptor() {
        super(Phase.RECEIVE);
    }

    @Override
    public void handleMessage(Message message){
     ...........

     if (callerInfo == null) {
            callerInfo =  
          javax.enterprise.inject.spi.CDI.current().select(CallerInfoBean.class).get();
        }
    }
ulab
  • 1,079
  • 3
  • 15
  • 45