Autowire Spring Bean By Type Examples

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

1. How To Autowire Spring Beans By Type Example.

  1. If you really need to Autowire two or more same class type spring bean, you can set the autowire attribute’s value to “byName” to make different source bean property Autowired to same class type but different name target bean. You can read Autowire Spring Bean By Name Examples.
  2. This example contains two beans, one is CarBean the other is BrandBean.
  3. CarBean has two property carBrand and carBrand1, their type are all BrandBean.
  4. Please note, if you want to use the autowiring feature by bean class type, you should add autowire=”byType” in the source bean configuration.
  5. 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 id="carBean" class="com.dev2qa.example.spring.bean.autowire.bytype.CarBean" 
          <property name="carName" value="Mondeo"></property> 
          <property name="carPrice" value="180"></property> 
    // 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;
    // 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());
  8. Execution Output.
    Car name : Mondeo
    Car price : 180
    Brand name : Ford
    Brand country : USA
    Brand value : 10000
    Brand1 name : Ford
    Brand1 country : USA
    Brand1 value : 10000

Leave a Comment

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.