tags:

views:

98

answers:

3

Hi

Someone knows some way that how can I achieve the same functionality in Guice as the 'afterPropertiesSet' interface in spring ? ( its a post construction hook )

+2  A: 

I guess using @PostConstruct is the way to go.

Here is a related blog post : http://macstrac.blogspot.com/2008/10/adding-support-for-postconstruct.html

And here is an addon library that provides the support : http://code.google.com/p/guiceyfruit/

Adding lifecycle support via Guiceyfruit is described here : http://code.google.com/p/guiceyfruit/wiki/Lifecycle

Timo Westkämper
`@PostConstruct` is the preferred approach in Spring, also
skaffman
I just dont understand , do I have to download some patch to add support for this JRS annotations ?
Roman
A: 

It seems it is not yet supported , so for everyone how wants this work , here is small solution.

public class PostConstructListener implements TypeListener{

    private static Logger logger = Logger.getLogger(PostConstructListener.class);

    @Override
    public <I> void hear(TypeLiteral<I> iTypeLiteral,final TypeEncounter<I> iTypeEncounter) {

        Class<? super I> type = iTypeLiteral.getRawType();

        ReflectionUtils.MethodFilter mf = new ReflectionUtils.MethodFilter() {
            @Override
            public boolean matches(Method method) {
                return method.isAnnotationPresent(PostConstruct.class);
            }
        };

        ReflectionUtils.MethodCallback mc = new ReflectionUtils.MethodCallback() {
            @Override
            public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
                if (!(method.getReturnType().equals(Void.TYPE) && method.getParameterTypes().length == 0))
                {
                    logger.warn("Only VOID methods having 0 parameters are supported by the PostConstruct annotation!" +
                            "method " + method.getName() + " skipped!");
                    return;

                }
                iTypeEncounter.register(new PostConstructInvoker<I>(method));
            }
        };

        ReflectionUtils.doWithMethods(type,mc,mf);
    }

    class PostConstructInvoker<I> implements InjectionListener<I>{

        private Method method;

        public PostConstructInvoker(Method method) {
            this.method = method;
        }

        @Override
        public void afterInjection(I o) {
            try {
                method.invoke(o);
            } catch (Throwable e) {
                logger.error(e);
            }
        }
    }
}

The ReflectionUtils package is defined in spring.

Bind this listener to any event with :

bindListener(Matchers.any(),new PostConstructListener());

in your guice module. Have fun

Roman
A: 

You'll want to read the CustomInjections page on the Guice wiki:

In addition to the standard @Inject-driven injections, Guice includes hooks for custom injections. This enables Guice to host other frameworks that have their own injection semantics or annotations. Most developers won't use custom injections directly; but they may see their use in extensions and third-party libraries. Each custom injection requires a type listener, an injection listener, and registration of each.

Jesse Wilson