我试图将FortuneService接口注入到TennisCoach类,但是遇到错误,如果尝试使用new关键字注入它,那么它工作正常。我通过创建HappyFortuneService对象尝试了它。我在TennisCoach课程中尝试过如下 FortuneService fortuneService =新的HappyFortuneService();但是,如果我尝试使用构造函数注入来注入它,则会给我以下错误。
我在线程“ restartedMain” java.lang.reflect.InvocationTargetException中遇到诸如Exception之类的错误
package com.annotation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import com.annotation.coach.Coach;
import com.annotation.coach.FortuneService;
import com.annotation.coach.HappyFortuneService;
import com.annotation.coach.TennisCoach;
@SpringBootApplication
public class Luv2CodeAnnotationApplication {
private static final Logger logger = LoggerFactory.getLogger(Luv2CodeAnnotationApplication.class);
public static void main(String[] args) {
SpringApplication.run(Luv2CodeAnnotationApplication.class, args);
Coach coach = new TennisCoach();
logger.info(coach.getDailyWorkout());
logger.info(coach.getDailyFortune());
}
}
package com.annotation.coach;
public interface Coach {
public String getDailyWorkout();
public String getDailyFortune();
}
package com.annotation.coach;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import com.annotation.Luv2CodeAnnotationApplication;
@Component
public class TennisCoach implements Coach {
private static final Logger logger = LoggerFactory.getLogger(Luv2CodeAnnotationApplication.class);
private FortuneService fortuneService;
public TennisCoach(FortuneService fortuneService) {
this.fortuneService = fortuneService;
}
public TennisCoach() {
}
@Override
public String getDailyWorkout() {
return "*************Tennis Coach Daily Workout*************";
}
@Override
public String getDailyFortune() {
logger.info("*********INVOKED***************");
return fortuneService.getFortune();
}
}
package com.annotation.coach;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
@Component
public class HappyFortuneService implements FortuneService {
@Override
public String getFortune() {
return "*************Happy Fortune Service*************";
}
}
package com.annotation.coach;
public interface FortuneService {
public String getFortune();
}
答案 0 :(得分:0)
如果使用new关键字创建任何bean且该bean不会添加到Spring Application Context中,则Coach coach = new TennisCoach();没有添加到应用程序上下文中。因此,TennisCoach类无法识别Autowired FortuneService。
您可以使用构造函数进行自动装配,当创建Luv2CodeAnnotationApplication时,会将Coach实例作为构造函数的参数注入。
请在下面找到修改后的代码
@SpringBootApplication
公共类Luv2CodeAnnotationApplication {
private static Coach coach;
private static final Logger logger = LoggerFactory.getLogger(Luv2CodeAnnotationApplication.class);
@Autowired
public Luv2CodeAnnotationApplication(Coach coach) {
Luv2CodeAnnotationApplication.coach = coach;
}
public static void main(String[] args) {
SpringApplication.run(Luv2CodeAnnotationApplication.class, args);
//Coach coach = new TennisCoach();
logger.info(coach.getDailyWorkout());
logger.info(coach.getDailyFortune());
}
}