Spring vs. Spring Boot: A Comparison of These Java Frameworks
Want to learn more about these two popular Java frameworks? Check out this look at Spring and Spring Boot and how they each solve a different type of problem.
Join the DZone community and get the full member experience.
Join For Freewhat is spring boot? and, what is a spring framework? what are their goals? how can we compare them? there must be a lot of questions running through your mind. at the end of this blog, you will have the answers to all of these questions. in learning more about the spring and spring boot frameworks, you will come to understand that each solve a different type of problem.
what is spring? what are the core problems spring solves?
the spring framework is one of the most popular application development frameworks for java. one of the best features in spring is that it has the dependency injection (di) or inversion of control (ioc), which allows us to develop loosely coupled applications. and, loosely coupled applications can be easily unit-tested.
example without dependency injection
consider the example below —
mycontroller
depends on
myservice
to perform a certain task. so, to get the instance of myservice, we will use:
myservice service = new myservice();
now, we have created the instance for
myservice
, and we see both are tightly coupled. if i create a mock for
myservice
in a unit test for
mycontroller
, how do i make
mycontroller
use the mock? it's bit difficult — isn't it?
@restcontroller
public class mycontroller {
private myservice service = new myservice();
@requestmapping("/welcome")
public string welcome() {
return service.retrievewelcomemessage();
}
}
example with a dependency injection
with the help of only two annotations, we can get the instance of
myservice
easily, which is not tightly coupled. the spring framework does all the hard work to make things simpler.
-
@component
is simply used in the spring framework as a bean that you need to manage within your own beanfactory (an implementation of the factory pattern). -
@autowired
is simply used to in the spring framework to find the correct match for this specific type and autowire it.
so, spring framework will create a bean for
myservice
and autowire it into
mycontroller
.
in a unit test, i can ask the spring framework to auto-wire the mock of
myservice
into
mycontroller
.
@component
public class myservice {
public string retrievewelcomemessage(){
return "welcome to innovationm";
}
}
@restcontroller
public class mycontroller {
@autowired
private myservice service;
@requestmapping("/welcome")
public string welcome() {
return service.retrievewelcomemessage();
}
}
the spring framework has many other features, which are divided into twenty modules to solve many common problems. here are some of the more popular modules:
- spring jdbc
- spring mvc
- spring aop
- spring orm
- spring jms
- spring test
- spring expression language (spel)
aspect oriented programming(aop) is another strong side of the spring framework. the key unit in object-oriented programming is the class , whereas, in aop, the key unit is the aspect . for example, if you want to add the security in your project, logging, etc., you can just use the aop and keep these as a cross-cutting concern away from your main business logic. you can perform any action after a method call, before a method call, after a method returns, or after the exception arises.
the spring framework does not have its own orm, but it provides a very good integration with orm, like hibernate, apache ibatis, etc.
in short, we can say that the spring framework provides a decoupled way of developing web applications. web application development becomes easy with the help of these concepts in spring, like dispatcher servlet, modelandview, and view resolver.
if spring can solve so many problems, why do we need spring boot?
now, if you have already worked on spring, think about the problem that you faced while developing a full-fledged spring application with all functionalities. not able to come up with one? let me tell you — there was lot of difficulty to setup hibernate datasource, entity manager, session factory, and transaction management. it takes a lot of time for a developer to set up a basic project using spring mvc with minimum functionality.
<bean
class="org.springframework.web.servlet.view.internalresourceviewresolver">
<property name="prefix">
<value>/web-inf/views/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<mvc:resources mapping="/webjars/**" location="/webjars/"/>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>
org.springframework.web.servlet.dispatcherservlet
</servlet-class>
<init-param>
<param-name>contextconfiglocation</param-name>
<param-value>/web-inf/my-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
when we use hibernate, we have to configure these things like a datasource, entitymanager, etc.
<bean id="datasource" class="com.mchange.v2.c3p0.combopooleddatasource"
destroy-method="close">
<property name="driverclass" value="${db.driver}" />
<property name="jdbcurl" value="${db.url}" />
<property name="user" value="${db.username}" />
<property name="password" value="${db.password}" />
</bean>
<jdbc:initialize-database data-source="datasource">
<jdbc:script location="classpath:config/schema.sql" />
<jdbc:script location="classpath:config/data.sql" />
</jdbc:initialize-database>
<bean
class="org.springframework.orm.jpa.localcontainerentitymanagerfactorybean"
id="entitymanagerfactory">
<property name="persistenceunitname" value="hsql_pu" />
<property name="datasource" ref="datasource" />
</bean>
<bean id="transactionmanager" class="org.springframework.orm.jpa.jpatransactionmanager">
<property name="entitymanagerfactory" ref="entitymanagerfactory" />
<property name="datasource" ref="datasource" />
</bean>
<tx:annotation-driven transaction-manager="transactionmanager"/>
how does spring boot solve this problem?
-
spring boot does all of those using
autoconfiguration
and will take care of all the internal dependencies that your application needs — all you need to do is run your application. spring boot will auto-configure with the dispatcher servlet, if springjar
is in the class path. it will auto-configue to the datasource, if hibernatejar
is in the class path. spring boot gives us a pre-configured set of starter projects to be added as a dependency in our project. - during web-application development, we would need the jars that we want to use, which versions of the jars to use, and how to connect them together. all web applications have similar needs, for example, spring mvc, jackson databind, hibernate core, and log4j (for logging). so, we had to choose the compatible versions of all these jars. in order to decrease the complexity, spring boot has introduced what we call spring boot starters.
dependency for spring web project
<dependency>
<groupid>org.springframework</groupid>
<artifactid>spring-webmvc</artifactid>
<version>4.2.2.release</version>
</dependency>
<dependency>
<groupid>com.fasterxml.jackson.core</groupid>
<artifactid>jackson-databind</artifactid>
<version>2.5.3</version>
</dependency>
<dependency>
<groupid>org.hibernate</groupid>
<artifactid>hibernate-validator</artifactid>
<version>5.0.2.final</version>
</dependency>
<dependency>
<groupid>log4j</groupid>
<artifactid>log4j</artifactid>
<version>1.2.17</version>
</dependency>
starters are a set of convenient dependencies that you can include in your spring boot application. for using spring and hibernate, we just have to include the spring-boot-starter-data-jpa dependency in the project.
dependency for spring boot starter web
<dependency>
<groupid>org.springframework.boot</groupid>
<artifactid>spring-boot-starter-web</artifactid>
</dependency>
the following screenshot shows the different packages under a single dependency that are added into our application:
there are other packages that you will see. once you add that starter dependency, the spring boot starter web comes pre-packaged with all of these. as a developer, we would not need to worry about these dependencies and their compatible versions.
spring boot starter project options
these are few starter projects to help us get started quickly with developing specific types of applications.
- spring-boot-starter-web-services: soap web services
- spring-boot-starter-web: web and restful applications
- spring-boot-starter-test: unit testing and integration testing
- spring-boot-starter-jdbc: traditional jdbc
- spring-boot-starter-hateoas: add hateoas features to your services
- spring-boot-starter-security: authentication and authorization using spring security
- spring-boot-starter-data-jpa: spring data jpa with hibernate
- spring-boot-starter-cache: enabling spring framework’s caching support
- spring-boot-starter-data-rest: expose simple rest services using spring data rest
Published at DZone with permission of Ankit Kumar. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments