Spring – Properties File 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

  • In case of simple dtd based configuration it is not possible to read external property data. If you want to read external property files data then you have to go through namespace. You have to use util namespace here. Xml schemas we have to use instead of dtd based configuration.
<beans xmlns="bean-namespace" xmlns:util="util-namespace">
	<bean id="" class="">
		<property name="">
			<util:property location="classpath:resources/Drivers.properties">
		<property>
	</bean>
</beans>

Create Java Project

  • Open Eclipse
  • Go to File -> New -> Others… -> Java Project
  • Create DI-PropertiesFile 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
  • * You can find namespace information from spring-beans-X.X.X.jar -> META-INF -> spring.schemas

driver.properties

driverClass=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test
password=password
user=username

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">

	<bean id="t" class="com.codeFactory.beans.Test">
		<property name="driver">
			<util:properties location="classpath:com/codeFactory/resources/driver.properties" />
		</property>
	</bean>
</beans>

Test.java

package com.codeFactory.beans;

import java.util.Map.Entry;
import java.util.Properties;

/**
 * @author code.factory
 *
 */
public class Test {
	private Properties driver;

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

	public void print() {
		for(Entry<Object, Object> obj : driver.entrySet()) {
			System.out.println(obj.getKey() + " : " + obj.getValue());
		}
	}
}

Client.java

package com.codeFactory.test;

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

import com.codeFactory.beans.Test;

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

Output:

driverClass : com.mysql.jdbc.Driver
user : username
password : password
url : jdbc:mysql://localhost:3306/test

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