Tomcat 8, axis2 webservices aar, spring jndi not b

2019-04-13 02:34发布

问题:

I have been working on an issue where JNDI name is not found in the context an axis2 webservice is running. This issue is only in Tomcat 8 when I use spring. Some details: (I will provide the elements that are relevant) 1. services.xml

<service name="ScoreService" class="com.bpl.ws.service.ScoreServiceInitializer">
<description>Simple test service</description>
<parameter name="ServiceObjectSupplier" locked="false">org.apache.axis2.extensions.spring.receivers.SpringAppContextAwareObjectSupplier</parameter>
<parameter name="SpringBeanName" locked="false">scoreService</parameter>

2. server.xml:

      <Host name="localhost"  appBase="webapps" xmlBase="C:\Applications\apache-tomcat-8.0.30-windows-x64\context"
        unpackWARs="true" autoDeploy="true">
  1. Context.xml

        <JarResources className="org.apache.catalina.webresources.DirResourceSet"
               base="C:\Applications\apache-tomcat-8.0.30-windows-x64\commonLib" webAppMount="/WEB-INF/lib"/>
    

4.JNDI resource in the context file:

<Resource name="jdbc/ADS" auth="Container"
  factory="com.bpl.ws.EncryptedJdbcDataSourceFactory"
  type="javax.sql.DataSource"
  driverClassName="oracle.jdbc.OracleDriver"
  url="jdbc:oracle:thin:@xxxxxxx.com:3203/xxxx"
  username="xxxxx"
  password="xxxx"
  initialSize="10"
  logAbandoned="false"
  maxActive="20"
  maxIdle="10"
  maxWait="10000"
  removeAbandoned="true"
  removeAbandonedTimeout="120"
  jdbcInterceptors="QueryTimeoutInterceptor(queryTimeout=10)"
  testOnBorrow="true"
  validationInterval="30000"
  validationQuery="Select 1 from dual"/>

As shown in services.xml file listing, I am using an initializer class and the code to load the spring context looks like this: 4.ScoreSerivceInitializer

public void startUp(ConfigurationContext ignore, AxisService service) {
    System.out.println("SCORESERVICE:: Starting up..");
    DataSource ds;


        ClassLoader cloader = service.getClassLoader();
        Thread.currentThread().setContextClassLoader(cloader);
        System.out.println("SCORESERVICE:: spring context starting up");
        spContext = new ClassPathXmlApplicationContext(new String[] {"DST-Context.xml"},false);
        spContext.setClassLoader(cloader);


        try {
            spContext.refresh();
  1. spring context.xml (DST-Context.xml)

The DST-Context.xml entry looks like this:

<bean id="applicationContext"
    class="org.apache.axis2.extensions.spring.receivers.ApplicationContextHolder" />

<bean id="datasource" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName">
        <value>java:comp/env/jdbc/ADS</value>
    </property>
</bean>
  1. Tomcat log:

    [WARN] Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'datasource' defined in class path resource [DST-Context.xml]: Invocation of init method failed; nested exception is javax.naming.NameNotFoundException: Name [java:comp/env/jdbc/ADS] is not bound in this Context. Unable to find [java:comp].org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'datasource' defined in class path resource [DST-Context.xml]: Invocation of init method failed; nested exception is javax.naming.NameNotFoundException: Name [java:comp/env/jdbc/ADS] is not bound in this Context. Unable to find [java:comp].

Without changing anything in the configuration, if I changed the ScoreServiceInitializer to do this:

initCtx = new InitialContext();

        envCtx = (Context) initCtx.lookup("java:comp/env"); 

        ds = (DataSource)
                  envCtx.lookup("jdbc/ADS");

Everything works. As you see here I don't use any spring and the jndi datasource is in context.

If I deploy the code with spring config in Tomcat 7, it works fine. The spring context seems to be the issue but have been looking at it for a while and cant seem to figure out why Tomcat 8 has a different behavior compared to Tomcat 7. I know that Tomcat8 has changed some behavior in terms of how the Resources are configured and the dbcp is now dbcp2 and I have updated config files accordingly. Any help is greatly appreciated. Please let me know if any other info is required.