Guice

Guice FactoryModuleBuilder an instance with constructor parameters

半腔热情 提交于 2019-12-12 03:24:58
问题 I´m using Guice to initalize a class with some arguments from a config file @Provides @Singleton RetryServiceCaller provideMaxRetryAttempts(@Named("config") JsonObject config) throws IOException { JsonObject retryDetails = config.getJsonObject("retry_details"); return new RetryServiceCaller(retryDetails.getInteger("maxRetryAttempts"), retryDetails.getInteger("upperBoundary"), retryDetails.getInteger("lowerBoundary"), retryDetails.getLong("multiplicationFactor"), retryDetails.getInteger(

Guice change binding by property (string in external text file)/ on runtime

女生的网名这么多〃 提交于 2019-12-12 03:14:36
问题 how to change/set a binding by a property/string given in a property/text file? in my case i want to implement a kind of "demo mode". In normal mode a property gives an url to an external service but if url is "demo" the binding of the according interface should be changed like this: normal: bind(SasDatenProvider.class).to(SasDataProviderHttpImpl.class); demo bind(SasDataProvider.class).to(SasDataProviderFileImpl.class); how to achieve that? thx in advance 回答1: You can use a method annotated

Guice - bind different instances based on enclosing class

天涯浪子 提交于 2019-12-12 02:43:28
问题 Is it possible to bind Named instance based on the enclosing class? For ex: in the below sample, classes A and B will get DataStore instance injected. However, I need to have primary store as StoreA and secondary store as StoreB in the scope/context of class A but primary store as StoreC and secondary store as StoreD in the scope/context of class B. How can this be achieved? class A { @Inject public A(DataStore dataStore) { ... } } class B { @Inject public B(DataStore dataStore) { ... } }

Dynamically bind instances using guice

落爺英雄遲暮 提交于 2019-12-12 01:19:10
问题 In my application (stand alone apache camel) i have to bind several beans (instances of pojos). Because those pojos could not be used directly (in java) but have to be used via bound references in urls i want to "register" all available beans in an enum. The beans are then bound like this: public class BeanRegistry extends JndiRegistry { public BeanRegistry() { for (Beans bean : Beans.values()) { try { this.bind(bean.name(), bean.clazz().newInstance()); } catch (InstantiationException |

Jersey + Guice + Tomcat producing 404 when served with something other than root directory

断了今生、忘了曾经 提交于 2019-12-11 23:32:28
问题 I have a resource that looks like this: @Path("/Resources/Console") public class ConsoleResource { @POST @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) public String post(/* */) { /* */ } } Whenever my JerseyServletModule is configured as follows, the services work: @Override protected void configureServlets() { bind(ConsoleResource.class); bind(MessageBodyReader.class).to(JacksonJsonProvider.class);

Guice inject annotation value

拥有回忆 提交于 2019-12-11 17:53:34
问题 Hello i want to inject annotation value to parameter. for example @BindingAnnotation @Target({ ElementType.FIELD, ElementType.PARAMETER}) @Retention(RetentionPolicy.RUNTIME) public @interface MyAnnotation { int value() default 0; } public class A { @Inject @MyAnnotation(30) protected Integer a; } how i can inject 30 inside the a variable. thank you very much 回答1: Use bindConstant() as bindConstant().annotatedWith(MyAnnotation.class).to(30); You can just have @Inject and @MyAnnotation

How do I bind a specific parameter to an instance of a custom annotation?

喜你入骨 提交于 2019-12-11 15:19:32
问题 How do I make the following work using Guice? // The Guice Module configuration void configure() { // The following won't compile because HelpTopicId is abstract. // What do I do instead? bind(new TypeLiteral<String>(){}). annotatedWith(new HelpTopicId("A")).toInstance("1"); bind(new TypeLiteral<String>(){}). annotatedWith(new HelpTopicId("B")).toInstance("2"); } public @interface HelpTopicId { public String helpTopicName(); } public class Foo { public Foo(@HelpTopicId("A") String helpTopicId

GWT Guice/Gin on the server side problem

↘锁芯ラ 提交于 2019-12-11 14:25:18
问题 Hi guys with my question. A GWT project, as i have read Gin i usable only on the client side than Guice is usable on the server side. Here is my question. Let first post some example code. Server side. public class WebchargeServiceImpl extends RemoteServiceServlet implements WebchargeService { @Inject private Injector injector; @Inject private ExecuteOperations executeOperations; ..... executeOperations.do(); .... Here is the injected class ExecuteOperations @Singleton public class

Multiple instance of the same provider

夙愿已清 提交于 2019-12-11 14:15:06
问题 I created an DataSourceProvider to look up container managed dataSource: public class ContainerDataSourceProvider implements Provider<DataSource> { private final DataSource ds; @Inject ContainerDataSourceProvider (String dataSourceName) throws NamingException { Context initCtx = new InitialContext(); Context envCtx = (Context) initCtx.lookup("java:comp/env"); ds = (DataSource) envCtx.lookup("jdbc/" + dataSourceName); } @Override public DataSource get() { return ds; } } I can't used @Named

Apache Wicket: Injecting dependencies in form validators (using Guice)

拜拜、爱过 提交于 2019-12-11 13:58:48
问题 (This is basically a follow on this question.) I need to access the DB service layer in one of my form validators (to make sure the email is not already taken when registering a new user). I tried the following (some input-fields omitted for brevity): public class RegistrationPage extends WebPage { @Inject private UserService userService; public RegistrationPage() { add(new FeedbackPanel("feedback")); TextField<String> email = new TextField<String>("email", Model.of("")); ... email.add(new