Re: Comment on AOP in google-guice

127 views
Skip to first unread message

google...@googlecode.com

unread,
Apr 15, 2011, 5:36:07 PM4/15/11
to google-g...@googlegroups.com
Comment by Mas...@gmail.com:

This is lot easier to read:
{{{
String today_name = today.getDisplayName(DAY_OF_WEEK, LONG, ENGLISH)
if( today_name.equals("Saturday") || today_name.equals("Sunday") )
{
// ...
}
}}}

For more information:
http://code.google.com/p/google-guice/wiki/AOP

google...@googlecode.com

unread,
Jun 17, 2011, 1:06:49 PM6/17/11
to google-g...@googlegroups.com
Comment by dvilave...@gmail.com:

Is it possible to use field interceptors with guice? If not, are there
plans to support field interceptors in Guice AOP or are there any plugins
or is it possible to write a plugin to support field interceptors?

google...@googlecode.com

unread,
Oct 24, 2011, 5:25:27 PM10/24/11
to google-g...@googlegroups.com
Comment by mradu.ro...@gmail.com:

Hi,

I've used Guice AOP to handle database transactions after painfully using
programatically Spring transactions. I've written a small boilerplate code:
http://dailyjavatips.com/2011/10/24/database-transactions-google-guice-aop/

Cheers,
Mihai

google...@googlecode.com

unread,
May 17, 2012, 4:42:18 AM5/17/12
to google-g...@googlegroups.com
Comment by ant.kura...@gmail.com:

Is it possible to annotate entire class to intercept all methods (like
Spring does)?

google...@googlecode.com

unread,
Sep 18, 2012, 1:44:00 AM9/18/12
to google-g...@googlegroups.com
Comment by mukesh.k...@gmail.com:

Hi Below is my code, when i applied @Dirtycheck it was not applied at all.

Please suggest where I m getting wrong.

package com.csdcsystems.amanda.interceptor;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;


import com.google.inject.BindingAnnotation;

@Retention (RetentionPolicy.RUNTIME)
@Target( {ElementType.METHOD})
@BindingAnnotation
public @interface DirtyCheck
{


}




package com.csdcsystems.amanda.interceptor;
import com.google.inject.AbstractModule;
import com.google.inject.matcher.Matchers;

public class DirtyCheckModule extends AbstractModule {

public void configure() {
DirtyCheckInterceptor intercepter = new DirtyCheckInterceptor();
// requestInjection(intercepter);
bindInterceptor(Matchers.any(),
Matchers.annotatedWith(DirtyCheck.class), intercepter);



}
}



package com.csdcsystems.amanda.interceptor;

import com.csdcsystems.amanda.web.viewmodel.PermitEditViewModel;

import com.google.inject.Inject;

import java.lang.reflect.Method;
import java.util.Map;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;


public class DirtyCheckInterceptor implements MethodInterceptor {


public Object invoke(MethodInvocation invocation) throws Throwable {

System.out.println("hiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii");
Method method = invocation.getMethod();
//PermitEditViewModel
pViewModel=(PermitEditViewModel)invocation.getClass();
//pViewModel.get_initValues();
// pViewModel.get_dirtyValues();
DirtyCheck annotation = method.getAnnotation(DirtyCheck.class);
// Make sure the intercepter was called for a transaction method
if (annotation == null) {
return invocation.proceed();
}
if (invocation.getArguments().length == 0 ||
(invocation.getArguments()[0]. equals("Mukesh"))) {

throw new Exception("First parameter must not be Mukesh: " +
method.getName());

}
else
{
Object objModifiedValue=invocation.getArguments()[0];
System.out.print(objModifiedValue);
// if (objModifiedValue != null
//
&& !org.zkoss.lang.Objects.equals(pViewModel.get_initValues().get("StatusCode"),
// objModifiedValue)) {
// pViewModel.get_dirtyValues().put("StatusCode", new Boolean(true));
//
// } else {
// pViewModel.get_dirtyValues().put("StatusCode", new Boolean(false));
// }
}

return invocation.proceed();
}

}


@DirtyCheck
public void setStatusCode(SelectOption<String> statusCode) {
this.statusCode = statusCode;
updateDirtyValues(FIELD_STATUSCODE, statusCode);

google...@googlecode.com

unread,
Apr 10, 2013, 5:01:34 PM4/10/13
to google-g...@googlegroups.com
Comment by dave.che...@gmail.com:

Does Guice provide something like Spring AopUtils.isAopProxy(Object object)?
So I can check if a class is a enhanced by Guice?

For more information:
https://code.google.com/p/google-guice/wiki/AOP

google...@googlecode.com

unread,
May 20, 2013, 2:33:51 AM5/20/13
to google-g...@googlegroups.com
Comment by bells...@gmail.com:

hi below is my code:
BillingModule.java
public class BillingModule extends AbstractModule {

@Override
public void configure() {
bind(Calendar.class).to(GregorianCalendar.class);
bindInterceptor(Matchers.any(),
Matchers.annotatedWith(NotOnWeekends.class), new WeekBlocker(
getProvider(Calendar.class)));

bind(TransactionLog.class).toProvider(TransactionLogProvider.class);

bind(CreditCardProcessor.class).to(PaypalCreditCardProcessor.class);

}
}

Main.java:
public class Main {

/**
* @param args
*/
public static void main(String[] args) {
Injector injector = Guice.createInjector(new BillingModule());
BillingService billingService = injector
.getInstance(RealBillingService.class);
BillingService billingService2 = injector
.getInstance(RealBillingService.class);
System.out.println(billingService + "" + billingService2);
billingService.chargeOrder(new PizzaOrder(), new CreditCard());
}

}

but the result is exception:
<font color="#FF0000">
2013-5-20 14:26:06 com.google.inject.internal.MessageProcessor visit
信息: An exception was caught and reported. Message:
java.lang.IllegalStateException: This Provider cannot be used until the
Injector has been created.
java.lang.IllegalStateException: This Provider cannot be used until the
Injector has been created.
at
com.google.inject.internal.util.$Preconditions.checkState(Preconditions.java:142)
at com.google.inject.spi.ProviderLookup$1.get(ProviderLookup.java:87)
at guide.WeekBlocker.<init>(WeekBlocker.java:29)
at guide.BillingModule.configure(BillingModule.java:34)
at com.google.inject.AbstractModule.configure(AbstractModule.java:59)
at
com.google.inject.spi.Elements$RecordingBinder.install(Elements.java:223)
at com.google.inject.spi.Elements.getElements(Elements.java:101)
at
com.google.inject.internal.InjectorShell$Builder.build(InjectorShell.java:133)
at
com.google.inject.internal.InternalInjectorCreator.build(InternalInjectorCreator.java:103)
at com.google.inject.Guice.createInjector(Guice.java:95)
at com.google.inject.Guice.createInjector(Guice.java:72)
at com.google.inject.Guice.createInjector(Guice.java:62)
at guide.Main.main(Main.java:19)
Exception in thread "main" com.google.inject.CreationException: Guice
creation errors:

1) An exception was caught and reported. Message: This Provider cannot be
used until the Injector has been created.
at
com.google.inject.internal.InjectorShell$Builder.build(InjectorShell.java:133)

1 error
at
com.google.inject.internal.Errors.throwCreationExceptionIfErrorsExist(Errors.java:435)
at
com.google.inject.internal.InternalInjectorCreator.initializeStatically(InternalInjectorCreator.java:154)
at
com.google.inject.internal.InternalInjectorCreator.build(InternalInjectorCreator.java:106)
at com.google.inject.Guice.createInjector(Guice.java:95)
at com.google.inject.Guice.createInjector(Guice.java:72)
at com.google.inject.Guice.createInjector(Guice.java:62)
at guide.Main.main(Main.java:19)
Caused by: java.lang.IllegalStateException: This Provider cannot be used
until the Injector has been created.
at
com.google.inject.internal.util.$Preconditions.checkState(Preconditions.java:142)
at com.google.inject.spi.ProviderLookup$1.get(ProviderLookup.java:87)
at guide.WeekBlocker.<init>(WeekBlocker.java:29)
at guide.BillingModule.configure(BillingModule.java:34)
at com.google.inject.AbstractModule.configure(AbstractModule.java:59)
at
com.google.inject.spi.Elements$RecordingBinder.install(Elements.java:223)
at com.google.inject.spi.Elements.getElements(Elements.java:101)
at
com.google.inject.internal.InjectorShell$Builder.build(InjectorShell.java:133)
at
com.google.inject.internal.InternalInjectorCreator.build(InternalInjectorCreator.java:103)
... 4 more

</font>

google...@googlecode.com

unread,
May 20, 2013, 6:21:07 AM5/20/13
to google-g...@googlegroups.com
Comment by mccu...@gmail.com:

@bellszhu you can't call Provider.get() until after the injector is
created. You either need to store the Provider in WeekBlocker and call
get() when method interception occurs (ie. once the injector is up and
running), or use requestInjection with a Calendar field/setter.
Reply all
Reply to author
Forward
0 new messages