CodesJava

Easy learning with example program codes

spring bean autowire by constructor


Spring bean autowire by constructor is analogous to spring bean autowire by type but applies to constructor arguments.

Let’s discuss spring bean autowire by constructor with below example. In below example we have SortNumbers class which have one dependency for sorting implementation. We use spring bean autowire by constructor here as we take sortAlgo as constructor argument and in application context there is only one property exist of IsortAlgo type (BubbleSort).

Note: If there is more than one, a fatal exception is thrown or we have to provide the qualify information to help spring for identifying which one it should use.

We are using spring boot here.

Example

SpringAutowiringApplication.java

package com.codesjava.SpringAutowiring;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
 
@SpringBootApplication
public class SpringAutowiringApplication {	
        private static ApplicationContext appContext;
 
	public static void main(String[] args) {
		appContext = SpringApplication.run(SpringAutowiringApplication.class, args);
		sort(new int[]{ 31,22,13,43,15,6,37});
	}
 
	private static void sort(int[] data) {
		SortNumbers sortNumers = (SortNumbers) appContext.getBean("sortNumbers");
		sortNumers.sortNumbers(data);		
	}
 
}

SortNumbers.java

package com.codesjava.SpringAutowiring;
 
import org.springframework.stereotype.Component;
 
@Component
public class SortNumbers {
	ISortAlgo sortalgo;	
 
	public SortNumbers(ISortAlgo sortalgo) {
		super();
		this.sortalgo = sortalgo;
	}
 
	public void sortNumbers(int[] data){
		printNumbers(sortalgo.sort(data));
	}
 
	private static void printNumbers(int[] data) {          
        for (int i = 0; i < data.length; i++) {
            System.out.print(data[i]);
            if(i != data.length-1){
            	System.out.print(", ");
            }
        }
        System.out.println("\n");
    }    
}

ISortAlgo.java

package com.codesjava.SpringAutowiring;
 
public interface ISortAlgo {
	public int[] sort(int[] data);
}

BubbleSort.java

package com.codesjava.SpringAutowiring;
 
import org.springframework.stereotype.Component;
 
@Component
public class BubbleSort implements ISortAlgo {
	@Override
	public int[] sort(int[] array) {
		int n = array.length;
        int k;
        for (int m = n; m >= 0; m--) {
            for (int i = 0; i < n - 1; i++) {
                k = i + 1;
                if (array[i] > array[k]) {
                    swapNumbers(i, k, array);
                }
            }
        }
		return array;
	}	
 
	private void swapNumbers(int i, int j, int[] array) {  
        int temp;
        temp = array[i];
        array[i] = array[j];
        array[j] = temp;
    }
}

Output

6, 13, 15, 22, 31, 37, 43

Related topics

Spring Tutorial

Spring framework.
Spring framework architecture.
Spring ioc container.
Spring bean.
Spring bean scopes.
Spring bean life cycle.
Spring callback methods.
Spring hello world.
Spring bean definition inheritance.
Spring bean definition template.
Spring dependency injection.
Spring constructor based injection.
Constructor injection type ambiguities.
Setter based dependency injection.
Spring dependency injection collections.
Spring autowire
Spring autowire by name
Spring autowire by type
Spring autowire by constructor
Spring JDBC tutorial
Spring JDBC Prepared Statement
Spring ResultSetExtractor
Spring RowMapper
Spring aop tutorial.
Spring AOP AspectJ Xml.
Spring AOP AspectJ Annotation.
Spring MVC tutorial.
Spring mvc framework.
Spring mvc configuration file.
Spring mvc hello world.
Spring MVC multiple controller.
Spring MVC login.
Spring mvc form handling.
Spring mvc exception handling.
Spring spel tutorial.
Spring spel hello world.
Spring spel operators.
Spring spel ternary operator.
Spring spel standardevaluationcontext.
Spring spel bean reference.
Spring spel method invocation.
Spring spel list, map.
Spring spel regex.
Maven Eclipse Spring
Spring boot overview
Spring boot architecture diagram
Spring boot components
Spring boot starter parent
Spring boot web app configuration
Run spring boot application
Spring boot change port
Spring boot change context path
Spring boot log sql statements
Spring boot hello world
Spring boot JSP
Spring boot thymeleaf
Spring boot with mysql
Spring security overview
Spring security architecture
Spring security maven dependency
Spring security xml
Spring security annotation
Spring security custom login xml
Spring security custom login annotation
Spring security form login
Spring security remember me
Spring security method level


Industrial Training

We offers Placement Oriented Training on Java, Spring, JSF, Hibernate, PHP, AngularJS, Angular 4, PLSQL, Oracle BI Publisher etc. We also provides Online training, please mail us at hr@codesjava.com.

Development

We also provides the Development services for Website Development , Java Development, PHP Development, Android App Development etc. You can contact us on hr@codesjava.com.

Copyright © 2019 CodesJava DMCA.com Protection Status SiteMap Reference: Java Wiki