Spring - Injecting Properties into bean example

Posted on December 31, 2016


In this post we will learn how to inject the java.util.Properties into spring bean with xml and annotation based configuration.

Spring Bean + Properties + XML configuration example

Consider the following spring bean class.

DatabaseConfig.java

package com.boraji.tutorial.spring.xml;

import java.util.Properties;

/**
 * @author imssbora
 */
public class DatabaseConfig {
   private Properties properties;

   public void setProperties(Properties properties) {
      this.properties = properties;
   }

   public Properties getProperties() {
      return properties;
   }
}

Declare DatabaseConfing bean in XML configuration metadata.

beans.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	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.xsd">

	<bean id="configBean" class="com.boraji.tutorial.spring.xml.DatabaseConfig">
		<property name="properties">
			<props>
				<prop key="driverClassName">com.mysql.jdbc.Driver</prop>
				<prop key="url">jdbc:mysql://localhost:3306/mydb</prop>
				<prop key="username">root</prop>
				<prop key="password">abcdxyz</prop>
			</props>
		</property>
	</bean>

</beans>

Or using <util:properties/> to instantiate a java.util.Properties instance with values

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">

	<util:properties id="props">
		<prop key="driverClassName">com.mysql.jdbc.Driver</prop>
		<prop key="url">jdbc:mysql://localhost:3306/mydb</prop>
		<prop key="username">root</prop>
		<prop key="password">abcdxyz</prop>
	</util:properties>

	<bean id="configBean" class="com.boraji.tutorial.spring.xml.DatabaseConfig">
		<property name="properties" ref="props" />
	</bean>

</beans>

Create main class and run application.

MainApp.java

package com.boraji.tutorial.spring.xml;

import java.util.Properties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @author imssbora
 */
public class MainApp {
   public static void main(String[] args) {
      @SuppressWarnings("resource")
      ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
      DatabaseConfig config = context.getBean("configBean", DatabaseConfig.class);

      Properties properties = config.getProperties();
      System.out.println("driverClassName : " + properties.getProperty("driverClassName"));
      System.out.println("url : " + properties.getProperty("url"));
      System.out.println("username : " + properties.getProperty("username"));
      System.out.println("password : " + properties.getProperty("password"));
   }
}

Output

driverClassName : com.mysql.jdbc.Driver
url : jdbc:mysql://localhost:3306/mydb
username : root
password : abcdxyz

 

Spring Bean + Properties + Annotation configuration example

Create a configuration class and define the properties bean in it as shown below.

AppConfig.java

package com.boraji.tutorial.spring.annotation;

import java.util.Properties;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

/**
 * @author imssbora
 */
@Configuration
@ComponentScan(basePackages="com.boraji.tutorial.spring.annotation")
public class AppConfig {
  
   @Bean
   @Qualifier("databaseConfigProps")
   public Properties getDatabaseConfig(){
      Properties props=new Properties();
      props.setProperty("driverClassName", "com.mysql.jdbc.Driver");
      props.setProperty("url", "jdbc:mysql://localhost:3306/mydb");
      props.setProperty("username", "root");
      props.setProperty("password", "abcdxyz");
      return props;
   }
}

Create a spring bean class and inject properties into it using @Autowired and @Qualifier annotations.

DatabaseConfig.java

package com.boraji.tutorial.spring.annotation;

import java.util.Properties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

/**
 * @author imssbora
 */
@Component
public class DatabaseConfig {
   private Properties properties;

   @Autowired
   @Qualifier("databaseConfigProps")
   public void setProperties(Properties properties) {
      this.properties = properties;
   }

   public Properties getProperties() {
      return properties;
   }
}

Create main class and run application.

MainApp.java

package com.boraji.tutorial.spring.annotation;

import java.util.Properties;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * @author imssbora
 */
public class MainApp {
   public static void main(String[] args) {
      AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
      DatabaseConfig config = context.getBean(DatabaseConfig.class);
      Properties properties = config.getProperties();
      System.out.println("driverClassName : " + properties.getProperty("driverClassName"));
      System.out.println("url : " + properties.getProperty("url"));
      System.out.println("username : " + properties.getProperty("username"));
      System.out.println("password : " + properties.getProperty("password"));
      
      context.close();
   }
}

Output

driverClassName : com.mysql.jdbc.Driver
url : jdbc:mysql://localhost:3306/mydb
username : root
password : abcdxyz

 

Download Sources