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");
}
}
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");
}
}
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");
}
}
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>()
报错了,Spring 无法判断调用哪个。
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");
}
}
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");
}
// 告诉 Spring 使用哪个构造方法
@Autowired
public UserService(OrderService orderService, OrderService orderService1) {
System.out.println("3");
}
public UserService() {
System.out.println("1");
}
}
3
哪个构造方法上使用@Autowired 注解,调用该构造方法。
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/chengqiuming/article/details/122279193
内容来源于网络,如有侵权,请联系作者删除!