We will start with What Spring Context is and create a sample project, add dependencies, create a service with spring annotations and pragmatically instantiate a container.
Spring context is also termed as Spring IoC container which is responsible for instantiate, configure and assemble the beans by reading configuration meta data from XML, Java annotations and/ or Java code in configuration files.
Technologies used
- Spring 4.3.6.RELEASE
- Maven Complier 3.6.0
- Java 1.8
We will first create a simple maven project. We can select the maven-archtype-quickstart as archtype. Now, let's add a spring-framework-bom first.
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-framework-bom</artifactId>
<version>4.3.6.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
The benefit of adding this are to manage the version of the added spring dependencies from one place. By this, you can omit mentioning version number for spring dependencies. And, now add spring-context
as dependency in pom.xml
.
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<scope>runtime</scope>
</dependency>
Now, we will create a class GreetingService
which is eligible to get registered as bean in Spring context.
@Service
public class GreetingService {
private static final Logger logger = Logger.getLogger(GreetingService.class.getName());
public GreetingService() {}
public void greet() {
logger.info("Gaurav Bytes welcomes you for your first tutorial on Spring!!!");
}
}
@Service
annotation at class-level means that this is service and is eligible to be registered as bean in Spring context.
Now, we will create object of Spring context. We are using AnnotationConfigApplicationContext
as spring container. Also, there exists other spring container like ClassPathXmlApplicationContext
, GenericGroovyApplicationContext
etc. which we will discuss in future posts.
ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(
"cc.gaurav.blog.hellogb.service");
As you see at the time of object contruction of AnnotationConfigApplicationContext
, I am passing one string parameter. This parameter ( of varags type) is the basePackages which spring context will scan for bean registration.
Now, we will get object of bean by calling getBean()
on spring context.
GreetingService greetingService = context.getBean(GreetingService.class);
greetingService.greet();
At last, we are closing the spring container by calling close()
.
context.close();
It is important to close the spring context(container) after use. By closing it, we ensure that it will release all the resources and locks that its implementation might hold and will also destroy all the cached singleton beans.
We have also included maven-compiler-plugin in pom.xml
to compile the java sources with the configured java version (in our case it is Java 1.8).
You can also find the example code on Github.