Autowire Spring Bean By Type Examples

If you set a spring bean’s autowire attribute’s value to “byType” in bean configuration xml file, then it’s property will be autowired to a same type spring bean in current spring application context. If can not find any matching bean, then nothing happened, if there are two or more matching bean, then spring framework will throw an exception.

If you really need to autowire two or more same class type spring bean, you can set autowire to “byName” to make different source bean property autowire to same class type but different name target bean. You can read Autowire Spring Bean By Name Examples.

This example contains two bean, one is CarBean the other is BrandBean. CarBean has two property carBrand and carBrand1, their type are all BrandBean. Please Note, if you want to use autowiring by bean class type, you should add autowire=”byType” in source bean configuration.

BeanSettings.xml

 <bean id="brandBean" class="com.dev2qa.example.spring.bean.autowire.bytype.BrandBean"> 
      <property name="brandName" value="Ford"></property> 
      <property name="brandCountry" value="USA"></property> 
      <property name="brandValue" value="10000"></property> 
</bean> 

<bean id="carBean" class="com.dev2qa.example.spring.bean.autowire.bytype.CarBean" 
       autowire="byType"> 
      <property name="carName" value="Mondeo"></property> 
      <property name="carPrice" value="180"></property> 
</bean>

BrandBean.java

// Represent a car brand.
public class BrandBean {

	private String brandName;
	
	private String brandCountry;
	
	private int brandValue;

	public String getBrandName() {
		return brandName;
	}

	public void setBrandName(String brandName) {
		this.brandName = brandName;
	}

	public String getBrandCountry() {
		return brandCountry;
	}

	public void setBrandCountry(String brandCountry) {
		this.brandCountry = brandCountry;
	}

	public int getBrandValue() {
		return brandValue;
	}

	public void setBrandValue(int brandValue) {
		this.brandValue = brandValue;
	}
}

CarBean.java

// Represent a car.
public class CarBean {
	
	private String carName;
	
	private BrandBean carBrand;
	
	private BrandBean carBrand1;
	
	private int carPrice;

	public String getCarName() {
		return carName;
	}

	public void setCarName(String carName) {
		this.carName = carName;
	}

	public BrandBean getCarBrand() {
		return carBrand;
	}

	public void setCarBrand(BrandBean carBrand) {
		this.carBrand = carBrand;
	}

	public int getCarPrice() {
		return carPrice;
	}

	public void setCarPrice(int carPrice) {
		this.carPrice = carPrice;
	}

	public BrandBean getCarBrand1() {
		return carBrand1;
	}

	public void setCarBrand1(BrandBean carBrand1) {
		this.carBrand1 = carBrand1;
	}

	public static void main(String[] args) {
	   	// Initiate Spring application context.
		ApplicationContext springAppContext = new ClassPathXmlApplicationContext("BeanSettings.xml");

		// Get CarBean by id. 
		CarBean carBean = (CarBean) springAppContext.getBean("carBean");
		
                System.out.println("Car name : " +carBean.getCarName());
        
                System.out.println("Car price : " +carBean.getCarPrice());
        
                System.out.println("Brand name : " +carBean.getCarBrand().getBrandName());
        
                System.out.println("Brand country : " +carBean.getCarBrand().getBrandCountry());
        
                System.out.println("Brand value : " +carBean.getCarBrand().getBrandValue());
        
                System.out.println("Brand1 name : " +carBean.getCarBrand1().getBrandName());
        
                System.out.println("Brand1 country : " +carBean.getCarBrand1().getBrandCountry());
        
                System.out.println("Brand1 value : " +carBean.getCarBrand1().getBrandValue());
	}
}

Execution Output

autowire spring bean by type example output

Source Code Download

  1. Download “Autowire-Spring-Bean-By-Type-Examples.zip” Autowire-Spring-Bean-By-Type-Examples.zip – Downloaded 53 times – 1 KB

(Visited 63 times, 1 visits today)
READ :   How To Inject Value To Spring Bean Properties

Leave a Reply

Your email address will not be published. Required fields are marked *

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