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() {
        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 也不知道使用哪个构造方法。
  • 当一个类中明确指定了使用哪个构造方法,就调用该构造方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值