一 只有1个无参构造函数
1 代码
public class Test {
public static void main(String[] args) {
// Spring FrameWork 只是 Spring 家族中的一个成员,Spring 一共有24个项目
// context 就是一个构造 Bean 的工厂,或者理解为 Bean 容器
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
UserService userService = context.getBean("userService", UserService.class);
}
}
@ComponentScan("beandemo.service")
public class AppConfig {
}
@Component
public class UserService /*implements InitializingBean*/ {
public UserService() {
System.out.println("1");
}
}
2 测试
1
3 说明
只有1个无参的构造方法,调用无参构造方法
二 有1个无参构造方法和1个非无参构造方法
1 代码
public class Test {
public static void main(String[] args) {
// Spring FrameWork 只是 Spring 家族中的一个成员,Spring 一共有24个项目
// context 就是一个构造 Bean 的工厂,或者理解为 Bean 容器
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
UserService userService = context.getBean("userService", UserService.class);
}
}
@ComponentScan("beandemo.service")
public class AppConfig {
}
@Component
public class UserService /*implements InitializingBean*/ {
public UserService() {
System.out.println("1");
}
public UserService(UserService userService) {
System.out.println("2");
}
}
2 测试
1
3 说明
有一个无参构造方法和一个非无参构造方法,调用无参构造方法
三 有两个非无参构造方法
1 代码
public class Test {
public static void main(String[] args) {
// Spring FrameWork 只是 Spring 家族中的一个成员,Spring 一共有24个项目
// context 就是一个构造 Bean 的工厂,或者理解为 Bean 容器
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
UserService userService = context.getBean("userService", UserService.class);
}
}
@ComponentScan("beandemo.service")
public class AppConfig {
}
@Component
public class UserService /*implements InitializingBean*/ {
public UserService(OrderService orderService) {
this.orderService = orderService;
System.out.println("2");
}
public UserService(OrderService orderService, OrderService orderService1) {
System.out.println("3");
}
}
2 测试
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [beandemo.service.UserService]: No default constructor found; nested exception is java.lang.NoSuchMethodException: beandemo.service.UserService.<init>()
3 说明
报错了,Spring 无法判断调用哪个。
四 只有1个非无参构造方法
1 代码
public class Test {
public static void main(String[] args) {
// Spring FrameWork 只是 Spring 家族中的一个成员,Spring 一共有24个项目
// context 就是一个构造 Bean 的工厂,或者理解为 Bean 容器
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
UserService userService = context.getBean("userService", UserService.class);
}
}
@ComponentScan("beandemo.service")
public class AppConfig {
}
@Component
public class UserService /*implements InitializingBean*/ {
public UserService(OrderService orderService) {
this.orderService = orderService;
System.out.println("2");
}
}
2 测试
1
3 说明
只有1个非无参的构造方法,调用该构造方法
五 告诉 Spring 使用哪个构造方法
1 代码
public class Test {
public static void main(String[] args) {
// Spring FrameWork 只是 Spring 家族中的一个成员,Spring 一共有24个项目
// context 就是一个构造 Bean 的工厂,或者理解为 Bean 容器
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
UserService userService = context.getBean("userService", UserService.class);
}
}
@ComponentScan("beandemo.service")
public class AppConfig {
}
@Component
public class UserService /*implements InitializingBean*/ {
public UserService(OrderService orderService) {
this.orderService = orderService;
System.out.println("2");
}
// 告诉 Spring 使用哪个构造方法
@Autowired
public UserService(OrderService orderService, OrderService orderService1) {
System.out.println("3");
}
public UserService() {
System.out.println("1");
}
}
2 测试
3
3 说明
哪个构造方法上使用
@Autowired 注解
,调用该构造方法。
六 小结
- 当一个类中只有一个构造方法,就使用这个构造方法。
- 当一个类中有多个构造方法,并且有无参构造方法,就使用无参构造方法。
- 当一个类中有多个构造方法,并且多个构造方法中没有无参构造方法,Spring 就会报错,因为 Spring 也不知道使用哪个构造方法。
- 当一个类中明确指定了使用哪个构造方法,就调用该构造方法。