Spring@Autowire和@Qualifier注释奇怪的行为
所以我在练习使用Spring注释@Autowire和@Qualifier进行依赖注入的特定代码。该代码在输出中有一点异常的情况下工作正常 Coach.javaSpring@Autowire和@Qualifier注释奇怪的行为,spring,annotations,Spring,Annotations,所以我在练习使用Spring注释@Autowire和@Qualifier进行依赖注入的特定代码。该代码在输出中有一点异常的情况下工作正常 Coach.java package com.luv2code.springdemo; public interface Coach { public String getDailyWorkout(); public String getDailyFortune(); package com.luv2code.springdemo; import or
package com.luv2code.springdemo;
public interface Coach {
public String getDailyWorkout();
public String getDailyFortune();
package com.luv2code.springdemo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class SwimCoach implements Coach {
@Autowired
@Qualifier("randomFortuneService")
private FortuneService fortuneService;
@Value("${foo.email}")
private String email;
@Value("${foo.team}")
private String team;
public SwimCoach() {
System.out.println("Inside Swimcoach no-arg constructor");
}
@Override
public String getDailyWorkout() {
return "Swim 1000 meters as a warm up.";
}
@Override
public String getDailyFortune() {
return fortuneService.getFortune();
}
public String getEmail() {
return email;
}
public String getTeam() {
return team;
}
package com.luv2code.springdemo;
public interface FortuneService {
public String getFortune();
}
package com.luv2code.springdemo;
import java.util.Random;
import org.springframework.stereotype.Component;
//with @Component, the class is ready to be scanned by Spring.
@Component
public class RandomFortuneService implements FortuneService {
// create an array of strings
private String[] data = {
"perseverance is the key to success.",
"Diligence is the mother of good luck.",
"Your Journey towards the success is itself a reward."
};
private Random myRandom = new Random();
@Override
public String getFortune() {
int index = myRandom.nextInt(data.length);
String theFortune = data[index];
return theFortune;
}
package com.luv2code.springdemo;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class PracticeHelloSpringApp {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
SwimCoach coach = context.getBean("swimCoach",SwimCoach.class);
//In the above code,SwimCoach defines two extra methods getEmail and getTeam which are not a part of Coach interface.
//So we need to create coach variable of type SwimCoach class with which we can access interface methods as well as class methods.
System.out.println(coach.getDailyWorkout());
System.out.println(coach.getDailyFortune());
System.out.println(coach.getEmail());
System.out.println(coach.getTeam());
context.close();
}
package com.luv2code.springdemo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
//Incase of default bean ID, It would be "pingPongCoach".
@Component("myPingPongCoach")
public class PingPongCoach implements Coach {
private FortuneService fortuneService;
//This is the case when we have multiple implementations of FortuneService and we are constructor injection for
//injecting dependency with the use of @Autowired and @Qualifier. Check the @Qualifier annotation inside of the constructor arguments.
@Autowired
public PingPongCoach(@Qualifier("randomFortuneService") FortuneService fortuneService) {
super();
System.out.println("inside constructor of PingPongCoach");
this.fortuneService = fortuneService;
}
@Override
public String getDailyWorkout() {
return "Ping Pong is no sport...You don't need any practice!!";
}
@Override
public String getDailyFortune() {
return fortuneService.getFortune();
}
}
}
SwimCoach.java
package com.luv2code.springdemo;
public interface Coach {
public String getDailyWorkout();
public String getDailyFortune();
package com.luv2code.springdemo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class SwimCoach implements Coach {
@Autowired
@Qualifier("randomFortuneService")
private FortuneService fortuneService;
@Value("${foo.email}")
private String email;
@Value("${foo.team}")
private String team;
public SwimCoach() {
System.out.println("Inside Swimcoach no-arg constructor");
}
@Override
public String getDailyWorkout() {
return "Swim 1000 meters as a warm up.";
}
@Override
public String getDailyFortune() {
return fortuneService.getFortune();
}
public String getEmail() {
return email;
}
public String getTeam() {
return team;
}
package com.luv2code.springdemo;
public interface FortuneService {
public String getFortune();
}
package com.luv2code.springdemo;
import java.util.Random;
import org.springframework.stereotype.Component;
//with @Component, the class is ready to be scanned by Spring.
@Component
public class RandomFortuneService implements FortuneService {
// create an array of strings
private String[] data = {
"perseverance is the key to success.",
"Diligence is the mother of good luck.",
"Your Journey towards the success is itself a reward."
};
private Random myRandom = new Random();
@Override
public String getFortune() {
int index = myRandom.nextInt(data.length);
String theFortune = data[index];
return theFortune;
}
package com.luv2code.springdemo;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class PracticeHelloSpringApp {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
SwimCoach coach = context.getBean("swimCoach",SwimCoach.class);
//In the above code,SwimCoach defines two extra methods getEmail and getTeam which are not a part of Coach interface.
//So we need to create coach variable of type SwimCoach class with which we can access interface methods as well as class methods.
System.out.println(coach.getDailyWorkout());
System.out.println(coach.getDailyFortune());
System.out.println(coach.getEmail());
System.out.println(coach.getTeam());
context.close();
}
package com.luv2code.springdemo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
//Incase of default bean ID, It would be "pingPongCoach".
@Component("myPingPongCoach")
public class PingPongCoach implements Coach {
private FortuneService fortuneService;
//This is the case when we have multiple implementations of FortuneService and we are constructor injection for
//injecting dependency with the use of @Autowired and @Qualifier. Check the @Qualifier annotation inside of the constructor arguments.
@Autowired
public PingPongCoach(@Qualifier("randomFortuneService") FortuneService fortuneService) {
super();
System.out.println("inside constructor of PingPongCoach");
this.fortuneService = fortuneService;
}
@Override
public String getDailyWorkout() {
return "Ping Pong is no sport...You don't need any practice!!";
}
@Override
public String getDailyFortune() {
return fortuneService.getFortune();
}
}
}
FortuneService.java
package com.luv2code.springdemo;
public interface Coach {
public String getDailyWorkout();
public String getDailyFortune();
package com.luv2code.springdemo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class SwimCoach implements Coach {
@Autowired
@Qualifier("randomFortuneService")
private FortuneService fortuneService;
@Value("${foo.email}")
private String email;
@Value("${foo.team}")
private String team;
public SwimCoach() {
System.out.println("Inside Swimcoach no-arg constructor");
}
@Override
public String getDailyWorkout() {
return "Swim 1000 meters as a warm up.";
}
@Override
public String getDailyFortune() {
return fortuneService.getFortune();
}
public String getEmail() {
return email;
}
public String getTeam() {
return team;
}
package com.luv2code.springdemo;
public interface FortuneService {
public String getFortune();
}
package com.luv2code.springdemo;
import java.util.Random;
import org.springframework.stereotype.Component;
//with @Component, the class is ready to be scanned by Spring.
@Component
public class RandomFortuneService implements FortuneService {
// create an array of strings
private String[] data = {
"perseverance is the key to success.",
"Diligence is the mother of good luck.",
"Your Journey towards the success is itself a reward."
};
private Random myRandom = new Random();
@Override
public String getFortune() {
int index = myRandom.nextInt(data.length);
String theFortune = data[index];
return theFortune;
}
package com.luv2code.springdemo;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class PracticeHelloSpringApp {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
SwimCoach coach = context.getBean("swimCoach",SwimCoach.class);
//In the above code,SwimCoach defines two extra methods getEmail and getTeam which are not a part of Coach interface.
//So we need to create coach variable of type SwimCoach class with which we can access interface methods as well as class methods.
System.out.println(coach.getDailyWorkout());
System.out.println(coach.getDailyFortune());
System.out.println(coach.getEmail());
System.out.println(coach.getTeam());
context.close();
}
package com.luv2code.springdemo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
//Incase of default bean ID, It would be "pingPongCoach".
@Component("myPingPongCoach")
public class PingPongCoach implements Coach {
private FortuneService fortuneService;
//This is the case when we have multiple implementations of FortuneService and we are constructor injection for
//injecting dependency with the use of @Autowired and @Qualifier. Check the @Qualifier annotation inside of the constructor arguments.
@Autowired
public PingPongCoach(@Qualifier("randomFortuneService") FortuneService fortuneService) {
super();
System.out.println("inside constructor of PingPongCoach");
this.fortuneService = fortuneService;
}
@Override
public String getDailyWorkout() {
return "Ping Pong is no sport...You don't need any practice!!";
}
@Override
public String getDailyFortune() {
return fortuneService.getFortune();
}
}
RandomFortuneService.java
package com.luv2code.springdemo;
public interface Coach {
public String getDailyWorkout();
public String getDailyFortune();
package com.luv2code.springdemo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class SwimCoach implements Coach {
@Autowired
@Qualifier("randomFortuneService")
private FortuneService fortuneService;
@Value("${foo.email}")
private String email;
@Value("${foo.team}")
private String team;
public SwimCoach() {
System.out.println("Inside Swimcoach no-arg constructor");
}
@Override
public String getDailyWorkout() {
return "Swim 1000 meters as a warm up.";
}
@Override
public String getDailyFortune() {
return fortuneService.getFortune();
}
public String getEmail() {
return email;
}
public String getTeam() {
return team;
}
package com.luv2code.springdemo;
public interface FortuneService {
public String getFortune();
}
package com.luv2code.springdemo;
import java.util.Random;
import org.springframework.stereotype.Component;
//with @Component, the class is ready to be scanned by Spring.
@Component
public class RandomFortuneService implements FortuneService {
// create an array of strings
private String[] data = {
"perseverance is the key to success.",
"Diligence is the mother of good luck.",
"Your Journey towards the success is itself a reward."
};
private Random myRandom = new Random();
@Override
public String getFortune() {
int index = myRandom.nextInt(data.length);
String theFortune = data[index];
return theFortune;
}
package com.luv2code.springdemo;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class PracticeHelloSpringApp {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
SwimCoach coach = context.getBean("swimCoach",SwimCoach.class);
//In the above code,SwimCoach defines two extra methods getEmail and getTeam which are not a part of Coach interface.
//So we need to create coach variable of type SwimCoach class with which we can access interface methods as well as class methods.
System.out.println(coach.getDailyWorkout());
System.out.println(coach.getDailyFortune());
System.out.println(coach.getEmail());
System.out.println(coach.getTeam());
context.close();
}
package com.luv2code.springdemo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
//Incase of default bean ID, It would be "pingPongCoach".
@Component("myPingPongCoach")
public class PingPongCoach implements Coach {
private FortuneService fortuneService;
//This is the case when we have multiple implementations of FortuneService and we are constructor injection for
//injecting dependency with the use of @Autowired and @Qualifier. Check the @Qualifier annotation inside of the constructor arguments.
@Autowired
public PingPongCoach(@Qualifier("randomFortuneService") FortuneService fortuneService) {
super();
System.out.println("inside constructor of PingPongCoach");
this.fortuneService = fortuneService;
}
@Override
public String getDailyWorkout() {
return "Ping Pong is no sport...You don't need any practice!!";
}
@Override
public String getDailyFortune() {
return fortuneService.getFortune();
}
}
}
PracticeHelloSpringApp.java
package com.luv2code.springdemo;
public interface Coach {
public String getDailyWorkout();
public String getDailyFortune();
package com.luv2code.springdemo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class SwimCoach implements Coach {
@Autowired
@Qualifier("randomFortuneService")
private FortuneService fortuneService;
@Value("${foo.email}")
private String email;
@Value("${foo.team}")
private String team;
public SwimCoach() {
System.out.println("Inside Swimcoach no-arg constructor");
}
@Override
public String getDailyWorkout() {
return "Swim 1000 meters as a warm up.";
}
@Override
public String getDailyFortune() {
return fortuneService.getFortune();
}
public String getEmail() {
return email;
}
public String getTeam() {
return team;
}
package com.luv2code.springdemo;
public interface FortuneService {
public String getFortune();
}
package com.luv2code.springdemo;
import java.util.Random;
import org.springframework.stereotype.Component;
//with @Component, the class is ready to be scanned by Spring.
@Component
public class RandomFortuneService implements FortuneService {
// create an array of strings
private String[] data = {
"perseverance is the key to success.",
"Diligence is the mother of good luck.",
"Your Journey towards the success is itself a reward."
};
private Random myRandom = new Random();
@Override
public String getFortune() {
int index = myRandom.nextInt(data.length);
String theFortune = data[index];
return theFortune;
}
package com.luv2code.springdemo;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class PracticeHelloSpringApp {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
SwimCoach coach = context.getBean("swimCoach",SwimCoach.class);
//In the above code,SwimCoach defines two extra methods getEmail and getTeam which are not a part of Coach interface.
//So we need to create coach variable of type SwimCoach class with which we can access interface methods as well as class methods.
System.out.println(coach.getDailyWorkout());
System.out.println(coach.getDailyFortune());
System.out.println(coach.getEmail());
System.out.println(coach.getTeam());
context.close();
}
package com.luv2code.springdemo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
//Incase of default bean ID, It would be "pingPongCoach".
@Component("myPingPongCoach")
public class PingPongCoach implements Coach {
private FortuneService fortuneService;
//This is the case when we have multiple implementations of FortuneService and we are constructor injection for
//injecting dependency with the use of @Autowired and @Qualifier. Check the @Qualifier annotation inside of the constructor arguments.
@Autowired
public PingPongCoach(@Qualifier("randomFortuneService") FortuneService fortuneService) {
super();
System.out.println("inside constructor of PingPongCoach");
this.fortuneService = fortuneService;
}
@Override
public String getDailyWorkout() {
return "Ping Pong is no sport...You don't need any practice!!";
}
@Override
public String getDailyFortune() {
return fortuneService.getFortune();
}
}
}
applicationContext.xml
<?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">
<context:component-scan base-package="com.luv2code.springdemo"/>
<context:property-placeholder location="classpath:sport.properties"/>
如果您注意到,在PingPongCoach的构造函数中有一个,它是Coach接口的一个完全不同的实现。这里我还使用了@限定符(“随机财富服务”)作为构造函数注入
PingPongCoach.java
package com.luv2code.springdemo;
public interface Coach {
public String getDailyWorkout();
public String getDailyFortune();
package com.luv2code.springdemo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class SwimCoach implements Coach {
@Autowired
@Qualifier("randomFortuneService")
private FortuneService fortuneService;
@Value("${foo.email}")
private String email;
@Value("${foo.team}")
private String team;
public SwimCoach() {
System.out.println("Inside Swimcoach no-arg constructor");
}
@Override
public String getDailyWorkout() {
return "Swim 1000 meters as a warm up.";
}
@Override
public String getDailyFortune() {
return fortuneService.getFortune();
}
public String getEmail() {
return email;
}
public String getTeam() {
return team;
}
package com.luv2code.springdemo;
public interface FortuneService {
public String getFortune();
}
package com.luv2code.springdemo;
import java.util.Random;
import org.springframework.stereotype.Component;
//with @Component, the class is ready to be scanned by Spring.
@Component
public class RandomFortuneService implements FortuneService {
// create an array of strings
private String[] data = {
"perseverance is the key to success.",
"Diligence is the mother of good luck.",
"Your Journey towards the success is itself a reward."
};
private Random myRandom = new Random();
@Override
public String getFortune() {
int index = myRandom.nextInt(data.length);
String theFortune = data[index];
return theFortune;
}
package com.luv2code.springdemo;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class PracticeHelloSpringApp {
public static void main(String[] args) {
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
SwimCoach coach = context.getBean("swimCoach",SwimCoach.class);
//In the above code,SwimCoach defines two extra methods getEmail and getTeam which are not a part of Coach interface.
//So we need to create coach variable of type SwimCoach class with which we can access interface methods as well as class methods.
System.out.println(coach.getDailyWorkout());
System.out.println(coach.getDailyFortune());
System.out.println(coach.getEmail());
System.out.println(coach.getTeam());
context.close();
}
package com.luv2code.springdemo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
//Incase of default bean ID, It would be "pingPongCoach".
@Component("myPingPongCoach")
public class PingPongCoach implements Coach {
private FortuneService fortuneService;
//This is the case when we have multiple implementations of FortuneService and we are constructor injection for
//injecting dependency with the use of @Autowired and @Qualifier. Check the @Qualifier annotation inside of the constructor arguments.
@Autowired
public PingPongCoach(@Qualifier("randomFortuneService") FortuneService fortuneService) {
super();
System.out.println("inside constructor of PingPongCoach");
this.fortuneService = fortuneService;
}
@Override
public String getDailyWorkout() {
return "Ping Pong is no sport...You don't need any practice!!";
}
@Override
public String getDailyFortune() {
return fortuneService.getFortune();
}
}
谁能告诉我为什么在这里调用PingPongCoach No arg构造函数吗?好的,你用
@Component
@Component("myPingPongCoach")
public class PingPongCoach implements Coach
因此,在组件扫描期间,spring会找到它并对其进行初始化
您为构造函数提供了一个参数以抑制默认构造函数,因此Spring将选择它。
为什么乒乓球没有参数构造函数?你为什么问这个。您的日志说正在调用公共乒乓球控制台(@Qualifier(“randomFortuneService”)FortuneService FortuneService)
。@MạnhQuyế特恩盖伊ễn嗨,先生,我感谢你在这里的努力。但是我对Spring完全陌生,并且一直在实践这些代码,正如在源代码中所说的。我不明白如何从SwimCoach类调用公共乒乓球Coach(@Qualifier(“randomFortuneService”)FortuneService-FortuneService)?是什么在他们之间建立了联系?如果您能让我明白这一点…删除类的@Component将有助于理解和应用…谢谢