user3310115
user3310115

Reputation: 1460

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. What type of dependency injection is this? Is it by type because the name for sure doesn't match?

@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;
    }
}

Upvotes: 0

Views: 977

Answers (1)

Harsh
Harsh

Reputation: 972

@Configuration annotation serves as a placeholder to mention that whichever classes annotated with @Configuration are holding the bean definitions!

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 !

To answer your question, it should create a bean and this is a setter based injection!

However, your @Bean must be some user defined or business entity class in ideal scenarios!

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/

Upvotes: 1

Related Questions