Guice

Post creation initialization of guice singleton [duplicate]

匿名 (未验证) 提交于 2019-12-03 09:14:57
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: This question already has an answer here: Guice call init method after instantinating an object 5 answers Configure an object provided by a Guice Module 2 answers Is there a way to have guice call a init() method after it has instantiated a singleton? Calling init() inside the constructor is not an option since init() could be overriden by a subclass. 回答1: You can use `@PostConstruct' in guice when you use the mycila/jsr250 extension . This will cause your init() method to be called right after instantiation. @PostConstruct void init() { //

Guice:Binding Annotations with Attributes

匿名 (未验证) 提交于 2019-12-03 09:05:37
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Now I am reading Guice's official document, but I have some questions related to the chapter of Binding Annotation. This explains "Annotation with Attributes". But, I'm not sure of the explanation. Binding Annotations with Attributes Guice supports binding annotations that have attribute values. In the rare case that you need such an annotation: Create the annotation @interface. Create a class that implements the annotation interface. Follow the guidelines for equals() and hashCode() specified in the Annotation Javadoc. Pass an instance of

Using Google Guice to inject java properties

爱⌒轻易说出口 提交于 2019-12-03 08:53:54
问题 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

Guice injectMembers method

匿名 (未验证) 提交于 2019-12-03 08:44:33
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: 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()?? 回答1: Why do you need to inject

Guice proxying to support circular dependency

匿名 (未验证) 提交于 2019-12-03 08:41:19
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I'm getting the following error in my code at launch: Tried proxying com.bar.Foo to support a circular dependency, but it is not an interface. How exactly does this proxying work? If I just throw enough classes behind interfaces, will everything be fine? (I know that circular dependencies are usually a code smell, but I think in this case it's ok.) 回答1: While the "inject an interface" approach is totally valid, and might even be the better solution in some occasions, in general, you can use a simpler solution: Providers. For every class "A"

Guice and general application configuration

佐手、 提交于 2019-12-03 08:35:34
问题 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

Guice runtime dependency parameters reinjection

只谈情不闲聊 提交于 2019-12-03 08:14:58
A question about Guice. I'm still learning it, but I can understand the fundamentals. This question was already asked a couple of times on the net, but never with a concrete answer(none that I could find). Say I have a situation like on the picture(a similar example was somewere on the net). public class Dog {} public class Walk implements Walkable { private final Dog dog; private final boolean leash; @Inject public Walk(Dog dog, @Assisted boolean leash) { this.dog = dog; this.leash = leash; } public void go() { } } public interface Walkable { void go(); } public interface WalkFactory { Walk

Guice SPI: find bindings by wildcard types

匿名 (未验证) 提交于 2019-12-03 07:50:05
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: Guice provides a means to find all bindings for a given type ( Injector#findBindingsByType ) and it also provides a TypeLiteral class from which it seems possible to construct a wildcard type. What I would like to do is find all bindings for some type that is parameterised by a wildcard type but I can't figure out how to do it. A look at the guice src suggests I might be barking up the wrong tree but I figured I'd ask around anyway... so for example given a type Foo < E extends Bar > BarImplOne implements Bar BarImplTwo implements

Apache jclouds java.lang.NoSuchMethodError when using Rackspace in a Spring Boot application

匿名 (未验证) 提交于 2019-12-03 07:50:05
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: I'm trying to integrate Apache Jclouds into a Spring Boot application I'm working on so that I can upload files to Rackspace Cloud Files(UK). I've created a class which I'm creating as a Bean; import com.google.common.io.ByteSource; import com.google.common.io.Files; import org.jclouds.ContextBuilder; import org.jclouds.io.Payload; import org.jclouds.io.Payloads; import org.jclouds.openstack.swift.v1.features.ObjectApi; import org.jclouds.rackspace.cloudfiles.v1.CloudFilesApi; import org.springframework.web.multipart.MultipartFile; import

How to bind String to variable in Guice?

别来无恙 提交于 2019-12-03 07:48:30
问题 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? 回答1: You first need to annotate the constructor for SomeClass : class SomeClass { @Inject SomeClass(@Named(