Nowadays, most of us use Spring Boot, and we tend to skip the basics of the Spring Framework. Even those new to Spring would not think twice about turning to Spring Boot to develop new applications. Without Spring Boot, how can we have an application that uses the minimum Spring Framework dependency for IOC?
Spring Framework With Spring Boot
Without Spring Boot, we craft our pom.xml – not from scratch! Consider the following pom.xml file. We stripped it down from its Spring Boot version to the bare minimum. From hereon, we add the bare minimum dependency for an application that only uses the Spring Framework IoC.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.turreta.learnspring</groupId> <artifactId>spring-minimum-ioc</artifactId> <version>0.0.1-SNAPSHOT</version> <name>spring-minimum-ioc</name> <description>spring-minimum-ioc</description> <properties> <maven.compiler.source>11</maven.compiler.source> <maven.compiler.target>11</maven.compiler.target> </properties> <dependencies> ... </dependencies> <build> <plugins> ... </plugins> </build> </project> |
The Only Spring Framework Dependency We Need: spring-context (IoC)
To have an application that has the essential dependency for us to try out the framework’s IoC feature, we only need the spring-context. For example, we can have the following lines in our pom.xml
1 2 3 4 5 6 7 8 9 | ... <dependencies> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.3.25</version> </dependency> </dependencies> ... |
Does it look too good to be true? It does. But the spring-context dependency has other dependencies. When we build our application, Maven uses that dependency information within spring-context to fetch the other dependencies.
Therefore, the minimum number of dependencies we need for a Spring application that only uses IoC is 6. These are spring-aop, spring-beans, spring-context, spring-core, spring-expression, and spring-jcl.
Testing Our Spring Application for IoC
Now we can craft some codes and test our Spring application for IoC. To do that, we need some actual codes.
We have codes that represent a service and a DAO it uses.
1 2 3 4 5 6 7 8 9 | package com.turreta.leanspring; public class Dao { @Override public String toString() { return "In Dao"; } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | package com.turreta.leanspring; public class Service { private Dao dao; public Service(Dao dao) { this.dao = dao; } public Dao getDao() { return dao; } @Override public String toString() { return "In Service"; } } |
Then, we have the configuration file that will instruct Spring on how to combine our beans. In this case, Spring will create instances of Dao and Service classes. Then, it will inject the Dao object into the Service object.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | package com.turreta.leanspring; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class AppConfig { @Bean public Dao dao() { return new Dao(); } @Bean public Service service(Dao dao) { return new Service(dao); } } |
Lastly, we have the main Java class.
1 2 3 4 5 6 7 8 9 10 11 12 13 | package com.turreta.leanspring; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class BareMinimumIoCApplication { public static void main(String[] args) { ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class); Service service = context.getBean(Service.class); System.out.println(service); System.out.println(service.getDao()); } } |
When we run the codes, we get the following output.
Download the codes from this GitHub URL.