Professional Documents
Culture Documents
(/)
Spring (https://www.baeldung.com/category/spring/) +
1. Overview
In this article, we'll introduce the concepts of IoC (Inversion of Control) and DI
(Dependency Injection), and we'll then take a look at how these are
implemented in the Spring framework.
https://www.baeldung.com/inversion-control-and-dependency-injection-in-spring#constructor-based-dependency-injection 1/11
1/4/2020 Inversion of Control and Dependency Injection with Spring | Baeldung
Further reading:
Wiring in Spring: @Autowired, @Resource and @Inject
(https://www.baeldung.com/spring-annotations-
resource-inject-autowire)
This article will compare and contrast the use of annotations related to
dependency injection, namely the @Resource, @Inject, and @Autowired
annotations.
In the next sections, we'll see how we can provide the implementation of Item
through metadata.
Both IoC and DI are simple concepts, but have deep implications in the way
we structure our systems, so they're well worth understanding well.
1 ApplicationContext context
2 = new ClassPathXmlApplicationContext("applicationContext.xml");
To set the item attribute in the example above, we can use metadata. Then,
the container will read this metadata and use it to assemble beans at runtime.
Dependency Injection in Spring can be done through constructors, setters
or elds.
https://www.baeldung.com/inversion-control-and-dependency-injection-in-spring#constructor-based-dependency-injection 4/11
1/4/2020 Inversion of Control and Dependency Injection with Spring | Baeldung
1 @Configuration
2 public class AppConfig {
3
4 @Bean
5 public Item item1() {
6 return new ItemImpl1();
7 }
8
9 @Bean
10 public Store store() {
11 return new Store(item1());
12 }
13 }
The @Con guration annotation indicates that the class is a source of bean
de nitions. Also, we can add it to multiple con guration classes.
The @Bean annotation is used on a method to de ne a bean. If we don't
specify a custom name, the bean name will default to the method name.
For a bean with the default singleton scope, Spring rst checks if a cached
instance of the bean already exists and only creates a new one if it doesn't. If
we're using the prototype scope, the container returns a new bean instance
for each method call.
Another way to create the con guration of the beans is through XML
con guration:
For setter-based DI, the container will call setter methods of our class, after
invoking a no-argument constructor or no-argument static factory method to
instantiate the bean. Let's create this con guration using annotations:
1 @Bean
2 public Store store() {
3 Store store = new Store();
4 store.setItem(item1());
5 return store;
6 }
We can also use XML for the same con guration of beans:
https://www.baeldung.com/inversion-control-and-dependency-injection-in-spring#constructor-based-dependency-injection 6/11
1/4/2020 Inversion of Control and Dependency Injection with Spring | Baeldung
8. Autowiring Dependencies
https://www.baeldung.com/inversion-control-and-dependency-injection-in-spring#constructor-based-dependency-injection 7/11
1/4/2020 Inversion of Control and Dependency Injection with Spring | Baeldung
1 @Bean(autowire = Autowire.BY_TYPE)
2 public class Store {
3
4 private Item item;
5
6 public setItem(Item item){
7 this.item = item;
8 }
9 }
We can also inject beans using the @Autowired annotation for autowiring by
type:
If there's more than one bean of the same type, we can use the @Quali er
annotation to reference a bean by name:
Next, let's inject a bean named item into the item property of store bean by
name through XML:
By default, the container creates and con gures all singleton beans during
initialization. To avoid this, you can use the lazy-init attribute with value true on
the bean con guration:
As a consequence, the item1 bean will be initialized only when it's rst
requested, and not at startup. The advantage of this is faster initialization time,
but the trade-o is that con guration errors may be discovered only after the
bean is requested, which could be several hours or even days after the
application has already been running.
10. Conclusion