Guice

Error when retrying method after OptimisticLockException - org.postgresql.util.PSQLException: This statement has been closed

浪尽此生 提交于 2019-12-11 00:47:33
问题 I have method: public void changeItemName(long id, String nmae) { Item item = itemDAO.find(id); item.setName(name); try { itemDAO.save(item); } catch (RollbackException | OptimisticLockException | StaleStateException e) { logger.warn("Retry method after " + e.getClass().getName()); itemDAO.clear(); changeItemName(id, name); } } First, I manually provoke OptimisticLockException by setting higher version, so it goes to catch block, clears EntityManager and retries the method. When retrying, the

Dependency injection using Guice within Constraint validator

时光总嘲笑我的痴心妄想 提交于 2019-12-11 00:16:25
问题 I have a use case of injecting a class inside an implementation of ConstraintValidator. I'm using Google guice for Dependency injection and am currently unable to inject inside the validator. Simplified form of my scenario Inside Module: @Provides @Singleton public ServiceA getServiceA() { return new ServiceA(); } The constraint validator: public class MyValidator implements ConstraintValidator<ValidS,List<String>> { private final ServiceA serviceA; @Inject public MyValidator(ServiceA

how to create an object from a different scope

自作多情 提交于 2019-12-10 20:27:37
问题 I have an object in Guice with scope singleton. In method f() I want to create a new object but let Guice do the injection. I think it is not a good practice to pass the injector around. So how can I get a new "Guicy" instance of object? 回答1: As suggested above, probably provider is the way to do it. it is also mentioned in this question. 来源: https://stackoverflow.com/questions/9819794/how-to-create-an-object-from-a-different-scope

Google Guice persistence + Spring Data JPA

与世无争的帅哥 提交于 2019-12-10 20:15:57
问题 I want to use Spring Data JPA in my web application build with Google Guice. And I found out that Google Guice provide the Guice Persist and say "JPA (Java Persistence API) - with any compliant vendor implementation.". Google Guice Persist - https://github.com/google/guice/wiki/GuicePersist Is it possible to use Spring Data JPA into Google Guice ? 回答1: I believe guice repository does what you want. It makes Spring Data JPA easy to use with Google Guice. 来源: https://stackoverflow.com/questions

How to get instance for dynamic type/dynamically built ParameterizedType in Guice

我与影子孤独终老i 提交于 2019-12-10 18:55:36
问题 Given an injector, I am wondering how I can retrieve a specific instance of some parameterized type (but I don't have the Type itself). Let me explain myself: Imagine that you have made the following bindings: List<Apple> bound to ArrayList<Apple> Set<Pears> bound to HashSet<Pear> etc... for other Collection of Fruit . Now I have a Fruit fruit instance and I would like to retrieve the appropriate Collection instance. How can I achieve this? Here is a small working snippet that illustrates

Guice nullpointer exception on injected instance

六眼飞鱼酱① 提交于 2019-12-10 18:46:51
问题 I'm using Guice in Jersey2 for DI (i want to use it so i can use Google App Engine -> not working with HK2). My ApplicationResource: public class ApplicationResource extends ResourceConfig { private static final Logger LOGGER = null; public ApplicationResource() { System.out.println("Application startup"); // Register resources and providers using package-scanning. packages("com.crawler.c_api"); // Register my custom provider - not needed if it's in my.package. register(ResponseCorsFilter

How can I get all singleton instances from a Guice Injector?

ぃ、小莉子 提交于 2019-12-10 18:34:05
问题 Is there an easy way to enumerate all the singleton instances already created by a Guice Injector? Or alternately a way to get all singletons that implement a specific interface? I would like to find all singleton instances that implement java.io.Closeable so I can close them cleanly when my service is shut down. 回答1: This would be fairly easy to write using Guice's SPI. Guice's Injector instance exposes a getAllBindings() method that lets you iterate through all of the bindings. // Untested

How to get HK2 ServiceLocator in a Jersey2 ServletContainer?

人走茶凉 提交于 2019-12-10 16:46:46
问题 I'd like to get Jersey2 and Guice to cooperate together, which is apparently rather difficult. I've seen some solution to this involving using the HK2-to-Guice bridge. But the bridge rely on getting the HK2 ServiceLocator instance in the init() of a custom Jersey2 ServletContainer in order to initialize GuiceBrige : public class MyServletContainer extends ServletContainer { @Override public void init() { ServiceLocator sloc = getApplicationHandler().getServiceLocator(); ... } } But somehow in

Cucumber with Guice - multiple guice injector

末鹿安然 提交于 2019-12-10 15:56:57
问题 I'm using Cucumber with Guice as DI. I've encountered following problem: I've got one step i.e. class MyStep() { @Inject private MyService myService; @Given("Some acction happen") public void sthHappen() { myService.doSth(); } } And I've got this class to run it as JUnit test @RunWith(Cucumber.class) @CucumberOptions(...) public class MyTest { } There is a class MyModule extends AbstractModule { @Override protected void configure() { bind(MyService.class).to(MyFirstService.class); } } which

How to improve Guice performance at startup

不羁的心 提交于 2019-12-10 15:32:35
问题 Ok, I know my computations are not objective and so on, but anyway, I hate to wait so much time when performing my unit-tests: My guice swing application takes about 7 seconds to initialize. It's a simple IRC client. At that moment, no connection are open, I even haven't called any java.io or java.net classes yet. I've tried to narrow down what exactly is wrong and I get that 5.8 seconds (average) are used by Guice in order to create the injector with the 2 modules I'm using (one normal