Guice

Guice , afterPropertiesSet

匿名 (未验证) 提交于 2019-12-03 02:23:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Someone knows some way that how can I achieve the same functionality in Guice as the ' afterPropertiesSet ' interface in spring ? ( its a post construction hook ) 回答1: By far the simplest solution, if you're using constructor injection and not doing anything too crazy, is to create a post-construction method and annotate it with @Inject : final class FooImpl implements Foo { private final Bar bar; @Inject FooImpl(Bar bar) { this.bar = bar; ... } @Inject void init() { // Post-construction code goes here! } } When Guice provides FooImpl, it'll

ClassNotFoundException with Guice 2.0

匿名 (未验证) 提交于 2019-12-03 01:48:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: The code below generates an error using Guice 2.0. With Guice 1.0 everything is fine. The JDK is Java 6 update 15. public class App { public static void main(String[] args) { Guice.createInjector(new AbstractModule() { @Override protected void configure() { // just testing } }); } } The error is: Exception in thread "main" java.lang.NoClassDefFoundError: [Lorg/aopalliance/intercept/MethodInterceptor; at java.lang.Class.getDeclaredMethods0(Native Method) at java.lang.Class.privateGetDeclaredMethods(Class.java:2427) at java.lang.Class

Injecting a generic factory in Guice

匿名 (未验证) 提交于 2019-12-03 01:25:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: The following code is an example of a factory that produces a Bar given a Foo . The factory doesn't care what T is: for any type T , it can make a Bar from a Foo . import com.google.inject.*; import com.google.inject.assistedinject.*; class Foo { public void flip(T x) { System.out.println("flip: " + x); } } interface Bar { void flipflop(T x); } class BarImpl implements Bar { Foo foo; @Inject BarImpl(Foo foo) { this.foo = foo; } public void flipflop(T x) { foo.flip(x); System.out.println("flop: " + x); } } interface BarFactory { Bar create

Generic @Inject'd fields in an abstract superclass

冷暖自知 提交于 2019-12-03 01:22:32
Consider a MVP-ish set of types. An abstract Presenter exists, with a View interface: public interface View { //... } public abstract class AbstractPresenter<V extends View> { @Inject V view; //... } Then, lets have a specific concrete presenter subclass, with its view interface and implementation: public interface LoginView extends View { //... } public LoginPresenter extends AbstractPresenter<LoginView> { //... } public class LoginViewImpl implements LoginView { //... } In a Dagger module, of course we would define a @Provides method: @Provides LoginView provideLoginView() { return new

How to retrieve annotated instance from Guice's injector?

和自甴很熟 提交于 2019-12-03 01:00:21
问题 Let's say I have a module: Module extends AbstractModule { @Override protected void configure() { bind(String.class). annotatedWith(Names.named("annotation")). toInstance("DELIRIOUS"); } } and I want to test the module and check if it injects the right value in a String field annotated with Names.named("annotation") without having a class and a field but obtaining the value directly from the injector: @Test public void test() { Injector injector = Guice.createInjector(new Module()); // THIS

Guice 3.0 - ArrayIndexOutOfBoundsException on startup?

匿名 (未验证) 提交于 2019-12-03 00:59:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Why Guice 3.0 throws this exception instead of a formatted message for wrong configurated components (@Inject is missing for example)? Exception in thread "main" com.google.inject.internal.util.$ComputationException: java.lang.ArrayIndexOutOfBoundsException: 16640 at com.google.inject.internal.util.$MapMaker$StrategyImpl.compute(MapMaker.java:553) at com.google.inject.internal.util.$MapMaker$StrategyImpl.compute(MapMaker.java:419) at com.google.inject.internal.util.$CustomConcurrentHashMap$ComputingImpl.get(CustomConcurrentHashMap.java:2041)

Guice: “Could not find suitable constructor” when suitable constructor supplied in FactoryBuilder

匿名 (未验证) 提交于 2019-12-03 00:59:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I'm using Guice for dependency injection, but I'm getting this error: 1) Could not find a suitable constructor in java.lang.Void. Classes must have either one (and only one) constructor annotated with @Inject or a zero-argument constructor that is not private. at java.lang.Void.class(Void.java:44) while locating java.lang.Void at com.myfeed.algorithm.clusterer.tree.wrapper.ItemSourceTreeWrapperFactory.create(ItemSourceTreeWrapperFactory.java:1) What's especially odd is that it mentions java.lang.Void. This is the module code : (I bind a

How to improve Guice performance at startup

匿名 (未验证) 提交于 2019-12-03 00:53:01
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: 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 module and one built with FactoryModuleBuilder ,

Method interception in Jersey using Guice AOP

北城余情 提交于 2019-12-03 00:41:22
Is it possible to use Guice AOP to intercept an annotated method on a Jersey resource? I have a successfully configured Guice integration working with Jersey with respect to Dependency Injection without any problems, however my configured Interceptor is not intercepting my annotated method at all. web.xml <listener> <listener-class>my.package.GuiceConfig</listener-class> </listener> <filter> <filter-name>guiceFilter</filter-name> <filter-class>com.google.inject.servlet.GuiceFilter</filter-class> </filter> <filter-mapping> <filter-name>guiceFilter</filter-name> <url-pattern>/*</url-pattern> <

Using Google Guice to inject java properties

时光毁灭记忆、已成空白 提交于 2019-12-03 00:27:25
I want to use google guice to make properties available in all classes of my application. I defined a Module which loads and binds the properties file Test.properties . Property1=TEST Property2=25 package com.test; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.Properties; import com.google.inject.AbstractModule; import com.google.inject.name.Names; public class TestConfiguration extends AbstractModule { @Override protected void configure() { Properties properties = new Properties(); try { properties.load(new FileReader("Test