Spring – Properties File Data DI Parameters | Code Factory


Donate : Link

Medium Blog : Link

Applications : Link

This image has an empty alt attribute; its file name is image-1.png

Spring Tutorial Index Page: Link

  • To read properties file into class directly then we use PropertyPlaceholderConfigurer.

Create Java Project

  • Open Eclipse
  • Go to File -> New -> Others… -> Java Project
  • Create DI-PropertiesFileData project
  • Right click on project -> Build Path -> Configure Build Path -> Libraries tab -> Add External JARs
    • commons-logging-X.X.jar
    • spring-beans-X.X.X.jar
    • spring-context-X.X.X.jar
    • spring-core-X.X.X.jar
    • spring-expression-X.X.X.jar
    • mysql-connector-java-X.X.X.jar
  • * You can find namespace information from spring-beans-X.X.X.jar -> META-INF -> spring.schemas

driver.properties

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test
username=root
password=root

spring.xml

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:util="http://www.springframework.org/schema/util"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
   http://www.springframework.org/schema/util 
   http://www.springframework.org/schema/util/spring-util-4.3.xsd">

	<!-- load properties data into IOC context scope -->
	<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="location" value="com/codeFactory/resources/Driver.properties" />
	</bean>
	
	<!-- inject context scope data into bean -->
	<bean id="cc" class="com.codeFactory.beans.ConnectionConfig">
		<property name="driver" value="${driver}" />
		<property name="url" value="${url}" />
		<property name="username" value="${username}" />
		<property name="password" value="${password}" />
	</bean>
</beans>

ConnectionConfig.java

package com.codeFactory.beans;

import java.sql.Connection;
import java.sql.DriverManager;

/**
 * @author code,factory
 *
 */
public class ConnectionConfig {

	private String driver, url, username, password;
	private Connection con;

	public void setDriver(String driver) {
		this.driver = driver;
	}

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

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

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

	public Connection getConnection() throws Exception {
		Class.forName(driver);
		con = DriverManager.getConnection(url, username, password);
		System.out.println("Connection created: " + con);
		return con;
	}
}

Client.java

package com.codeFactory.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.codeFactory.beans.ConnectionConfig;

/**
 * @author code.factory
 *
 */
public class Client {
	public static void main(String... args) throws Exception {
		ApplicationContext context = new ClassPathXmlApplicationContext("com/codeFactory/resources/spring.xml");
		
		ConnectionConfig t = (ConnectionConfig) context.getBean("cc");
		t.getConnection();
	}
}

Output:

Connection created: com.mysql.cj.jdbc.ConnectionImpl@55b699ef

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 )

Connecting to %s