[英]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
password
和url
,应该在其他地方添加其他注释或我在做什么
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; 它无法读取username
和password
因为它们为空,并且我具有java.sql.SQLException:ORA-01017:无效的用户名/密码; logon denied 登录被拒绝
You have two issues: 您有两个问题:
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")
注释该类。
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.