VOOZH about

URL: https://thenewstack.io/spring-framework-has-three-major-pitfalls-heres-what-to-do/

⇱ Spring Framework Has Three Major Pitfalls — Here’s What To Do - The New Stack


TNS
SUBSCRIBE
Join our community of software engineering leaders and aspirational developers. Always stay in-the-know by getting the most important news and exclusive content delivered fresh to your inbox to learn more about at-scale software development.
REQUIRED
It seems that you've previously unsubscribed from our newsletter in the past. Click the button below to open the re-subscribe form in a new tab. When you're done, simply close that tab and continue with this form to complete your subscription.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.
Welcome and thank you for joining The New Stack community!
Please answer a few simple questions to help us deliver the news and resources you are interested in.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Great to meet you!
Tell us a bit about your job so we can cover the topics you find most relevant.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Welcome!

We’re so glad you’re here. You can expect all the best TNS content to arrive Monday through Friday to keep you on top of the news and at the top of your game.

What’s next?

Check your inbox for a confirmation email where you can adjust your preferences and even join additional groups.

Follow TNS on your favorite social media networks.

Become a TNS follower on LinkedIn.

Check out the latest featured and trending stories while you wait for your first TNS newsletter.

PREV
1 of 2
NEXT
VOXPOP
As a JavaScript developer, what non-React tools do you use most often?
Angular
0%
Astro
0%
Svelte
0%
Vue.js
0%
Other
0%
I only use React
0%
I don't use JavaScript
0%
Thanks for your opinion! Subscribe below to get the final results, published exclusively in our TNS Update newsletter:
NEW! Try Stackie AI
From clobbered drafts to real-time sync
Apr 14th 2026 10:00am, by David Moore
TypeScript 6.0 RC arrives as a bridge to a faster future
Mar 14th 2026 9:00am, by Darryl K. Taft
Mastra empowers web devs to build AI agents in TypeScript
Jan 28th 2026 11:00am, by Loraine Lawson
2024-05-13 06:58:09
Spring Framework Has Three Major Pitfalls — Here’s What To Do
sponsor-sonarsource,sponsored-post-contributed,
DevOps / Frontend Development

Spring Framework Has Three Major Pitfalls — Here’s What To Do

Spring has some fantastic features that aid in the development process, but it’s vital to understand the framework's limitations and drawbacks.
May 13th, 2024 6:58am by Jonathan Vila Lopez
👁 Featued image for: Spring Framework Has Three Major Pitfalls — Here’s What To Do
Image from terimma on Shutterstock.
Sonar sponsored this post. Insight Partners is an investor in Sonar and TNS.

Spring is a classic framework — 50% of developers use it these days, and it’s great for creating standalone production-grade applications. With its new classes, interfaces and APIs aiding the development process, developers must educate themselves to decide whether they want to use it in their coding. This is because misuse of Spring Boot’s new features can lead to bugs, misconfigurations and security issues that can impact code quality.

There are three important points to be aware of when using the Spring framework.

Transactional Operations

Database operations must all be committed in order to be available to other connections. That means that for every operation done to the database, the process entails having to open a transaction, change the data and commit the transaction, or roll back the transaction if anything fails.

Spring can annotate methods to create proxies via @Transactional, generating code that operates seamlessly within the codebase to manage transactions. However, you may have chains of method calls where an operation makes multiple changes to the database and those changes have to be split into several methods for clarity. That’s where transactional propagation takes place.

Usually, we have an entry point method with the @Transactional annotation that starts the transaction. The rest of the methods in the call chain won’t specify the annotation, which allows the first method to do the whole commit. This is the required default propagation method. If there’s no transaction running, it’ll create one.

But reality is often more complex than we would imagine. For example, say you have methods that are part of different operations, and sometimes your method is the only operation that fits. In these chains of calls, we have to keep compatible transaction propagation, but Spring won’t consider the transaction specifications of self-invocation.

So, what does this mean? When you call a method from another one in the same class, Spring will use the “this” approach to refer to the receiver method. Spring then generates the code as a proxy to handle transactions that can’t be executed.

To avoid that, in the methods that can execute other methods inside transactions, we should specify the @Transaction annotation.

Persistent Entities

One of the great things about Spring is its ease of interaction with the persistence layer. In order to use typed objects and properties, Java provides an @Entity annotation to represent a relational table, and Spring provides the @Document annotation to represent MongoDB and ElasticSearch documents. In these cases, Spring can use the information in the element and make a bridge between the object domain and the database domain.

It’s critical to understand here that these objects represent data objects with a direct conversion to the stored elements in the database, meaning all fields carried by that object will be saved in the database. Spring is able to share methods to generate REST API services that execute when the user makes an HTTP request to that server. These methods also enable the use of entities or documents as arguments that Spring will map from the request payload.

To prevent security issues like an attacker impersonating a user, it’s encouraged to use data transfer objects (DTOs) to translate the information coming from the user into the entity or document. This will consider only the necessary information and sanitize the translation too.

Bean Definitions

A main power of Spring is its dependency injection, which enables users to define beans that will be injected into other objects and their lifespan. With this feature, classes only have to know what their dependencies are. It doesn’t require understanding how and when they have to be instantiated and deleted.

Spring also has a bean discovery mechanism; it scans source code packages to search for bean definitions. The Spring context then instantiates them according to the configuration. But with this power comes responsibility. It’s important to be aware that this scanning mechanism can affect overall application performance and induce runtime errors that are challenging to spot while coding. The key to preventing this is always having a package in the application as the starting point of the bean scan for Spring.

Spring, with its dependency injection framework, offers a powerful injection mechanism on the consumer side of those beans. This makes very easy-to-use instances of beans, with specific life scopes, without needing to worry about when and where those beans have been created or deleted. To avoid the injection of beans being done before it’s needed, which can hurt application performance, it’s recommended to not use the @Autowired annotation. Instead, the injection should be requested as late as possible when it’s needed by parameter injection. This will tell Spring that the bean needs to be created right before the creation of the dependent bean.

Final Thoughts

Spring has some fantastic features that aid in the development process, but it also comes with complex configurations. It’s vital to understand Spring’s limitations and drawbacks to get the most value out of it, but that can be tough.

It’s not always clear where the code can potentially create disruption of performance and stability. This is where static analysis solutions like Sonar can help, with rules that cover and spot major issues, providing warning during the coding process as well as performing constant monitoring in the CI/CD pipeline. With the right rules in place to ensure quality, it’s easier to feel confident that the code being written will result in software that adds real value and doesn’t become a liability.

Sonar is the industry standard for code verification and automated code review, trusted by 75% of the Fortune 100. Its SonarQube platform analyzes over 750 billion lines of code daily, helping to prevent outages, reduce risk, lower technical debt, and ensure compliance.
Learn More
The latest from Sonar
Hear more from our sponsor
TRENDING STORIES
Jonathan Vila Lopez is developer advocate at Sonar. He is a Java Champion and cofounder of the conferences in Spain JBCNConf and DevBcn, an organizer at Barcelona Java Users Group (JUG), and a member of BarcelonaJUG. He has also been...
Read more from Jonathan Vila Lopez
Sonar sponsored this post. Insight Partners is an investor in Sonar and TNS.
SHARE THIS STORY
TRENDING STORIES
TNS owner Insight Partners is an investor in: Sonar.
SHARE THIS STORY
TRENDING STORIES
TNS DAILY NEWSLETTER Receive a free roundup of the most recent TNS articles in your inbox each day.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.