简体   繁体   English

spring 是否注入 @Configuration 类中提供的任何 bean

[英]Does spring inject any beans provided in the @Configuration class

If I have a @Configuration class where I have a bean like below, will the dataMap be resolved in the constructor of the DataService class.如果我有一个@Configuration类,其中我有一个如下所示的 bean, dataMap是否会在DataService类的构造函数中解析。 What type of dependency injection is this?这是什么类型的依赖注入? Is it by type because the name for sure doesn't match?是因为名称肯定不匹配而by type划分吗?

@Bean
public Map<String, List<Data>> data() {
    final Map<String, List<Data>> dataMap = new HashMap<>();
    readings.put("1", new Data());
    return dataMap;
}

and a class和一堂课

@Service
public class DataService {

    private final Map<String, List<Data>> information;

    public DataService(Map<String, List<Data>> information) {
        this.information = information;
    }
}

@Configuration annotation serves as a placeholder to mention that whichever classes annotated with @Configuration are holding the bean definitions! @Configuration注解用作一个占位符,以提及使用@Configuration注解的任何类都持有 bean 定义!

When Spring application comes up, spring framework will read these definitions and create beans (or simply objects) in IOC (Inversion of control) container These would be Spring managed objects/beans !当 Spring 应用程序启动时,spring 框架将读取这些定义并在IOC (Inversion of control) container中创建beans (or simply objects)这些将是 Spring 管理的对象/bean!

To answer your question, it should create a bean and this is a setter based injection!要回答您的问题,它应该创建一个 bean,这是一个基于setter的注入!

However, your @Bean must be some user defined or business entity class in ideal scenarios!但是,在理想情况下,您的@Bean必须是某个用户定义的或业务实体类!

Few links for you to refer to:几个链接供您参考:

https://www.codingame.com/playgrounds/2096/playing-around-with-spring-bean-configuration https://www.linkedin.com/pulse/different-types-dependency-injection-spring-kashif-masood/ https://www.codingame.com/playgrounds/2096/playing-around-with-spring-bean-configuration https://www.linkedin.com/pulse/different-types-dependency-injection-spring-kashif-masood /

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM