栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring 推断构造方法

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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  {
    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  {
    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  {
    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.()

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  {
    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  {
    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 也不知道使用哪个构造方法。
  • 当一个类中明确指定了使用哪个构造方法,就调用该构造方法。
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/695304.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号