Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Autowiring calling wrong constructor

I'm learning Springs with annotations and auto wiring. I tried three types of auto wiring

  1. Constructor
  2. Setter injection
  3. Method

This is my configuration

<?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:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/context
                           http://www.springframework.org/schema/context/spring-context.xsd">

    <!-- add entry for component scanning -->
    <context:component-scan base-package="com.aht.spring.entity"></context:component-scan>

</beans>

These are my entities

Coach.java

package com.aht.spring.entity.coach;

public interface Coach {

    String getDailyWorkOut();

    String getDailyFortune();
}

FortuneService

package com.aht.spring.entity.fortune;

public interface FortuneService {

    String getFortune();
}

HappyFortuneService

package com.aht.spring.entity.fortune;

import org.springframework.stereotype.Component;

@Component
public class HappyFortuneService implements FortuneService {

    public String getFortune() {
        return "Feel energetic for first half of trainning";
    }
}

FootBallCoach

package com.aht.spring.entity.coach;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.aht.spring.entity.fortune.FortuneService;

@Component
public class FootBallCoach implements Coach {

    private FortuneService fortuneService;

    @Autowired
    public FootBallCoach(FortuneService fortuneService) {
        this.fortuneService = fortuneService;
    }

    public String getDailyWorkOut() {
        return "Practice one-on-one for 2 hours";
    }

    public String getDailyFortune() {
        return fortuneService.getFortune();
    }
}

CricketCoach

package com.aht.spring.entity.coach;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.aht.spring.entity.fortune.FortuneService;

@Component
public class CricketCoach implements Coach {

    private FortuneService fortuneService;

    public CricketCoach() {
        System.out.println("Default constructor");
    }

    @Autowired
    public void setFortuneService(FortuneService fortuneService) {
        this.fortuneService = fortuneService;
    }

    public String getDailyWorkOut() {
        return "Practice out field tips";
    }

    public String getDailyFortune() {
        return fortuneService.getFortune();
    }
}

BaseBallCoach

package com.aht.spring.entity.coach;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.aht.spring.entity.fortune.FortuneService;

@Component
public class BaseBallCoach implements Coach {

    private FortuneService fortuneService;

    public String getDailyWorkOut() {
        return "Practice curve whole day";
    }

    public String getDailyFortune() {
        return fortuneService.getFortune();
    }

    @Autowired
    public void customAutoWire(FortuneService fortuneService) {
        this.fortuneService = fortuneService;
    }
}

I've three classes for executing three ways of auto wiring, Constructor and Setter worked fine, but when method wise auto wiring was done a wrong constructor was called. One thing I missed in my in my BaseBallCoach class was a default constructor, but anyhow compiler will automatically generate one for me right?

This is my CoachMethodInjectionApp.java where I executed method auto wiring

package com.aht.spring.app;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.aht.spring.entity.coach.Coach;

public class CoachMethodInjectionApp {

    public static void main(String[] args) {

        //  read config-file
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

        //  get beans
        Coach coach = context.getBean("baseBallCoach", Coach.class);

        //  get dependencies
        System.out.println(coach.getDailyFortune());

        //  get daily workout
        System.out.println(coach.getDailyWorkOut());

        //  close context
        context.close();
    }
}

This was the output

Default constructor
Feel energetic for first half of trainning
Practice curve whole day

First line of output is what I don't understand Default constructor, why the constructor of CricketCoach executing??

like image 646
Arun Sudhakaran Avatar asked Jun 04 '26 08:06

Arun Sudhakaran


1 Answers

As CricketCoach class is annotated with @Component and the package is scanned when the Spring container starts it will create an instance of type CricketCoach by calling the no-arg constructor

All the beans defined in the xml file or annotated with @Component or any extension of @Component from scanned packages will be created at start time of the spring container no matter if they will be used or not


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!