3

Having an issue with how CXF interceptor is setup and used by Spring. I want to log the incoming SOAP requests to database for audit log. I have the setup as below, but whenever incoming SOAP request comes, I get NPE where the service layer class is being accessed. It looks from log that the web application context is being re-loaded again leading to null reference for service bean. I had a look at the two entries - this and this - which are close, and tried out the solution in first link, but not working. Any help is appreciated.

Thanks

Interceptor code:

public class AuditLogInterceptor extends AbstractLoggingInterceptor {

private AuditLogService auditLogService;

@Autowired
public void setAuditLogService(AuditLogService auditLogService) {
    this.auditLogService = auditLogService;
}
private void saveAuditLogEntry() {
    // some more code ...
    auditLogService.logRequest(logEntry);
}

cxf-servlet.xml

<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<bean id="jsonProvider" class="org.codehaus.jackson.jaxrs.JacksonJsonProvider" />

<!-- Add new endpoints for additional services you'd like to expose -->
<bean id="abstractLogInterceptor" abstract="true">
    <property name="prettyLogging" value="true" />
</bean>
<bean class="com.xyz.interceptor.AuditLogInterceptor" id="logInInterceptor" parent="abstractLogInterceptor"/>

<jaxws:endpoint id="dataService" implementor="#masterDataService" address="/MasterDataService">
    <jaxws:inInterceptors>
        <ref bean="logInInterceptor" />
    </jaxws:inInterceptors>
</jaxws:endpoint>

web.xml

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath:/applicationContext-resources.xml
            classpath:/applicationContext-dao.xml
            classpath:/applicationContext-service.xml
            classpath*:/applicationContext.xml
            /WEB-INF/applicationContext*.xml
            /WEB-INF/cxf-servlet.xml
            /WEB-INF/security.xml
        </param-value>
</context-param>
<listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <listener>
        <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
    </listener>
    <listener>
        <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
    </listener>
<servlet>
    <servlet-name>CXFServlet</servlet-name>
    <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>CXFServlet</servlet-name>
    <url-pattern>/services/*</url-pattern>
</servlet-mapping>
Community
  • 1
  • 1
Pragmatic
  • 495
  • 1
  • 7
  • 11
  • You have a bean config file called `cxf-servlet.xml` that imports a file called `cxf-servlet.xml`? Probably not the cause of your problems, but definitely confusing – artbristol Nov 27 '13 at 11:34
  • Can you add extracts from your web.xml, and any other spring config files you have? Is `@Autowired` working on other beans? – artbristol Nov 27 '13 at 11:40
  • above cxf-servlet.xml is present in my web application and the cxf-servlet.xml that it includes is CXF file. Adding web.xml to question. And Yes, the @Autowired works fine for other components such as Spring MVC controllers – Pragmatic Nov 27 '13 at 14:15
  • I expect you're getting the contents of `/WEB-INF/cxf-servlet.xml` included in both the CXFServlet's context and the `ContextLoaderListener`'s. Try removing the line `/WEB-INF/cxf-servlet.xml` from the ContextLoaderListener's `contextConfigLocation` attribute. – artbristol Nov 27 '13 at 16:17
  • Thanks for the guidance. These comments helped resolve issue. Here is what I did. 1. remove cxf-servlet entry from web.xml 2. deleted the file itself since the mere removal of entry did not help 3. moved the cxf related bean setup to applicationContext.xml. Now the beans are recognized and @Autowired works. Can you please post it as answer so I will accept it. Thanks – Pragmatic Nov 28 '13 at 08:52

1 Answers1

0

I expect you're getting the contents of /WEB-INF/cxf-servlet.xml included in both the CXFServlet's context and the ContextLoaderListener's. Try removing the line /WEB-INF/cxf-servlet.xml from the ContextLoaderListener's contextConfigLocation attribute. You should also rename cxf-servlet.xml because the CXFServlet looks for a file with that exact name (see http://cxf.apache.org/docs/configuration.html) - or merge it into the rest of your applicationContext.xml.

artbristol
  • 32,010
  • 5
  • 70
  • 103