Spring Framework is unarguably the most in-demand framework in the Java Enterprise development ecosystem. In this article, I am going to showcase a simple example using Spring Framework.
You can also check this tutorial in the following video:
If you are new to Spring, I encourage you to get a basic understanding of the concepts behind the Spring framework. One of my previous articles about what is Spring Framework can be a good starting point.
You can find more spring tutorials here.
In this section, I am going to discuss what do you need to start with:
- Java 8 or higher (JRE isn’t enough. Install Java SDK)
- IDE (I am using IntelliJ community edition)
- Basic knowledge of Spring
2. Why Spring Framework?
Spring framework surely makes it easy for the Java developers. Now, we are going to know more about the benefits of Spring framework.
- Spring framework is an extremely powerful framework, and thus automatically becomes the first choice for the developers. It successfully addresses the problems posed in Java EE.
- Another advantage of the Spring framework is modularity. Because of modularity, the developers easily choose to work on the specific modules. Examples of such modules are Spring JDBC, Spring MVC, and Spring ORM.
- Spring framework leads to ease in application testing. Testing occurs outside the enterprise container, resulting in the ease in testing.
- In addition, Spring framework supports the enterprise application development using POJO – Plain Old Java Object. POJO removes the need of importing very heavy enterprise containers.
- It is extremely easy to integrate Spring with any other frameworks like Struts, Hibernate, etc. Spring does not have any restriction on the imposition of one framework with the other.
- Spring is quite lightweight. This is a benefit for developing applications on systems with less memory and resources.
3. Spring Framework Architecture
Spring framework is a layered architecture and has many modules. Each module has its own functionality. Let us discuss each of the layers in detail. The image below shows the different modules in each of the layers.
3.1 The Core Container
The core container comprises of the core, bean, contexts, and the expression modules. The core and beans primarily give the fundamental base of the framework, including the dependency injection features. While the context builds onto the core and beans module, it is also a means to access the objects in a “framework-style” manner. The EL – expression language – helps in manipulating and querying the object graph at the runtime.
This module aids in the testing of different components of Spring with either JUnit or TestNG. It makes sure that the SoringApplicationContexts are consistently loaded, and also cached.
3.3 Data Access/Integration
This layer consists of the JDBC, ORM, OXM, JMS, and Transaction modules. The JDBC modules help the developer to drive away without doing the tedious coding for JDBC. While the ORM provides integration layers for the object-relational mapping APIs including the like of Hibernate and JPA, the OXM module is used for giving an abstraction layer – supporting the XML or Object mapping implementations. JMS – also known as Java Messaging services – contains the code for creating as well as consuming messages. At last, the transaction module supports classes that implement interfaces and all the Plain Old Java Objects.
The Web layer comprises of different modules like Web, Web-servlet, Web-Struts, and Web-Portlet. The Web module gives the basic web integration features such as multipart file-upload functionality. Web-Servlet is most commonly known, and it contains the Spring MVC (i.e. Model View Controller) implementation. Whereas the Web-Struts contain the basic support classes so that it can easily integrate a Struts web within the Spring application. Last, but not the least – Web Portlet provides the MVC implementation specifically to be used in a portlet environment, and provides similar functionality like that of Web-Servlet.
AOP stands for ‘Aspect-Oriented programming’. Spring AOP module provides AOP’s implementation to the developers. You can use the source-level metadata functionality in order to use the behavioral information into your code. While the Instrumentation module is specifically for the class support as well as the class loader implementations that are particularly used for application servers.
That’s all about the Spring architecture. Now, we can code it all out.
4. Get Started with Spring
Create a new maven project sprint demo and add below dependencies to your project:
I am going to show both XML based configuration and Java-based configuration.
Create a Java bean class
HellowWorld under the package
com.jcg.beans as below,
HelloWorld is a simple Java bean class with a single method. The bean then loads into the Spring Context. And different called objects consume the methods.
To load the HelloWorld bean into Spring Context using Java based configuration, create a config file
com.jcg.config package. Refer the below screenshot,
Each of the Java beans is annotated with
@Bean annotation. By default, beans are identified by their type. However, for more specific identification, they can be followed by a name.
Java beans can also be loaded to context using XML based configuration. Below is the sample XML configuration,
Java beans are configured in
<bean/> tag. spring-beans.xml file has to be created in
resources folder, that way
ClassPathXmlConfiguration class can pick it up without the absolute path.
To run the program,
- XML based configuration can be triggered by running
Above class loads, the Spring XML configuration from the classpath and the output of the program is as below,
- To trigger the Java-based configuration, run
Above class loads, the beans using annotations and output is as below,
5. How to load Spring beans from context?
In a standalone Spring application, Spring beans can be loaded from the configuration by making use of the below classes,
- ClassPathXmlApplicationContext – Open and read xml based application context configuration.
- AnnotationConfigApplicationContext – If you choose to use Java-based configuration instead of XML configuration, you need to use this class.
6. Additional knowledge
7. Download The Source Code
In this article, we have learned how a simple program can be created using the Spring framework.
You can download the full source code of this example here: Spring Framework Example
Last updated on May 17th, 2021