无论是否使用@Autowired,构造函数都可以正常工作
Constructor works fine with and without @Autowired
我有 tennisCoach
由 Spring 框架创建的对象:
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml") ;
Coach theCoach = context.getBean("tennisCoach", Coach.class);
无法理解为什么我需要在下面代码的 TennisCoach
构造函数中添加 @Autowired
注释。无论是否使用 @Autowired
注释,它都可以正常工作。
@Component
public class TennisCoach implements Coach {
private FortuneService fortuneService;
@Autowired
public TennisCoach(FortuneService theFortuneService) {
fortuneService = theFortuneService;
}
@Override
public String getDailyWorkout() {
return "Practice your backhand volley";
}
@Override
public String getDailyFortune() {
return fortuneService.getFortune();
}
}
UPD
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:component-scan>
</beans>
这里@Autowired用于构造函数注入。 TennisCoach 依赖于 FortuneService,它是通过构造函数注入的。我不确定您是如何在 applicationContext.xml
中配置 bean 的
来自 @Autowired
Javadoc:
If a class only declares a single constructor to begin with, it will always be used, even if not annotated.
从 Spring 4.3 开始,只要在 class 中拥有唯一的构造函数,就不需要 @Autowired
注释。
我有 tennisCoach
由 Spring 框架创建的对象:
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml") ;
Coach theCoach = context.getBean("tennisCoach", Coach.class);
无法理解为什么我需要在下面代码的 TennisCoach
构造函数中添加 @Autowired
注释。无论是否使用 @Autowired
注释,它都可以正常工作。
@Component
public class TennisCoach implements Coach {
private FortuneService fortuneService;
@Autowired
public TennisCoach(FortuneService theFortuneService) {
fortuneService = theFortuneService;
}
@Override
public String getDailyWorkout() {
return "Practice your backhand volley";
}
@Override
public String getDailyFortune() {
return fortuneService.getFortune();
}
}
UPD
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:component-scan>
</beans>
这里@Autowired用于构造函数注入。 TennisCoach 依赖于 FortuneService,它是通过构造函数注入的。我不确定您是如何在 applicationContext.xml
中配置 bean 的来自 @Autowired
Javadoc:
If a class only declares a single constructor to begin with, it will always be used, even if not annotated.
从 Spring 4.3 开始,只要在 class 中拥有唯一的构造函数,就不需要 @Autowired
注释。