Mit ‘Automatic Binding for Guice’ getaggte Beiträge

So,

it was quite for some time now. (12 days :)) My vacations are over and I have to develop GAB (Automatic Binding for Guice) in my spare time, but no problem so far. This means my evenings will be longer. 🙂

First good news: GAB is now listed as official 3rd-Party Extension for Google Guice!
Second good news: I release v0.7! 🙂

The blog was quite, but github was under fire 🙂

So what’s changed so far?

Summary:

  • Added Automatic Binding of AOP Interceptors
  • Added Automatic Binding Configurations for java.util.Properties
  • Added Automatic Binding Configurations Apache Commons Configurations
  • Added rocoto (Simple Configuration) to the Integrations
  • Improved JavaDoc, JUnit-Tests and Examples

Added Automatic Binding of AOP Interceptors

This Feature is one of the coolest things. 🙂 You can use your existing AOP-Alliance compatible MethodInterceptors. Just annotate them with @Interceptor and they will be bound automatically.
But Guice needs some more Informations:

  • Which Classes should be monitored
  • Which Methods of this Class should be intercepted

Normally you would pass a Matcher-Object to the Guice-Binder. Due the fact that this should happen automatically, I introduced two Annotations, which will mark the Methods, which returns the Matcher-Object. @ClassMatcher and @MethodMatcher

@Interceptor
public class AnnotatedMethodInterceptor{
    @Invoke
    public Object invoke(MethodInvocation invocation) throws Throwable {
	return invocation.proceed();
    }

    @ClassMatcher
    public Matcher<? super Class<?>> getClassMatcher() {
	return Matchers.any();
    }

    @MethodMatcher
    public Matcher<? super Method> getMethodMatcher() {
	return Matchers.annotatedWith(Intercept.class);
    }
}

This Example is a complete Implementation of a MethodInterceptor. There are different ways you could go. Use your existing MethodInterceptors, which implement The MethodInterceptor Interface.
If you choose this way, you need to annotate at least two Methods for the Matchers.

The second way can be, that you extend my abstract GuiceMethodInterceptor Class, so you need no Annotations.

Or you are going the complete dynamic way, like the one displayed above. Just annotate the Matcher-Methods and the Method which should be invoked, if a monitored Method matches the Criterias.

Added Automatic Binding Configurations for java.util.Properties

GAB is now able, to bind your needed Configurations automatically. 🙂

You just need one annotation, which informs GAB, if you want a Named- and/or Lazy-Binding and where it find the Configuration File (Classpath, File, URL)

@Configuration(name="config", path="/configuration.properties", pathType=PathType.CLASSPATH)
public class Example{
    @Inject @Named("config")
    private Properties config;
...
}

Added Automatic Binding Configurations Apache Commons Configurations

Or are you more a Friend of Apache Commons Configurations?

@Configuration(name="config", bind=PropertyListConfiguration.class, path="http://devsurf.de/guice/configuration.plist", pathType=PathType.URL)
public class Example{
    @Inject @Named("config")
    private PropertyListConfiguration config;

...or...

    @Inject @Named("config")
    private Configuration config;
}

Added rocoto (Simple Configuration) to the Integrations

So you don’t want a Properties or Configuration-Object? You could use the rocoto-Extension (rocoto-googlecode and homepage). This allows you to direct inject your values into an Object. GAB will do the Rest for your. (Automatically Binding/Installation of rocoto-Features)

@Configuration(path="/configuration.properties", pathType=PathType.CLASSPATH)
public class ExampleImpl implements Example {
    @Inject @Named("message")
    private String message;
    
    @Override
    public String sayHello() {
	return "sayHello() - "+message;
    }
}

No boilerplates anymore! 🙂

Checkout the Examples

So I just can say, check out the Examples and JUnit-Test! You will find several Examples, how to use the different Modules.

Straight to v1.0

The Release of the v1.0 is not so far away. At the moment it is planned for the End of October. Why so long? I’ll make sure that it is not only tested on Windows. I’m going to create a Web Application for Remote Configurations. So you can keep the Customer specific Things out of your Applications. This one will be tested at least under Windows/Linux and Tomcat/Glassfish.

After finishing that, I’ll release! Promised! 🙂

Btw: I tested the last release with the Google Guice 3.0-Branch. Works like a charm! Using only JSR330-Annotations leads to no heavy dependencies to Guice. (and if you want to remove all Source Code-Dependencies – checkout the JNDI-Example for Guice)

Advertisements