eBook – Guide Spring Cloud – NPI EA (cat=Spring Cloud)
announcement - icon

Let's get started with a Microservice Architecture with Spring Cloud:

>> Join Pro and download the eBook

eBook – Mockito – NPI EA (tag = Mockito)
announcement - icon

Mocking is an essential part of unit testing, and the Mockito library makes it easy to write clean and intuitive unit tests for your Java code.

Get started with mocking and improve your application tests using our Mockito guide:

Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Reactive – NPI EA (cat=Reactive)
announcement - icon

Spring 5 added support for reactive programming with the Spring WebFlux module, which has been improved upon ever since. Get started with the Reactor project basics and reactive programming in Spring Boot:

>> Join Pro and download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Jackson – NPI EA (cat=Jackson)
announcement - icon

Do JSON right with Jackson

Download the E-book

eBook – HTTP Client – NPI EA (cat=Http Client-Side)
announcement - icon

Get the most out of the Apache HTTP Client

Download the E-book

eBook – Maven – NPI EA (cat = Maven)
announcement - icon

Get Started with Apache Maven:

Download the E-book

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

eBook – RwS – NPI EA (cat=Spring MVC)
announcement - icon

Building a REST API with Spring?

Download the E-book

Course – LS – NPI EA (cat=Jackson)
announcement - icon

Get started with Spring and Spring Boot, through the Learn Spring course:

>> LEARN SPRING
Course – RWSB – NPI EA (cat=REST)
announcement - icon

Explore Spring Boot 3 and Spring 6 in-depth through building a full REST API with the framework:

>> The New “REST With Spring Boot”

Course – LSS – NPI EA (cat=Spring Security)
announcement - icon

Yes, Spring Security can be complex, from the more advanced functionality within the Core to the deep OAuth support in the framework.

I built the security material as two full courses - Core and OAuth, to get practical with these more complex scenarios. We explore when and how to use each feature and code through it on the backing project.

You can explore the course here:

>> Learn Spring Security

Course – LSD – NPI EA (tag=Spring Data JPA)
announcement - icon

Spring Data JPA is a great way to handle the complexity of JPA with the powerful simplicity of Spring Boot.

Get started with Spring Data JPA through the guided reference course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – LJB – NPI EA (cat = Core Java)
announcement - icon

Code your way through and build up a solid, practical foundation of Java:

>> Learn Java Basics

Partner – LambdaTest – NPI EA (cat= Testing)
announcement - icon

Distributed systems often come with complex challenges such as service-to-service communication, state management, asynchronous messaging, security, and more.

Dapr (Distributed Application Runtime) provides a set of APIs and building blocks to address these challenges, abstracting away infrastructure so we can focus on business logic.

In this tutorial, we'll focus on Dapr's pub/sub API for message brokering. Using its Spring Boot integration, we'll simplify the creation of a loosely coupled, portable, and easily testable pub/sub messaging system:

>> Flexible Pub/Sub Messaging With Spring Boot and Dapr

1. Overview

We can override an instantiated object‘s behavior via runtime behavior modifications, which we typically achieve through design patterns and frameworks rather than by directly altering the object’s class at runtime.

Let’s discuss four approaches to modify the behavior of an existing object.

2. Adding Logging to a Calculator

Let’s say that we have a Calculator interface and implementation that defines two basic methods, add and subtract:

public interface Calculator {
    int add(int a, int b);
    int subtract(int a, int b);
}

public class SimpleCalculator implements Calculator {
    @Override
    public int add(int a, int b) {
        return a + b;
    }

    @Override
    public int subtract(int a, int b) {
        return a - b;
    }
}

Let’s further say that we’d like to track method call count or add logging to these methods, but we either can’t or don’t want to modify the class itself. In other words, when someone calls add(3, 5), or subtract(10, 3), we also want to track a count of method calls, or log some logging messages using the SLF4J Logback framework.

There are at least four ways we could consider doing this:

  • Subclass
  • The Decorator Pattern
  • JDK Dynamic Proxy
  • Spring’s ProxyFactory

3. Subclassing

Subclassing is a straightforward approach. Simply put, we can extend SimpleCalculator and override its methods add and subtract to include logging:

public class LoggingCalculator extends SimpleCalculator {

    @Override
    public int add(int a, int b) {
        log.debug("LOG: Before addition.");
        int result = super.add(a, b);
        log.debug("LOG: After addition. Result: {}", result);
        return result;
    }

    @Override
    public int subtract(int a, int b) {
        log.debug("LOG: Before subtraction.");
        int result = super.subtract(a, b);
        log.debug("LOG: After subtraction. Result: {}", result);
        return result;
    }
}

Now, if we construct a LoggingCalculator instead of a SimpleCalculator, we’ll get the logging behavior that we want. Thereupon, we verify using JUnit 5 test assertions that we can use this subclass:

@Test
void givenACalculatorClass_whenSubclassingToAddLogging_thenLoggingCalculatorCanBeUsed() {
    Calculator calculator = new LoggingCalculator();
    assertEquals(8, calculator.add(5, 3));
    assertEquals(2, calculator.add(5, 3));
}

There’s a limitation, though. For example, what do we do if we already have an instance of SimpleCalculator at runtime? Therefore, we need to find runtime approaches to overriding method behavior, which is what we discuss next.

4. Using the Decorator Pattern

Decorator is a design pattern that provides the benefits of subclassing while also addressing its limitations. It’s a structural pattern that enables behavior to be added to an individual object. Further, we can add behavior either statically or dynamically. Moreover, we can add behavior without affecting the class itself or the behavior of other objects from the same class.

Let’s define a decorator class called MeteredCalculatorDecorator that implements the Calculator interface, and overrides its two methods to track method call count:

public class MeteredCalculatorDecorator implements Calculator {
    private final Calculator wrappedCalculator;
    private final Map<String, Integer> methodCalls;

    public MeteredCalculatorDecorator(Calculator calculator) {
        this.wrappedCalculator = calculator;
        this.methodCalls = new HashMap<>();
        methodCalls.put("add", 0);
        methodCalls.put("subtract", 0);
    }

    @Override
    public int add(int a, int b) {
        methodCalls.merge("add", 1, Integer::sum);
        return wrappedCalculator.add(a, b);  
    }

    @Override
    public int subtract(int a, int b) {
        methodCalls.merge("subtract", 1, Integer::sum);
        return wrappedCalculator.subtract(a, b);  
    }

    public int getCallCount(String methodName) {
        return methodCalls.getOrDefault(methodName, 0);
    }
}

Accordingly, this decorator wraps behavior, and thus, when we pass it a Calculator object, it wraps its behavior to add tracking of method call count. As before, we can verify that it does extend the methods’ behavior with a test method:

@Test
void givenACalculator_whenUsingMeteredDecorator_thenMethodCallsAreCountedCorrectly() {
    Calculator simpleCalc = new SimpleCalculator();
    MeteredCalculatorDecorator decoratedCalc = new MeteredCalculatorDecorator(simpleCalc);
    decoratedCalc.add(10, 5);
    decoratedCalc.add(2, 3);
    decoratedCalc.subtract(10, 5);
    assertEquals(15, decoratedCalc.add(10, 5), "Core functionality must still work.");
    assertEquals(3, decoratedCalc.getCallCount("add"), "The 'add' method should have been called 3 times.");
    assertEquals(1, decoratedCalc.getCallCount("subtract"), "The 'subtract' method should have been called 1 time.");
}

5. Using a JDK Dynamic Proxy

Alternatively, we can use a JDK dynamic proxy. A JDK dynamic proxy generates a proxy class and object at runtime, implementing one or more interfaces. Further, it redirects method calls on the proxy to a custom InvocationHandler.

Let’s create an invocation handler that intercepts all method calls on the proxy to add logging:

public class LoggingInvocationHandler implements InvocationHandler {
    private final Object target;

    public LoggingInvocationHandler(Object target) {
        this.target = target;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        
        log.debug("PROXY LOG: Intercepting method: {}", method.getName());
        Object result = method.invoke(target, args);
        log.debug("PROXY LOG: Method {} executed.", method.getName());
        return result;
    }
}

Afterward, let’s use a test method to verify that we can generate and use the dynamic proxy with a Calculator object:

@Test
void givenACalculator_whenUsingJdkDynamicProxy_thenJdkDynamicProxyCanBeUsed() {
    Calculator simpleCalc = new SimpleCalculator();
    LoggingInvocationHandler handler = new LoggingInvocationHandler(simpleCalc);

    Calculator proxyCalc = (Calculator) Proxy.newProxyInstance(
        Calculator.class.getClassLoader(),
        new Class<?>[] { Calculator.class },
        handler
    );

    assertEquals(30, proxyCalc.add(20, 10));
    assertEquals(10, proxyCalc.subtract(20, 10));
}

6. Using Spring’s ProxyFactory

Or, we can use Spring’s ProxyFactory, a sophisticated utility that abstracts the proxy creation mechanism. Additionally, it automatically chooses between JDK Dynamic Proxy (for interfaces) and CGLIB (for concrete classes), enabling us to inject method interceptors (AOP Advice).

Spring’s MethodInterceptor is similar to InvocationHandler but uses AOP-standard interfaces instead.

To begin with, let’s add dependencies for Spring AOP (aspect-oriented programming) to the pom.xml:

<dependencies>
    ...
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aop</artifactId>
        <version>6.0.11</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>6.0.11</version>
    </dependency>
</dependencies>

Then, let’s create the Spring equivalent of an InvocationHandler:

public class LoggingMethodInterceptor implements MethodInterceptor {
    
    @Override
    public Object invoke(MethodInvocation invocation) throws Throwable {
        
        log.debug("SPRING PROXY: Intercepting method: {}", invocation.getMethod().getName());
        Object result = invocation.proceed();
        log.debug("SPRING PROXY: Method {} completed.", invocation.getMethod().getName());
        return result;
    }
}

Afterward, let’s verify that we can use Spring’s ProxyFactory with a Calculator object:

@Test
void givenACalculator_whenUsingSpringProxyFactory_thenSpringProxyFactoryCanBeUsed() {
    SimpleCalculator simpleCalc = new SimpleCalculator();
    ProxyFactory factory = new ProxyFactory();
        
    factory.setTarget(simpleCalc);
    factory.addAdvice(new LoggingMethodInterceptor());

    Calculator proxyCalc = (Calculator) factory.getProxy();

    assertEquals(60, proxyCalc.add(50, 10));
    assertEquals(40, proxyCalc.subtract(50, 10));
}

7. Choosing an Approach

Let’s review the use case for each of these approaches:

Feature Subclassing Decorator Pattern JDK Dynamic Proxy Spring’s ProxyFactory
When to Use When we want to change behavior for all new instances of a derived type. When we need to dynamically add new behavior to individual objects, and these modifications must be applied at the point of instantiation/assembly. Excellent for cross-cutting concerns like logging, caching, or validation. When we need to apply cross-cutting concerns (logging, security, transactions) to many objects without modifying the source code, and without writing specific decorator classes. When working within the Spring ecosystem, or when we need a robust, unified way to apply cross-cutting concerns (AOP) that handles both interfaces (JDK proxy) and classes (CGLIB) automatically.
Limitations We can’t modify the behavior of an already instantiated object. It requires manual creation of a Decorator class for every set of new behaviors, which can lead to a large number of small, single-purpose classes. JDK Dynamic Proxy can only proxy interfaces. It can’t proxy concrete classes directly. Performance can be slightly lower than direct method calls. It requires the Spring AOP/Context dependency. It’s perhaps too heavyweight for a simple, standalone application.

8. Conclusion

In this article, we explored four ways to override a method’s behavior in Java. We also used a comparison table to choose the best approach for our use case.

As always, the full code for the examples is available over on GitHub.

Baeldung Pro – NPI EA (cat = Baeldung)
announcement - icon

Baeldung Pro comes with both absolutely No-Ads as well as finally with Dark Mode, for a clean learning experience:

>> Explore a clean Baeldung

Once the early-adopter seats are all used, the price will go up and stay at $33/year.

eBook – HTTP Client – NPI EA (cat=HTTP Client-Side)
announcement - icon

The Apache HTTP Client is a very robust library, suitable for both simple and advanced use cases when testing HTTP endpoints. Check out our guide covering basic request and response handling, as well as security, cookies, timeouts, and more:

>> Download the eBook

eBook – Java Concurrency – NPI EA (cat=Java Concurrency)
announcement - icon

Handling concurrency in an application can be a tricky process with many potential pitfalls. A solid grasp of the fundamentals will go a long way to help minimize these issues.

Get started with understanding multi-threaded applications with our Java Concurrency guide:

>> Download the eBook

eBook – Java Streams – NPI EA (cat=Java Streams)
announcement - icon

Since its introduction in Java 8, the Stream API has become a staple of Java development. The basic operations like iterating, filtering, mapping sequences of elements are deceptively simple to use.

But these can also be overused and fall into some common pitfalls.

To get a better understanding on how Streams work and how to combine them with other language features, check out our guide to Java Streams:

>> Join Pro and download the eBook

eBook – Persistence – NPI EA (cat=Persistence)
announcement - icon

Working on getting your persistence layer right with Spring?

Explore the eBook

Course – LS – NPI EA (cat=REST)

announcement - icon

Get started with Spring Boot and with core Spring, through the Learn Spring course:

>> CHECK OUT THE COURSE

Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

eBook Jackson – NPI EA – 3 (cat = Jackson)