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

Spring Tutorial Index Page: Link

<bean id="" class="">
<property name="">
<props>
<prop key="fname">ABC</prop>
</props>
<property>
</bean>

Create Java Project

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

--

--

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