Domanda

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

È stato utile?

Soluzione

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?!

Altri suggerimenti

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.

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();
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top