The Spring Framework has come a long way. Nowadays, it is way easier to start a Java project with the Spring Framework using Spring Boot. In the past, we used to download Spring individual jar files and configure the dependencies manually. Even though we had Apache Maven, things were still tricky, especially for relatively huge projects. With Spring Boot, developers now can focus more on implementing the business requirements. However, those who have just started learning Spring may find it has a steep learning curve. This post shows how to create a Spring Dependency Injection examples without using Spring Boot to get a better understanding of how things work.
Create A Java Project With Maven
Before we start working with the Spring Framework, we need a Maven project. Please go through the Create Java Project Using Maven in Command-Line post to create that Maven project.
Get Spring Core Dependencies From Maven Central
The latest version of Spring as of this writing is 5.28.RELEASE. Go to the online Maven Repository and copy the XML snippets as shown below and place them between the <dependencies> tags in pom.xml.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | <!-- https://mvnrepository.com/artifact/org.springframework/spring-core --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>5.2.8.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-context --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.2.8.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>5.2.8.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-context-support --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>5.2.8.RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-aop --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>5.2.8.RELEASE</version> </dependency> |
For our Spring dependency injection example, we only need five dependencies.
Dependency Injection Example With @ComponentScan
In this example, we use @ComponentScan to pick up all classes with @Component annotations from a specific package – com.turreta.mavenproj.
For this example, we need to change the MyConfiguration class as follows.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | package com.turreta.mavenproj; import com.turreta.mavenproj.beans.PersonBean; import com.turreta.mavenproj.beans.PetBean; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; //@Configuration public class MyConfiguration { @Bean public PersonBean person(@Autowired PetBean pet) { PersonBean personBean = new PersonBean(); personBean.setPet(pet); return personBean; } @Bean public PetBean petBean() { return new PetBean(); } } |
Commenting out the annotation prevents the following run-time error.
1 2 3 4 5 6 7 | Exception in thread "main" org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'com.turreta.mavenproj.beans.PersonBean' available: expected single matching bean but found 2: personBean,person at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveNamedBean(DefaultListableBeanFactory.java:1200) at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveBean(DefaultListableBeanFactory.java:420) at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:350) at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:343) at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1127) at com.turreta.mavenproj.App.main(App.java:24) |
Using Java Configuration
We will not use XML configuration because it is ancient, and we already stepped away from Spring Boot to make a dependency injection example. To keep things simpler, we will stick with Java configuration classes. Note the App class has different codes now.
The MyConfiguration class has the following content.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | package com.turreta.mavenproj; import com.turreta.mavenproj.beans.PersonBean; import com.turreta.mavenproj.beans.PetBean; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class MyConfiguration { @Bean public PersonBean person(@Autowired PetBean pet) { PersonBean personBean = new PersonBean(); personBean.setPet(pet); return personBean; } @Bean public PetBean petBean() { return new PetBean(); } } |
Those are examples of Spring Dependency Injection without Spring Boot.