I have Spring context in src/main/resources
<bean id="dataSource"
class="org.postgresql.ds.PGSimpleDataSource">
<property name="serverName" value="localhost"/>
<property name="databaseName" value="bookcompany"/>
<property name="user" value="thisadmin"/>
<property name="password" value="thisadmin"/>
</bean>
I want to update the "value" of each property value from Java swing GUI
screenshoot : Java Swing GUI for select database properties
This is my Java GUI code:
private void connectButtonActionPerformed(java.awt.event.ActionEvent evt) {
String serverNew = serverName.getText();
String databaseNew = databaseName.getText();
String dbUsernameNew = databaseName.getText();
String dbPasswordNew = databasePassword.getText();
//???
}
Is it possible to update property value of my spring-context using data from java GUI?? Please help..
You can change those values if you create the object with @Bean
and use @ComponentScan
, but I am not sure if you can change the database connection parameters dynamically.
One thing you can do is to create a needed bean or object yourself after you have the values from GUI by extending PersistenceUnitInfo
if you want a database connection, for example.
I have found a solution, I use the Properties file to store property values. Then my Spring context file uses that properties file.
First, I update Spring context file to this :
<context:property-placeholder location="file:./jdbc.properties" />
<bean id="dataSource"
class="org.postgresql.ds.PGSimpleDataSource">
<property name="serverName" value="${db.server}"/>
<property name="databaseName" value="${db.database}"/>
<property name="user" value="${db.username}"/>
<property name="password" value="${db.password}"/>
</bean>
I update my Java code :
String serverNew = serverName.getText();
String databaseNew = databaseName.getText();
String dbUsernameNew = databaseName.getText();
String dbPasswordNew = databasePassword.getText();
Properties props = new Properties();
File f = new File("./jdbc.properties");
if (f.exists()) {
props.load(new FileReader(f));
props.setProperty("db.host", serverNew);
props.setProperty("db.database", databaseNew);
props.setProperty("db.username", dbUsernameNew);
props.setProperty("db.password", dbPasswordNew);
f.createNewFile();
}
out = new FileOutputStream(f);
props.store(out, null);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.