Public Static Factory Method To Create Spring Beans

Besides factory bean, you can also use a factory method to create spring bean, but the factory method must be public static and return the desired spring bean class type. This article will show you examples about how to do that.

This example will has two classes, one is DBConnectionDTO which store jdbc connection information for a database, the other is DBConnectionFactory which has a public static factory method that return an instance of DBConnectionDT with provided database connection parameters.

This is the class which will be returned by the factory-method.

public class DBConnectionDTO {
	private String connUrl;
	private String jdbcDriverClass;
	private String dbUserName;
	private String dbPassword;

	public String getConnUrl() {
		return connUrl;

	public void setConnUrl(String connUrl) {
		this.connUrl = connUrl;

	public String getJdbcDriverClass() {
		return jdbcDriverClass;

	public void setJdbcDriverClass(String jdbcDriverClass) {
		this.jdbcDriverClass = jdbcDriverClass;

	public String getDbUserName() {
		return dbUserName;

	public void setDbUserName(String dbUserName) {
		this.dbUserName = dbUserName;

	public String getDbPassword() {
		return dbPassword;

	public void setDbPassword(String dbPassword) {
		this.dbPassword = dbPassword;

	public String toString() {
		StringBuffer retBuf = new StringBuffer();
		retBuf.append("Jdbc driver class : " + this.getJdbcDriverClass());
		retBuf.append(" , Connection url : " + this.getConnUrl());
		retBuf.append(" , DB username : " + this.getDbUserName());
		retBuf.append(" , DB password : " + this.getDbPassword());
		return retBuf.toString();


This is the class which has a public static factory method that will return an object of with input parameters.

public class DBConnectionFactory {

	/* This is the factory method, it return DBConnectionDTO object with provided input parameters. */
	public static DBConnectionDTO createDBConnectionDto(String jdbcDriverClass, String connUrl, String dbUserName, String dbPassword)
		DBConnectionDTO ret = new DBConnectionDTO();
		if(!DBConnectionFactory.ifStringEmpty(jdbcDriverClass) && !DBConnectionFactory.ifStringEmpty(connUrl) && !DBConnectionFactory.ifStringEmpty(dbUserName))
			throw new IllegalArgumentException("Database connection parameters can not be empty. ");
		return ret;

	/* This is a util method to check whether the input string is empty or not.*/
	private static boolean ifStringEmpty(String str)
		if(str!=null && str.trim().length()>0)
			return false;
			return true;
	/* This is the main method which will load the spring application context 
	 * and get related spring bean by bean id. 
	 * Because the spring bean configuration xml has an attribute named factory-method, 
	 * then spring will execute the specified factory-method(createDBConnectionDto) in this class 
	 * and return an instance of DBConnectionDTO class by that method. */
	public static void main(String[] args) {
	   	// Initiate Spring application context, this line code will invoke bean initialize method.
		ApplicationContext springAppContext = new ClassPathXmlApplicationContext("BeanSettings.xml");

		// Get the mysql db connection object.
		DBConnectionDTO dbConnMysql = (DBConnectionDTO)springAppContext.getBean("mysqlDBConn");
		// Get the oracle db connection object.
		DBConnectionDTO dbConnOracle = (DBConnectionDTO)springAppContext.getBean("oracleDBConn");

3. BeanSettings.xml

This is the spring application context configuration xml which specify factory-method and it’s input parameters.

This example use DBConnectionFactory.createDBConnectionDto as the factory-method, and provide input parameters for each database connection bean.

<bean id="oracleDBConn" class="com.dev2qa.example.spring.bean.factorymethod.DBConnectionFactory" 
      <constructor-arg index="0" value="oracle.jdbc.driver.OracleDriver"/>    
      <constructor-arg index="1" value="jdbc:oracle:thin:@localhost:1521:DataTiger"/>    
      <constructor-arg index="2" value="system"/>    
      <constructor-arg index="3" value="manager"/> 

<bean id="mysqlDBConn" class="com.dev2qa.example.spring.bean.factorymethod.DBConnectionFactory" 
      <constructor-arg index="0" value="com.mysql.jdbc.Driver"/>    
      <constructor-arg index="1" value="jdbc:mysql://localhost:3306/test"/>    
      <constructor-arg index="2" value="root"/>    
      <constructor-arg index="3" value=""/> 

4. Example Output

spring bean factory method example output

5. Source Code Download

  1. [download id=”2791″]
0 0 vote
Article Rating
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x