无法使用Oracle数据库连接在Spring中加载配置类

时间:2017-12-26 17:45:15

标签: java spring oracle

我正在尝试连接到oracle数据库中的现有表。 我在application.properties

中设置了一个oracle连接
spring.jpa.database-platform=org.hibernate.dialect.Oracle10gDialect

spring.datasource.url=jdbc:oracle:thin:@localhost:1521:XE
spring.datasource.username=talon
spring.datasource.password=talon

spring.datasource.tomcat.test-while-idle=true
spring.datasource.dbcp2.validation-query=SELECT 1

spring.jpa.show-sql=true
spring.jpa.hibernate.naming.strategy=org.hibernate.cfg.ImprovedNamingStrategy

spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.Oracle10gDialect

我有一个配置类

@Configuration
public class Db {

    @NotNull
    private String username;
    @NotNull
    private String password;
    @NotNull
    private String url;

    public void setUsername(String username) {
        this.username = username;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    private Db() {
        OracleDataSource dataSource = null;
        try {
            dataSource = new OracleDataSource();
            dataSource.setUser(username);
            dataSource.setUser(password);
            dataSource.setUser(url);
            dataSource.setImplicitCachingEnabled(true);
            dataSource.setFastConnectionFailoverEnabled(true);
            Connection connection = dataSource.getConnection();
            Statement statement = connection.createStatement();
            ResultSet rs = statement.executeQuery("select * from BOOK");
            rs.next();
            System.out.println(rs.getString(1));
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}

当我运行它时出现错误 java.lang.IllegalStateException:无法加载由 java.lang.IllegalArgumentException引起的配置类:oraclejpamaven.oraclejpamaven.Db :类中没有可见的构造函数oraclejpamaven.oraclejpamaven.Db

1 个答案:

答案 0 :(得分:1)

Spring正在尝试使用默认构造函数实例化您的配置类Db

通过此private Db()

更改此public Db() {

希望这有帮助!