Set datasource spring bean properties dynamically if server is not available

Hi Readers today i want to share with you guys something that i faced recently.

Problem :

Recently i was developing a spring application in which container need to initialized by public static void main method rather then any server .
We want user to enter details with swing ui and do not want to save them in properties file at run time. Risk in this is, that anybody can look into properties file which is a security threat.
So now the question is that how we will tell spring container to which DataBase it needs to connect with.

Solution :

So to solve this problem, I created a class which extends BasicDatasource as code given below.


public class CustomDatasource extends BasicDatasource {

private static String hostName;
private static String dbName;
private static String port;
private static String userName;
private static String dbPassword;


private CustomDatasource(){
setUrl("jdbc:oracle:thin:@"+hostName+":"+port+":"+dbName);
setDriverClassName("oracle.jdbc.OracleDriver");
setUsername(userName);
setPassword(dbPassword);
}


public static String getHostName() {
return hostName;
}

public static void setHostName(String hostName) {
CustomDatasource.hostName = hostName;
}

public static String getDbName() {
return dbName;
}

public static void setDbName(String dbName) {
CustomDatasource.dbName = dbName;
}

public static String getPort() {
return port;
}

public static void setPort(String port) {
CustomDatasource.port = port;
}

public static String getUserName() {
return userName;
}

public static void setUserName(String userName) {
CustomDatasource.userName = userName;
}

public static String getDbPassword() {
return dbPassword;
}

public static void setDbPassword(String dbPassword) {
CustomDatasource.dbPassword = dbPassword;
}

Now make this class as datasource bean in Beans.xml and inject this bean into persistanceUnitManager .
Now initialize spring container as given below.


public class Test {
public static void main(String args[]){
Scanner in = new Scanner(System.in);
System.out.println("Enter a string");
String dbName = in.nextLine();
CustomDatasource.setDbName(dbName);
String hostName = in.nextLine();
CustomDatasource.setHostName(hostName);
String port = in.nextLine();
CustomDatasource.setPort(port);
String userName = in.nextLine();
CustomDatasource.setUserName(userName);
String dbPassword = in.nextLine();
CustomDatasource.setDbPassword(dbPassword);
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("/Beans.xml");
}
}

Now without saving database configuration in any properties file your application will be able to connect with database.

Other related posts are :
HTML to PDF conversion in JAVA | HTML to PDF example in JAVA
Top 10 spring interview questions and their answers. Spring basic concepts.
MQ connection not closed and giving MQ Error-2009
JPA Hibernate Sequence generator generating odd ID value and IntegrityConstraintVolation exception is coming

Please share this posts on facebook,Linkeedin,Google+,Twitter by clicking on below icons.

Advertisements

8 thoughts on “Set datasource spring bean properties dynamically if server is not available

  1. Pingback: Logback logs are not working. Log4j logs are not working. Steps to make logging work. | Gaurav 's Blog

  2. Pingback: JPA Hibernate Sequence generator generating odd ID value and IntegrityConstraintVolation exception is coming | Blogger's Views

  3. Pingback: String vs StringBuffer vs StringBuilder | Techno Tailor

  4. Pingback: String interview questions and answers | Techno Tailor

  5. Pingback: MQ connection not closed and giving MQ Error-2009 | Techno Tailor

  6. Pingback: Top 10 spring interview questions and their answers. Spring basic concepts. | Techno Tailor

  7. Pingback: How to render HTML in java | Techno Tailor

  8. Pingback: Are Payment banks a competitive threat or addon to the traditional banking. | Techno Tailor

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s