为什么在依赖jar中有编译错误但仍然可以成功运行项目?

时间:2016-12-13 13:55:56

标签: maven

在intellij idea中查看org.apache.commons.dbcp.BasicDataSource的源代码时,我发现了这个错误

Class 'BasicDataSource' must either be declared abstract or implement abstract method 'getParentLogger()' in 'CommonDataSource'

enter image description here

我使用下面的maven依赖

    <dependency>
        <groupId>commons-dbcp</groupId>
        <artifactId>commons-dbcp</artifactId>
        <version>1.4</version>
    </dependency>

和这个配置

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
        <property name="url"
                  value="jdbc:mysql://localhost/test?useUnicode=true"/>
        <property name="username" value="root"/>
        <property name="password" value="123456"/>
    </bean>

但我的项目可以成功运行。然后我写了一个测试类来模拟这个错误

public class Foo {
    public static void main(String[] args) {
        System.out.println("hello world");
    }

    interface IA{
        String getParentLog();
    }
    class AImpl implements IA{
        public  void doSomething(){
            System.out.println("doSomething");
        }
    }
}

但这次我无法运行它并出现编译错误

Error:(14, 5) java: com.foobar.Foo.AImpl is not abstract and does not override abstract method getParentLog() in com.foobar.Foo.IA

为什么会这样?

1 个答案:

答案 0 :(得分:0)

两件事 -

  • 首先,与DataSources相关的部分与用法相矛盾。我怀疑你可能最终编辑了下载源代码的BasicDataSource类,或者使用了一些其他依赖项,这些依赖项带来了上面示例中实现的DataSource类的另一个实现。请确认您没有使用在类路径中带来冲突类的任何其他依赖项。
  • 请注意,在使用上述依赖项commons-dbcp:1.4时,将使用以下语法从包DataSource导入类javax.sql

    public interface DataSource extends javax.sql.CommonDataSource, 
                                                  java.sql.Wrapper { .. }
    
  • 第二部分,名为AImpl的类不是abstract,在这种情况下,它必须定义它所实现的interface的方法 -

    class AImpl implements IA{
        public  void doSomething(){
            System.out.println("doSomething");
        }
    
    
        @Override
        String getParentLog() {
            /** Define how getParentLog is implemented in this class */
           return null; // return string here after the above definition
        }
    }
    
相关问题