DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
11 Monitoring and Observability Tools for 2023
Learn more
  1. DZone
  2. Coding
  3. Frameworks
  4. Perils of Spring Boot and Opinionated Frameworks

Perils of Spring Boot and Opinionated Frameworks

Check out this post to learn more about opinionated frameworks and Inversion of Control!

Daniel Sagenschneider user avatar by
Daniel Sagenschneider
CORE ·
Mar. 15, 19 · Presentation
Like (18)
Save
Tweet
Share
17.23K Views

Join the DZone community and get the full member experience.

Join For Free

We, developers, like abstraction. Without it, we could not build applications. Our programming disciplines even require that we code to abstractions and avoid coupling our code to detailed implementations.

However, what is the right abstraction for your application?

Sadly, the choice of abstractions really comes from our choice of framework. Frameworks are basically abstract solutions that we extend to solve our problem.

Unfortunately, frameworks like Spring Boot come opinionated about the threading models you use, interfaces you need to extend, possibly the data repositories that are applicable, and various other assumptions about your problem space. That's a lot of restrictions before I've even written my first line of code.

What we really want to do is explore the problem space first. This is what test-driven design is all about. We write tests to define what is successful code. Then, we implement code to pass those tests. As we go along writing tests to cover off requirements, we subsequently churn out working code for the application. In time, we get enough working code to release as the application.

So, this leads me to ask: when do we test the choice of framework?

Opinionated Frameworks Force Abstractions Too Early in the Development Process

Well, I guess we pay very experienced senior people to make this choice. So, this choice must be correct. It would not be for reasons like:

  • I (or our company) only know this framework, so we are using it

  • It's new and shiny with lots of buzz words, so we must use it

  • My CVs a little old, let's try something new

  • This one is cheaper

  • Architecture believed what it says on the tin

Regardless of the reason, the only way to test the framework choice is to build the application with it. And just for those of you who like opinionated frameworks (like Spring Boot), please tell me you write the most risky aspects first. This is so you can quickly discover if the framework's opinions match with your problem.

Sadly, even if you test with the most risky aspects, finding out that the framework decision is wrong can lead to a lot of wasted code. This arguably wastes a lot of money for the business and can lead to failing projects.

For example, say we choose Spring Reactive. Yay, we can make concurrent asynchronous calls out to various microservices. We can also use the latest in NoSQL data stores. This was all a great decision. However, over time, we realize that we have a small amount of data where integrity of the data is very important. We find that we want to use a relational database to solve this and then incorporate JPA on this database for easier interaction. However, our choice of Spring Reactive has disallowed this because it requires all I/O to be asynchronous (JPA is synchronous database calls). OK, yes, we can use Schedulers, but I seem to be continually doing workarounds for lack of transactions. The data consistency issues are starting to mount up and we're missing deadlines. I'm now in a position of "do I throw out all the Reactive code, or do I keep making work arounds, hoping it might all hang together? I definitely need to swap jobs before this hits production and we start supporting it. In my next job, I've learned to use Spring Servlets for this type of problem.

The flip side of this could also be easily the case. We start out wanting Spring Servlet for JPA interaction with a database. However, over time, we realize the database interaction is mostly read-only. What we really wanted was asynchronous I/O from Spring Reactive to collect data from multiple microservices and data stores concurrently. Unfortunately, with our upfront Spring Servlet choice, the data collection is just too slow. Our work around is to use async Servlets and spawn threads to make concurrent requests. This worked initially, but over time, the load increased. This significantly increased thread counts, resulting in thread scheduling starvation, which resulted in timeouts. I've really got no way to fix this without significant rewrites of the application. In my next job, I've learned to use Spring Reactive for this type of problem.

So, can we look to test the framework without having to throw out all our code?

Inverting Framework Control

Dependency injection went a long way in inverting control. When I write my Servlet handling method, I no longer need to pass in all my dependent objects. I would define dependencies, via @Inject , to have the framework make them available. The framework, subsequently, no longer dictates what objects my implementation can depend on.

However, there is a lot more to a framework than just the objects. Frameworks will impose some threading model and require me to extend certain methods. While dependency injection provides references to objects, the framework still has to call the methods on the objects to do anything useful. For example, Spring goes a long way to make the methods flexible, but it still couples you to Reactive or Servlet coding by the required return type from the method.

As I need the Spring framework to undertake dependency injection for my tests, I'm coupled to the particular Spring Servlet/Reactive abstractions before I even write my first line of code — an upfront choice that could be quite costly to change if I get wrong!

What I really want to do is:

  1. Write tests for my implementations (as we are always test driven, of course)

  2. Write my implementations

  3. Wire up my implementations together to become the application

Well, the first two are very simple:

  1. Write tests calling a method passing in mock objects

  2. Write implementation of the method to pass the test

The last becomes very hard. The reason the last becomes very hard is that there is no consistent way to call every method. Methods have different names, different parameters, different exceptions, possibly different threading requirements, and different return types. What we need is some facade over the methods to make them appear the same.

The Inversion of (Coupling) Control (IoC) provides this facade over the method via the ManagedFunction. The ManagedFunction interface does not indicate what thread to use, what parameters/return types are required, nor what exceptions may be thrown. This is all specified by the contained method implementation. The coupling is inverted so that the implementation specifies what it requires.

This inversion of coupling allows framework decisions to be deferred. As I can have all my methods invoked in a consistent way, I can go ahead and start writing implementations. These implementations may require Reactive coding to undertake asynchronous calls out to different microservices. Some of these implementations may require using JPA to write to relational databases. I really should not care at the start of building the system. I'm tackling the concrete problems to gain a better understanding of the real problem space. I know my methods can be invoked by the framework via wrapping them in a ManagedFunction. We can deal with determining the right framework later on, once we know more.

Actually, this is allowing the implementations to choose the appropriate abstractions to be provided by the framework. My implementations define what objects they require, what other methods they require calling, and what thread models they will require. The implementations are, effectively, defining what abstractions are required from the framework.

Therefore, it is no longer the framework being opinionated. It is your developer code that is allowed to be opinionated.

This allows your implementations to be opinionated about the most appropriate framework to use. No longer do you have to guess the framework based on vague understanding of the problem space. You can see what abstractions your implementations require and make a more informed choice of framework.

In effect, IoC has a deferred choice of the framework to much later in the development process. This is so you can can make the decision much more confidently. And isn't this what Agile says, defer the commitment until the last responsible moment?

Summary

In summary, why be forced to make too many upfront decisions about your application? In choosing the framework, you are making some significant choices is solving your problem space. As frameworks are opinionated, they impose a lot of coupling on your solution.

Rather, why can't I just start writing solutions to concrete problems and worry about how they fit together later on? This allows me to make choices regarding the appropriate abstractions (and subsequently, the framework) when I know a lot more about the problem space.

Inversion of (Coupling) Control gives us this ability to defer abstraction and framework choices to much later in the development process, when you are more informed to make the decision correctly.

Spring Framework Framework Spring Boot

Published at DZone with permission of Daniel Sagenschneider. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • 7 Most Sought-After Front-End Frameworks for Web Developers
  • 3 Main Pillars in ReactJS
  • Build an Automated Testing Pipeline With GitLab CI/CD and Selenium Grid
  • How To Create a Failover Client Using the Hazelcast Viridian Serverless

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: