简体   繁体   English

Spring Autowiring调用错误的构造函数

[英]Spring Autowiring calling wrong constructor

I'm learning Springs with annotations and auto wiring. 我正在学习带有注释和自动接线的Springs 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. 我有三个用于执行三种自动接线方式的类,Constructor和Setter可以正常工作,但是当method明智的自动接线完成时,就会调用错误的构造函数。 One thing I missed in my in my BaseBallCoach class was a default constructor, but anyhow compiler will automatically generate one for me right? 我在BaseBallCoach类中错过的一件事是默认构造函数,但是无论如何,编译器会为我自动生成一个吗?

This is my CoachMethodInjectionApp.java where I executed method auto wiring 这是我的CoachMethodInjectionApp.java ,在其中执行方法自动接线

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?? 输出的第一行是我不了解的Default constructor ,为什么CricketCoach的构造CricketCoach执行?

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 由于CricketCoach类使用@Component注释,并且在Spring容器启动时扫描包, CricketCoach通过调用no-arg构造函数来创建CricketCoach类型的实例。

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 所有在XML文件中定义或注释的豆@Component或任何扩展@Component从扫描包将在Spring容器的开始时间或创建不管他们会不会用

You're creating a new instance of Coach class here: Coach coach = context.getBean("baseBallCoach", Coach.class); 您将在此处创建Coach类的新实例: Coach coach = context.getBean("baseBallCoach", Coach.class); Everytime new instance is created it is executing a constructor. 每次创建新实例时,它都会执行一个构造函数。 In which you have call to System.out.println("Default constructor"); 在其中调用System.out.println("Default constructor"); . You may try removing @Component from Cricket Coach imlpementation. 您可以尝试从板球教练训练中删除@Component。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM