Guice

Releasing ORMLite helper on @Singleton

守給你的承諾、 提交于 2019-12-05 13:22:43
I have a @Singleton class where I've injected an instance of OrmLiteSqliteOpenHelper . Do I actually ever need to call the OpenHelperManager.releaseHelper() ? In case I do, where and how should it be done as the class doesn't extend any Android base class where I could get to the onDestroy ? There is an ORMLite example Android project which demonstrates this called HelloAndroidNoBase . I'd check it out. The relevant code section from the main Activity is included below. You'll need to have this sort of code in each one of your Activity or other classes that uses the database. If your class

JBoss AS7 Automatically Loading JPA

[亡魂溺海] 提交于 2019-12-05 10:23:14
I have an application which uses JPA/Hibernate and Google Guice. Guice is bootstrapped in a ServletContextListener and it sets up the EntityManagerFactory itself. The application works fine on Tomcat 7, but when I deploy to JBoss AS7 it fails because JBoss decides to automatically setup JPA prior to invoking my ServletContextListener . How can I get JBoss to not initialize JPA automatically and instead wait for my ServletContextListener to do it? Update According to the link that James provided below: During application deployment, JPA use is detected (e.g. persistence.xml or

URI-specific ExceptionMapper in JAX-RS

偶尔善良 提交于 2019-12-05 09:37:24
I'm using Jersey, and Guice as my IOC-container. I'd like to know if it is possible to associate an ExceptionMapper with a specific URI. The reason for this is that I want to map the same exception differently based on what URI was visited. For example, suppose I've got the following two exception mappers for my custom exception: public class MyExceptionMapperForFirstURI implements ExceptionMapper<MyException> {..return response based on first URI..} public class MyExceptionMapperForSecondURI implements ExceptionMapper<MyException> {..return response based on second URI..} As far as I

How to override binding in GIN

廉价感情. 提交于 2019-12-05 07:44:00
I find the answer for Guice Overriding Binding in Guice but don't know how to do the same for GIN in GWT. Thanks in advance! As far as I know, it's not supported. To answer your comment: If you're running "pure" JUnit tests (not GWTTestcases) you don't use GIN, you use Guice, and in Guice you can override modules. If you want to reuse GIN modules, then wrap them using GinModuleAdapter . So you can do something like this: static class MyGinModule extends GinModule { ... } static class MyGuiceModule extends AbstractModule { ... } // And somewhere in your code, here's how you could create the

Constructor Injection using Guice

此生再无相见时 提交于 2019-12-05 06:10:25
I have some sample code which is using factories. I'd like to clean up the code by removing the factories and use Guice instead. I attempted to do this but I hit a small roadblock. I am really new to Guice, so I am hoping someone can help me out here. Existing client code (Using factories): public class MailClient { public static void main(String[] args) { MailConfig config = MailConfigFactory.get(); config.setHost("smtp.gmail.com"); Mail mail = MailFactory.get(config); mail.send(); } } My attempt to refactor using Guice: //Replaces existing factories public class MailModule extends

How to do manual DI with deep object graphs and many dependencies properly

血红的双手。 提交于 2019-12-05 05:46:38
I believe this questions has been asked in some or the other way but i'm not getting it yet. We do a GWT project and my project leader disallowed to use GIN/Guice as an DI framework (new programmers are not going to understand it, he argued) so I try to do the DI manually. Now I have a problem with deep object graphs. The object hierarchy from the UI looks like this: AppPresenter->DashboardPresenter->GadgetPresenter->GadgetConfigPresenter The GadgetConfigPresenter way down the object hierarchy tree has a few dependencies like CustomerRepository, ProjectRepository, MandatorRepository, etc. So

Guice injectMembers method

会有一股神秘感。 提交于 2019-12-05 05:26:22
I understand the benefits of using constructor injection over setter injection but in some cases I have to stick with setter-based injection only. My question is how to inject members of all the setter-based injection classes using injector.injectMembers() method? //I am calling this method in init method of my application private static final Injector injector = Guice.createInjector(new A(), new B()); //Injecting dependencies using setters of all classes bound in modules A and B injector.injectAllMembers()?? Why do you need to inject dependencies manually? Guice injects dependencies into the

How do I get google guice to inject a custom logger, say a commons-logging or log4j logger

时间秒杀一切 提交于 2019-12-05 04:53:45
Google guice has a built-in logger binding . But what if I want to use a commons-logging or log4j logger? Can I get guice to inject a Log created by LogFactory.getLog(CLASS.class) But having the same behavior as in built-in binding: The binding automatically sets the logger's name to the name of the class into which the Logger is being injected.. Does it even makes sense? Or shout I simply use the built-in java Logger? Or just use commons-logging without injections? Jesse Wilson The CustomInjections page on the Guice wiki describes exactly how to inject a logger named by the class it's being

spring与guice的区别

流过昼夜 提交于 2019-12-05 03:53:37
斧子的例子 借斧子的例子说一说 spring 与guice的区别。 看下边的例子:对于不同社会形态下一个人( java 对象,调用者)需要一把斧子(java对象,被调用者)。 原始社会时 劳动社会基本没有分工,需要斧子的人(调用者)只好自己去磨一把斧子,每个人拥有自己的斧子,如果把大家的石斧改为铁斧,需要每个人都要学会磨铁斧的本领,工作效率极低。对应Java里的情形是:java程序里的调用者new一个被调用者的实例。类 耦合度 极高,修改维护烦琐,效率极低。 工业社会时 工厂出现,斧子不再由普通人完成,而由工厂生产,当人们需要斧子的时候,可以到工厂购买斧子,无需关心斧子是怎么制造出来的,如果废弃铁斧为钢斧,只需改变工厂的制造工艺即可,制作工艺是工厂决定的,工厂生产什么斧子,工人们就得用什么斧子。对应的java里的情形是: Java 程序的调用者可以以来 简单工厂 创建被调用者,变化点被隔离到了简单工厂里,虽然 耦合度 降低,但是调用者会和工厂 耦合 ,而且需要定位自己的工厂 近代工业社会 工厂蓬勃发展,人们需要什么斧子,只需要提供一个斧子图形,商家会按照你提供的图形将你的斧子订做好,送上门。对应 Java 里的情形: spring 的依赖注入 按需要分配社会 信息进入现代化,人们不再去工厂购买斧子,不再拘泥于需要什么斧子事先画好什么样的图形,只需要打个电话

Why do I have an error when I try to override a generic binding with Guice? (TypeLiteral)

你离开我真会死。 提交于 2019-12-05 03:39:36
I'd like to override a generic type binding, but I allways got the same " No implementation was bound " error. I'm using roboguice 3. Here is a exemple of code that I use: public interface IParser<I, O> {} public class Parser1 implements IParser<String, String> { IParser<String, String> mParser; @Inject public Parser1(IParser<String, String> parser) { mParser = parser; } } public class Parser2 extends Parser1 { @Inject public Parser2(IParser<String, String> parser) { super(parser); } } public class MyModule extends AbstractModule { @Override protected void configure() { bind(new TypeLiteral