简体   繁体   English

Spring-如何从application.properties中的spring.datasource配置OracleDataSource

[英]Spring - How to configure OracleDataSource from spring.datasource in application.properties

In my application.properties i have some spring.datasource fields 在我的application.properties中,我有一些spring.datasource字段

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

These should be retrieved from a @Configuration annotated class 这些应该从@Configuration注释的类中检索

@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;
    }

    public Db() {
        OracleDataSource dataSource = null;
        try {
            dataSource = new OracleDataSource();
            dataSource.setUser(username);
            dataSource.setPassword(password);
            dataSource.setURL(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(2));
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}

But I'm unable to retrieve the username password and url from there, should I add another annotation somewhere or what am I doing 但是我无法从那里检索username passwordurl ,应该在其他地方添加其他注释或我在做什么

Like this I have the error: java.sql.SQLException: Invalid Oracle URL specified: OracleDataSource.makeURL 像这样我有错误: java.sql.SQLException:指定了无效的Oracle URL:OracleDataSource.makeURL

If I set the proper url with dataSource.setURL("jdbc:oracle:thin:@localhost:1521:XE"); 如果我使用dataSource.setURL("jdbc:oracle:thin:@localhost:1521:XE");设置正确的url dataSource.setURL("jdbc:oracle:thin:@localhost:1521:XE"); it can't read the username and password since they are null and I have java.sql.SQLException: ORA-01017: invalid username/password; 它无法读取usernamepassword因为它们为空,并且我具有java.sql.SQLException:ORA-01017:无效的用户名/密码; logon denied 登录被拒绝

You have two issues: 您有两个问题:

  1. You need to use another annotation in order for your fields to be populated. 您需要使用其他注释才能填充字段。 So annotate the class with @ConfigurationProperties("spring.datasource") 因此,使用@ConfigurationProperties("spring.datasource")注释该类。

  2. You cannot initialize your OracleDataSource directly in your constructor (ie Db() ) because the properties (your username/password/url fields) are not populated during the call of the constructor, so one thing that you can do is create another method and annotate that with @PostContruct in order for your dataSource to be correctly created. 您不能直接在构造函数(即Db() )中初始化OracleDataSource ,因为在构造函数的调用过程中未填充属性(用户名/密码/ URL字段),因此您可以做的一件事是创建另一个方法并进行注释与@PostContruct为了让您的dataSource ,以正确创建。

An example: 一个例子:

@PostConstruct
private void init() {
    // your data source initialization

}

One advice would be to change the way to initialize your dataSource and instead of trying to create it inside your constructor, you can rather create a new method which you can annotate with @Bean and make it return your dataSource and you can use it later using @Autowired . 一种建议是更改初始化dataSource的方式,而不是尝试在构造函数中创建它,而可以创建一个新方法,该方法可以用@Bean注释并使其返回数据源,以后可以使用它了。 @Autowired

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为mybatis定义了“jdbc.properties”,但仍需要在application.properties中添加spring.datasource? - defined “jdbc.properties” for mybatis, but still need add spring.datasource in application.properties? 如何配置Spring以从jar外部加载application.properties? - How to configure Spring to load application.properties from outside of jar? spring application.properties中的第二个数据源用于测试? - Second datasource in spring application.properties for tests? 如何找到 application.properties 的“spring.datasource.url=”? - How to find "spring.datasource.url=" for application.properties? 在Spring Boot中从application.properties配置国际化 - Configure internationalization from application.properties in spring boot 定义e如何在spring boot项目上的application.properties中选择第二个数据源 - How define e chose a second datasource from application.properties on spring boot project Spring Boot 2:如何使用application.properties文件配置HikariCP - Spring Boot 2: How to configure HikariCP using application.properties file 如何将 Spring Data JPA 正确配置到 Spring Boot 2.X 应用程序的 application.properties 配置文件中? - How to correctly configure Spring Data JPA into the application.properties configuration file of a Spring Boot 2.X application? 如何使用默认 spring.datasource 启动石英集群? - How to start a quartz cluster using default spring.datasource? Spring如何在运行时从application.properties重新加载值 - Spring how to reload the values from application.properties at runtime
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM