天天看點

eclipse上cxf與testNG的沖突

我打算在eclipse上的一個maven項目的某一個類釋出,成為web service,打算使用cxf進行釋出。

最初這個測試用的maven項目是這樣的:

eclipse上cxf與testNG的沖突

也就是說,在導入cxf之前,這個maven項目的功能是可以正常運作的,我的maven中有一個類名為YankDAO.java

我采用testNG進行測試,建立YankDAO的執行個體,執行它的方法,方法的内容是讀取eclipse同一台PC上的mysql中的資料,然後console中顯示這些資料。

其中testNG的測試代碼的内容如下:

package com.webservice.spring.DAO;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.testng.AbstractTransactionalTestNGSpringContextTests;
import org.testng.annotations.Test;

import com.webservice.spring.domain.Yank;

@ContextConfiguration("classpath*:/WebServiceApplicationContext.xml")
public class YankDAOTest extends AbstractTransactionalTestNGSpringContextTests{
    
    private YankDAO yd;

    @Autowired
    public void setYd(YankDAO yd) {
        this.yd = yd;
    }
    


    @Test
    public void f() {

        String st1 = yd.testF();
        
        List<Yank> lk1 = yd.getMenu();
        for (int i = 0; i < lk1.size(); i++) {

            Yank yk1 = lk1.get(i);
            System.out.println(yk1.getsCoffee());
            System.out.println("--------------");
            System.out.println(yk1.getsHouse());

        }

    }


}           

上述代碼,加載了一個spring的配置檔案,該配置檔案配置了一個可以通路本地mysql的資料源,并且向spring容器注冊了JDBCTemplate的bean。我們的YankDAO.java代碼中注入了這個bean并且利用它通路mysql資料庫。

其中spring配置檔案名為WebServiceApplicationContext.xml

這個檔案放在了maven項目的resources檔案夾下,預設情況,spring容器會通路這個檔案夾,讀取配置檔案。

配置檔案的代碼如下所示:

<?xml version="1.0" encoding="UTF-8"?>  
<!-- 內建hibernate之後的Spring的applicationContext配置檔案 -->
<!-- mysql dao和service層的Spring配置檔案 -->
<beans xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc" 
    xmlns:cache="http://www.springframework.org/schema/cache"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:cxf="http://cxf.apache.org/core"
    xmlns:jaxws="http://cxf.apache.org/jaxws"
    
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        classpath:/org/springframework/beans/factory/xml/spring-beans.xsd     
        http://www.springframework.org/schema/context
        classpath:/org/springframework/context/config/spring-context.xsd
        http://www.springframework.org/schema/aop
        classpath:/org/springframework/aop/config/spring-aop.xsd
        http://www.springframework.org/schema/tx
        classpath:/org/springframework/transaction/config/spring-tx.xsd
        http://www.springframework.org/schema/jdbc
        classpath:/org/springframework/jdbc/config/spring-jdbc.xsd
        http://www.springframework.org/schema/mvc  
        classpath:/org/springframework/web/servlet/config/spring-mvc.xsd"> 
        <!--  
                http://cxf.apache.org/core 
        classpath:/schemas/core.xsd
        http://cxf.apache.org/jaxws 
        classpath:/schemas/jaxws.xsd
        -->
        
    <context:component-scan base-package="com.webservice.spring.DAO"/>
    <!--com.mysql.cj.jdbc.Driver-->
    <!--配置檔案-->    
    <bean id="propertyConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
     <property name="locations">
         <list>
            <value>classpath:/WebServiceApplicationContext.properties</value>
         </list>
     </property>
    </bean>
    <tx:annotation-driven transaction-manager="transactionmanager"/>
    <!-- 配置mysql資料源, 阿裡巴巴的druid-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"
        destroy-method="close" 
        p:driverClassName="${jdbc.driver}"
        p:url="${jdbc.url}"
        p:username="${jdbc.username}"
        p:password="${jdbc.password}" 
        p:initialSize="${ds.initialSize}"
        p:minIdle="${ds.minIdle}"
        p:maxActive="${ds.maxActive}"
        p:maxWait="${ds.maxWait}"
        p:timeBetweenEvictionRunsMillis="${ds.timeBetweenEvictionRunsMillis}"
        p:minEvictableIdleTimeMillis="${ds.minEvictableIdleTimeMillis}"
        p:removeAbandoned="${ds.removeAbandoned}"
        p:removeAbandonedTimeout="${ds.removeAbandonedTimeout}"
        p:defaultAutoCommit="true">
    </bean>
    
    <!-- spring為內建hibernate提供的LocalSessionFactoryBean -->    
    <!-- 指定資料源 -->
    <bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
       <property name="dataSource" ref="dataSource"/>
       <property name="hibernateProperties">
          <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.format_sql">true</prop>
          </props>
       </property>
       <property name="packagesToScan" value="com.tsmi.hibernate.entity"/>
    </bean>
    
    <!-- 配置Hibernate的事務管理器  : 注入SessionFactory 會話工廠 -->
     <bean id="transactionmanager" class="org.springframework.orm.hibernate5.HibernateTransactionManager"> 
         <property name="sessionFactory" ref="sessionFactory"/>
    </bean> 
    <!--  -->
    <!-- 配置JdbcTemplate -->
    <bean id="jdbcTemplate" 
        class="org.springframework.jdbc.core.JdbcTemplate">
         <property name="dataSource" ref="dataSource"/>
    </bean>        
 </beans>           

上述功能是spring通過jdbctemplate通路mysql的簡單實作,也跑通了。

下面我們要為我們的項目導入cxf的jar環境

eclipse上cxf與testNG的沖突

導入,我們之前下載下傳并放置在c盤根目錄下的cxf,而且我們也配置了系統環境變量

eclipse上cxf與testNG的沖突
eclipse上cxf與testNG的沖突

但是導入之後就開始報錯

eclipse上cxf與testNG的沖突

表明,我們maven項目pom中導入的jar和單獨為項目build path導入的jar發生了沖突。

這個時候,如果我們為了不要報錯,單方面去pom.xml中注釋掉一些jar,确實可以讓makers中的報錯消失。

這時,原本可以運作的testNG的代碼,運作起來會報錯:

Caused by: java.lang.NoSuchMethodError: org.springframework.util.Assert.notNull(Ljava/lang/Object;Ljava/util/function/Supplier;)V
    at org.springframework.test.context.support.ContextLoaderUtils.resolveContextConfigurationAttributes(ContextLoaderUtils.java:240)
    at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildMergedContextConfiguration(AbstractTestContextBootstrapper.java:304)
    at org.springframework.test.context.support.AbstractTestContextBootstrapper.buildTestContext(AbstractTestContextBootstrapper.java:109)
    at org.springframework.test.context.TestContextManager.<init>(TestContextManager.java:135)
    at org.springframework.test.context.TestContextManager.<init>(TestContextManager.java:120)
    at org.springframework.test.context.testng.AbstractTestNGSpringContextTests.<init>(AbstractTestNGSpringContextTests.java:112)
    at org.springframework.test.context.testng.AbstractTransactionalTestNGSpringContextTests.<init>(AbstractTransactionalTestNGSpringContextTests.java:82)
    at com.webservice.spring.DAO.YankDAOTest.<init>(YankDAOTest.java:13)
    ... 26 more           
eclipse上cxf與testNG的沖突