Spring — java.util.Properties DI Parameters | Code Factory

Spring Tutorial Index Page: Link

  • For Property, inside dtd based configuration we can pass required key-value pairs.
<bean id="" class="">
<property name="">
<props>
<prop key="fname">ABC</prop>
</props>
<property>
</bean>
  • For Map we use <entry> tag but for Property we use <props> tag.

Create Java Project

  • Open Eclipse
  • Go to File -> New -> Others… -> Java Project
  • Create DI-Properties 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 dtd information from spring-beans-X.X.X.jar -> org -> springframework -> beans -> factory -> xml -> spring-beans.dtd (line no 36 & 37)
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">
<beans>
<bean id="t" class="com.codeFactory.beans.Test">
<property name="driver">
<props>
<prop key="driverClass">com.mysql.jdbc.Driver</prop>
<prop key="url">jdbc:mysql://localhost:3306/test</prop>
<prop key="user">username</prop>
<prop key="password">password</prop>
</props>
</property>
</bean>
</beans>
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() {
System.out.println("Driver Info: " + driver);
for(Entry<Object, Object> obj : driver.entrySet()) {
System.out.println(obj.getKey() + " : " + obj.getValue());
}
}
}
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();
}
}
Driver Info: {driverClass=com.mysql.jdbc.Driver, user=username, password=password, url=jdbc:mysql://localhost:3306/test}
driverClass : com.mysql.jdbc.Driver
user : username
password : password
url : jdbc:mysql://localhost:3306/test

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

The Functionality of the S-Wallet Mobile App

The Gladly Tech Stack

Playing Android with Firebase ML kit, Tensorflow Lite and ARCore 1

Getting started with AWS UI

5 Powerful Things That You Can Do INSIDE Chatfuel

The No-BS guide to Autocomplete and Fuzzy-search in Elasticsearch

How to find circumference of circle with Python?

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Code Factory

Code Factory

More from Medium

Getting started with Helidon 2

Retryable Topics with Spring Kafka

What test in a spring project and why

Visitor Design Pattern in Java