Guice

Guice and general application configuration

丶灬走出姿态 提交于 2019-12-03 00:09:25
For a monitoring software written in Java I consider using Google Guice as DI provider. The project needs to load its configuration from an external resource (file or database). The application is designed to run in standalone mode or in a servlet container. At the moment the configuration does not contain bindings or parameters for dependency injection, only some global application settings (JDBC connection definitions and associated database management/monitoring objects). I see two options: to use another library, for example Apache Commons Configuration , which supports file and JDBC

史上最好用的依赖注入框架Google Guice【转】

匿名 (未验证) 提交于 2019-12-02 23:49:02
Guice是Google开发的一个轻量级,基于Java5(主要运用 泛型 与注释特性)的依赖注入框架(IOC)。Guice非常小而且快。 (其他的依赖注入框架还有Dagger,Spring) Spring框架的依赖注入是家喻户晓的,但是在实际的开发中我们想使用便捷的依赖注入功能,但是又不想引入Spring框架的复杂性,该怎么办呢? 有了Google Guice,这个问题便简单了,首先在你的maven项目里引入 <dependency> <groupId>com.google.inject</groupId> <artifactId>guice</artifactId> <version>4.0</version> </dependency>    我们使用Guice创建了一个注射器Injector,然后从Injector拿到你想要的对象就可以了,Guice会自动装配依赖树。Guice的启动速度是很快的,在一个大型应用中,Guice装配所有的模块决不会超过1s。Guice是一个非常干净的依赖注入框架,框架除了依赖注入功能之外,没有任何其它非相关模块功能。 Guice里最常用的两个注解就是@Singleton和@Inject,Singleton表示构建的对象是单例的,Inject表示被标注的字段将使用Guice自动注入。在一般的项目中这两个注解一般可以完成90%以上的装配工作。

Guice: How to change injection on runtime based on a (dynamic web property)

一笑奈何 提交于 2019-12-02 22:22:52
The following is an approximation of the problem I'm facing. Think we have a password validator with some rules. public interface RuleChecker{ //Checks for a password strenght, returns 10 //for strong or 0 for soft password. int check(String pass); } And then we have several implementations, our service will only accept the password if it is over 8 score. public class NoCheck implements RuleChecker { public int check(String pass){return 10;} } public class LengthCheck implements RuleChecker{ ... } public class AlphanumericCheck implements RuleChecker{ ... } public class

How to @Inject into existing object hierarchy using Guice?

£可爱£侵袭症+ 提交于 2019-12-02 21:02:23
I have an existing object hierarchy where some objects have fields that need to be injected. Also there are some other objects that are constructed using Google Guice and need to be injected with references to some objects from previously described object hierarchy. How do I do such kind of injection with Guice? The problem is that objects from existing hierarchy were not constructed using Guice, and therefore are not subject to inject process by default. There is, of course injector.injectMembers() method that is able to inject into existing object instance, but it does not work on object

How to bind String to variable in Guice?

跟風遠走 提交于 2019-12-02 20:31:46
I'm new to Guice and here is a naive question. I learned that we could bind String to a particular value through: bind(String.class) .annotatedWith(Names.named("JDBC URL")) .toInstance("jdbc:mysql://localhost/pizza"); But what if I want to bind String to any possible characters? Or I think it could be described this way: How can I replace "new SomeClass(String strParameter)" with Guice? NamshubWriter You first need to annotate the constructor for SomeClass : class SomeClass { @Inject SomeClass(@Named("JDBC URL") String jdbcUrl) { this.jdbcUrl = jdbcUrl; } } I prefer to use custom annotations,

Guice Beginner - How to bind concrete classes?

倾然丶 夕夏残阳落幕 提交于 2019-12-02 20:02:37
I have this class: public class House { private final Door door; private final Window window; private final Roof roof; @Inject public House(Door door, Window window, Roof roof) { this.door = door; this.window = window; this.roof = roof; } } Where Door , Window and Roof are concrete classes. Now if I want to implement a Module for this scenario, I would do it like this: public class HouseModule extends AbstractModule { @Override protected void configure() { bind(Door.class).to(Door.class); bind(Window.class).to(Window.class); bind(Roof.class).to(Roof.class); } } But I wonder if this is the

How to hook Jackson ObjectMapper with Guice / Jersey

五迷三道 提交于 2019-12-02 18:30:28
I can't seem to get my Jackson ObjectMapper Module registered correctly. I'm using a Guice + Jersey + Jackson (FasterXML) stack. I've followed how to customise the ObjectMapper based on various question here. In particular, I have a ContextResolver declared, marked as an @javax.ws.rs.ext.Provider and a @javax.inject.Singleton. I have a GuiceServletContextListener along the lines of: @Override protected Injector getInjector() { Injector injector = Guice.createInjector(new DBModule(dataSource), new ServletModule() { @Override protected void configureServlets() { // Mapper bind(JacksonOMP.class)

Has anyone used ServiceLoader together with Guice?

冷暖自知 提交于 2019-12-02 17:18:14
I keep wanting to try this on a larger scale with our app + build system, but higher priorities keep pushing it to the back burner. It seems like a nice way to load Guice modules and avoids the common complaint about "hard coded configuration". Individual configuration properties rarely change on their own, but you will almost always have a set of profiles, usually for different environments (Debug, Production, etc). ServiceLoader lets you pull a list of all implementations defined as a service for a given type. Putting this together with Guice, you end up with: import java.util.ServiceLoader;

Injecting Collection of Classes with Guice

好久不见. 提交于 2019-12-02 17:05:27
I'm trying to inject things with Google Guice 2.0 and I have the following structure: FooAction implements Action BarAction implements Action I then have an ActionLibrary with the following constructor: ActionLibrary (List<Action> theActions) When I request an instance of ActionLibrary from Guice, I would like Guice to identify both of the registered Action classes (FooAction, BarAction) and pass them into the constructor. The motivation here being that when I add a third action BazAction, it would be as simple as registering it in the Module and it would automatically be added to the list in

How does guice's TypeLiteral work?

╄→гoц情女王★ 提交于 2019-12-02 16:17:44
How does Guice's TypeLiteral overcome the Java generic types erasure procedure? It works wonders but how is this accomplished? Moritz The trick that is used here is that the signatures of generic super types are stored in subclasses and thus survive erasure. If you create an anonymous subclass new TypeLiteral<List<String>>() {} Guice can call getClass().getGenericSuperclass() on it and get a java.lang.reflect.ParameterizedType on which exists a method getActualTypeArguments() to get List<String> as an instance of ParameterizedType . By a combination of anonymous types, subclassing and the fact