Quickstart

Add ProxyBuilder, wrap a service at runtime, see logging output.

This page shows the smallest possible ProxyBuilder usage end to end: add the dependency, define an interface, wrap an instance with pre and post hooks.

1. Add the dependency

In your pom.xml:

<dependency>
  <groupId>com.svenruppert</groupId>
  <artifactId>proxybuilder</artifactId>
  <version>00.10.00</version>
</dependency>

2. Define an interface and an implementation

public interface GreetingService {
  String greet(String name);
}

public class GreetingServiceImpl implements GreetingService {
  @Override
  public String greet(String name) {
    return "Hello, " + name;
  }
}

3. Wrap it with the builder

import com.svenruppert.proxybuilder.proxy.dymamic.DynamicProxyBuilder;

GreetingService proxy = DynamicProxyBuilder
    .createBuilder(GreetingService.class, new GreetingServiceImpl())
    .addIPreAction((original, method, args) ->
        System.out.println("before " + method.getName()))
    .addIPostAction((original, method, args) ->
        System.out.println("after " + method.getName()))
    .build();

System.out.println(proxy.greet("world"));

Run it. You will see:

before greet
after greet
Hello, world

What just happened

DynamicProxyBuilder.createBuilder(...) returned a builder. Each call to addIPreAction and addIPostAction registered a hook that runs around the real method invocation. .build() produced a JDK dynamic proxy that implements GreetingService and delegates to your hooks and your implementation.

Where to go next

Edit this page on GitHub ↗