Tech, told as stories

Spring Fiasco

· 10 min read

I opened the project. Every file was there. Every dependency was in the pom.xml. I had done this a hundred times.

It didn’t compile.

Not a typo. Not a missing semicolon. Just a wall of red, errors pointing at classes that very clearly existed, and IntelliJ looking at me like I’d asked it to read Latin.

This is the Spring fiasco. If you’re a Java developer, you’ve lived it. If you haven’t yet, you will.

The Perfect Plan That Wouldn’t Start

In Ocean’s Eleven, Danny Ocean has the perfect plan to rob three casinos at once. He has a crew of specialists, each one the best at exactly one thing. The plan only works if every one of them shows up, knows their role, and trusts the others to do theirs.

A Spring project is exactly that. Hundreds of specialists (your classes, your libraries, your configs), each doing one job, all wired together. When it works, it’s beautiful.

When it doesn’t, nobody tells you which crew member didn’t show up.

Two Pickpockets, One Job

After hours of staring, I found it. Not in my code. In my .m2 folder, the local warehouse where Maven keeps every library it has ever downloaded.

There were two conflicting versions of the same library sitting there. Maven was picking one, my code expected the other, and the build fell apart. Imagine two crew members both showing up claiming to be the pickpocket. One of them has the wrong plan.

Here’s what I did, and what I’d do in this order now, before you lose an evening like I did:

# 1. Ask Maven to stop trusting what it cached and check again
mvn clean install -U

# 2. Still broken? Find out who's fighting. This shows every library and who brought it in
mvn dependency:tree -Dverbose

# 3. Search for one suspect specifically
mvn dependency:tree -Dincludes=com.fasterxml.jackson.core

If one library is clearly corrupted or doubled up, delete just its folder in ~/.m2/repository and let Maven download it fresh. You don’t need to nuke the whole .m2. That’s like firing the entire crew because one guy lost his keys.

Then IntelliJ. Sometimes Maven is fine and the IDE is the one living in the past:

  • Click Reload All Maven Projects in the Maven tool window.
  • If that doesn’t work: File → Invalidate Caches → Invalidate and Restart.

I did all of this again and again. Clean, install, invalidate, restart, pray. It’s exhausting, mostly because you don’t know what’s wrong. The error says “cannot find symbol”. It never says “your .m2 has two versions of the same jar, and the wrong one won.”

So the first lesson: when Spring won’t build and your code looks fine, the problem is usually not your code. It’s your dependencies.

The Background Check: Black Duck

Just when the build finally went green, the next boss fight arrived: Black Duck.

Black Duck is a security scanner that goes through every library your project uses, including the libraries your libraries use, and checks them against known vulnerabilities. Think of it as the background check before the heist. It looks at your crew and says: “That one. The guy you hired for the vault. He has a record.”

So you replace him with a newer, patched version. Simple, right?

No. Because the new version might not get along with the rest of the crew. Upgrade one library and suddenly another one, three levels deep, refuses to work with it. Now you’re back to the build failing, except this time it’s on purpose.

This is where I started living on mvnrepository.com. For any library, it shows every version, which ones have known vulnerabilities, and what each version itself depends on. My routine became:

  1. Black Duck flags a library.
  2. Look it up on mvnrepository and find the nearest version without the vulnerability.
  3. Check what that version depends on, and whether it fits with everything else in the project.
  4. Override it, rebuild, run the tests, scan again.

And here are the two tools that make the override clean:

<!-- Force one version for the whole project, no matter who brings it in -->
<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>org.yaml</groupId>
      <artifactId>snakeyaml</artifactId>
      <version>2.2</version>
    </dependency>
  </dependencies>
</dependencyManagement>
<!-- Or: stop a library from bringing a bad crew member in the first place -->
<dependency>
  <groupId>some.group</groupId>
  <artifactId>some-library</artifactId>
  <exclusions>
    <exclusion>
      <groupId>bad.group</groupId>
      <artifactId>bad-library</artifactId>
    </exclusion>
  </exclusions>
</dependency>

And the biggest lesson of all: if you’re on Spring Boot, let Spring Boot pick the versions. The Spring Boot parent (or its BOM) already manages a big list of libraries in versions that are tested to work together. Every time you hard-code a version for something Boot already manages, you’re hiring a crew member Danny never vetted. Only override when you have a reason, like a Black Duck finding, and write a comment saying why.

So that’s the fiasco: Spring’s world is so big that the dependencies alone can take you down.

But here’s the twist. Spring is also the one that cleans it all up.

Danny Ocean Is the Spring Container

Look at how Danny runs the heist. He doesn’t make every crew member go find their own partners. He recruits everyone, tells each person exactly what to do, and introduces them to the people they need to work with. The pickpocket doesn’t need to know where the explosives guy came from. He just knows he’ll be there.

That’s Spring. In Spring language:

  • The crew members are beans: objects that Spring creates and manages for you.
  • Danny is the container (the ApplicationContext): it creates the beans and keeps them.
  • Introducing them to each other is dependency injection: instead of a class creating what it needs with new, Spring hands it in.
@Service
public class OrderService {

    private final PaymentClient paymentClient;

    // Spring sees this constructor and hands in the PaymentClient bean.
    // No "new PaymentClient()", no wiring by hand.
    public OrderService(PaymentClient paymentClient) {
        this.paymentClient = paymentClient;
    }
}

This is called inversion of control. Your class doesn’t control how it gets its tools anymore. Spring does. That sounds like losing control, but it’s the opposite: your classes get small, focused, and easy to swap out. Which matters a lot later, when we test.

The Annotations: How Danny Knows Who’s Who

Annotations are how you tell Spring what each class is for. They’re the labels on the crew’s name tags. Without them, you’d be writing pages of XML or wiring code by hand, which is how Java was done before, and nobody misses it.

The ones you’ll use constantly:

Annotation What it tells Spring
@SpringBootApplication “Start here.” Turns on configuration, auto-configuration and component scanning in one go
@Component “This is a bean. Manage it.”
@Service A bean that holds business logic
@Repository A bean that talks to the database (and translates database errors for you)
@RestController A bean that handles web requests and returns data
@Configuration + @Bean “Here’s how to build a bean I can’t annotate myself”, e.g. a library class
@Value("${...}") Inject a value from your config files
@Qualifier / @Primary “There are two beans of this type. Use this one.”

That last row is worth remembering, because it’s the bean-loading version of the two-pickpockets problem. When Spring finds two beans of the same type and doesn’t know which one to inject, it refuses to start and tells you there’s no unique bean. Mark one @Primary, or ask for a specific one with @Qualifier.

And its cousin, the other error you’ll meet: Spring says it can’t find a bean you’re sure exists. Nine times out of ten, the class is in a package that sits outside the folder of your @SpringBootApplication class, so component scanning never saw it. Danny never met that crew member. Move the class under the main package, and it shows up.

The Replica Vault: Testing With Mockito

My favourite part of Ocean’s Eleven is that before the real heist, they build a full replica of the vault and rehearse on it. They don’t practise on the real casino. That would be insane.

Testing is the same. You don’t want your unit tests calling a real payment system or a real database. You build a replica. That’s what Mockito does: it creates fake versions of your dependencies that behave exactly how you tell them to.

And this is where dependency injection pays off. Because OrderService gets its PaymentClient handed in through the constructor, you can just as easily hand in a fake one:

@ExtendWith(MockitoExtension.class)
class OrderServiceTest {

    @Mock
    PaymentClient paymentClient;      // the replica

    @InjectMocks
    OrderService orderService;        // the real class, using the replica

    @Test
    void placesOrderWhenPaymentSucceeds() {
        when(paymentClient.charge(any())).thenReturn(true);

        assertTrue(orderService.placeOrder(new Order()));
        verify(paymentClient).charge(any());
    }
}

No Spring, no database, no network. Runs in milliseconds.

When you do want Spring in the test, for example to check that a controller is wired correctly, Spring gives you test “slices” so you only start the part you need:

  • @WebMvcTest starts just the web layer.
  • @DataJpaTest starts just the database layer.
  • @SpringBootTest starts everything (slower, so save it for real end-to-end checks).
  • @MockitoBean swaps a real bean for a Mockito replica inside that Spring test. (Older code uses @MockBean, which is deprecated since Spring Boot 3.4.)

Lombok: The Forger Who Writes All the Paperwork

Every heist needs a forger, someone who quietly produces all the boring documents nobody wants to write by hand.

In Java, that boring paperwork is getters, setters, constructors, toString, equals and hashCode. For a class with ten fields, that’s easily a hundred lines of code that says nothing interesting.

That’s Lombok. (Small but important detail: Lombok isn’t part of Spring. It’s a separate library, but almost every Spring project uses it.)

// Without Lombok: ten fields, a hundred lines of getters and setters.
// With Lombok:
@Data
public class Customer {
    private String name;
    private String email;
    private int loyaltyPoints;
}

@Data writes the getters, setters, toString, equals and hashCode for you when the code compiles. You never see them, but they’re there.

A few more I use all the time:

  • @Getter / @Setter: when you only want some of it.
  • @Builder: lets you write Customer.builder().name("Asha").build().
  • @RequiredArgsConstructor: writes the constructor for all your final fields. Put it on a Spring service and you get constructor injection for free, without writing the constructor.
  • @Slf4j: gives you a log field, ready to use.

One warning from experience: be careful with @Data on database entities. Auto-generated equals, hashCode and toString can trip over lazy-loaded relationships. For entities, @Getter and @Setter are the safer choice.

And if Lombok’s generated methods “don’t exist” in IntelliJ even though the code builds fine, you’re back in the fiasco: make sure annotation processing is turned on in the IDE settings.

The Crew’s Rules

Everything I learned the hard way:

  1. When the build breaks and your code looks fine, check dependencies first. mvn dependency:tree before anything else.
  2. Fix the cache surgically. Delete one bad folder in .m2, not the whole thing. Invalidate IntelliJ’s caches only when Maven itself builds fine.
  3. Let Spring Boot manage versions. Override only with a reason, and leave a comment saying what the reason is.
  4. Treat Black Duck findings like a background check. Check the fix on mvnrepository, override it in one place, rebuild, retest, rescan.
  5. Use constructor injection. It makes classes honest about what they need, and makes them trivial to test.
  6. Keep your beans under the main package. Or Spring will never meet them.
  7. Rehearse on a replica. Mockito for unit tests, test slices when you need Spring, full @SpringBootTest only when you must.
  8. Let Lombok write the paperwork, except on entities, where you stay a little more careful.

The Heist Pays Off

Spring will give you some of the worst evenings of your career. A build that won’t start, a cache that lies, a security scan that makes you rearrange your whole dependency list.

But it’s also the reason a crew of hundreds of classes can work together without any of them knowing how the others were built. It hands each piece exactly what it needs, lets you swap any of them for a replica when you test, and with a few annotations writes away the code nobody wanted to write.

Spring creates the fiasco. Spring also cleans it up.

You just have to know which crew member to call.