0

I have some experience with guice and i just tried guice-persist. But now i get a very strange error in my very simple module. This is my module:

public class VotingModule extends AbstractModule {

    @Override
    protected void configure() {
        bind(VotingService.class).to(VotingServiceImpl.class);
    }

}

I created a factory (this is for using this api, there is no main) to get an instance of the service:

    public static VotingService getService(final String persistenceUnit) {
    // initialization of dependency injection
    Injector i = Guice.createInjector(new JpaPersistModule(persistenceUnit), new VotingModule());
    // Starts persistence stuff (jpa is ready now)
    i.getInstance(PersistService.class).start();
    return i.getInstance(VotingService.class);
}

The VotingService and its implementation encapsulate simple data-base interactions. For this "VotingServiceImpl" only injects an EntityManager and uses @Transactionl on some methods. So why i get

    1) Unable to method intercept: com.prodyna.nabucco.groupware.voting.core.service.impl.VotingServiceImpl
  at com.prodyna.nabucco.groupware.voting.core.service.impl.VotingModule.configure(VotingModule.java:10)

? The error is thrown on this simple test:

   @Test
    public void test(){
        VotingService vs = VotingServiceFactory.getService();
    }

Edit This error only occurs if bound implementation uses @Transactional. So something went wrong with aop stuff but how to fix it? Edit

dermoritz
  • 10,454
  • 16
  • 80
  • 151

3 Answers3

1

Ok i found the problem after some hours of debugging: The problem was a private constructor in interface' implementation. For vanilla guice private constructors are fine (imho good practice - you can't use "new"). but AOP (Interceptors) doesn't work with private constructors.

I think there should be some hints in doc about that?!

dermoritz
  • 10,454
  • 16
  • 80
  • 151
  • You're right that this doesn't seem to be mentioned in the docs. It makes some sense though, Guice generates a subclass of your class, so it has to be able to call the superclass constructor. You won't have any problems if your constructors are package-private, and that has the advantage of being unit testable. – Tavian Barnes Feb 12 '14 at 16:06
  • i am using guice (jukito) in unit tests. so i don't need constructors there. but thx for hint to package private. – dermoritz Feb 14 '14 at 09:10
  • For future reference, I just hit this problem, and it was caused by the class Guice was trying to intercept being declared final. – Michael Louis Thaler Oct 06 '17 at 16:59
1

Upgrading Guice from 4.0 to 4.2.1 solved the same reported error for me. Note that it only manifested in the first place when upgrading from JDK 1.8 to 1.10.

Daniel Gerson
  • 2,009
  • 1
  • 17
  • 27
0

In my case this error was caused by a static member initialized with the use of a constructor.

private static final CacheControl CACHE_CONTROL = new CacheControl();

Changed it to a non-static member.

private final CacheControl cacheControl = new CacheControl();
simenflatby
  • 21
  • 1
  • 3