我正在学习带有注释和自动接线的Springs
。我尝试了三种类型的自动接线
这是我的配置
<?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>
这些是我的实体
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";
}
}
足球教练
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();
}
}
板球教练
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;
}
}
我有三个用于执行三种自动接线方式的类,构造函数和设置器工作正常,但是当完成method
明智的自动接线时,就会调用错误的构造函数。我在BaseBallCoach
类中错过的一件事是默认构造函数,但是无论如何,编译器会自动为我生成一个吗?
这是我的 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();
}
}
这是输出
Default constructor
Feel energetic for first half of trainning
Practice curve whole day
输出的第一行是我不了解的Default constructor
,为什么要执行CricketCoach
的构造函数?
答案 0 :(得分:6)
由于CricketCoach
类带有@Component
注释,并且在Spring容器启动时扫描包,它将通过调用no-arg构造函数来创建类型为CricketCoach
的实例
所有在xml文件中定义或用@Component
注释或从扫描到的软件包进行@Component
的扩展名的bean都将在spring容器启动时创建,无论是否使用它们
答案 1 :(得分:1)
您要在此处创建Coach类的新实例:
Coach coach = context.getBean("baseBallCoach", Coach.class);
每次创建新实例时,它都会执行一个构造函数。您已在其中致电System.out.println("Default constructor");
。您可以尝试从板球教练训练中删除@Component。