Tag: guice

Guice:使用属性绑定注释

现在我正在阅读Guice的官方文档,但我有一些与Binding Annotation章节相关的问题。 这解释了“带属性的注释”。 但是,我不确定解释。 绑定注释与属性 Guice支持绑定具有属性值的注释。 在极少数情况下,您需要这样的注释: 创建注释@interface。 创建一个实现注释接口的类。 遵循Annotation Javadoc中指定的equals()和hashCode()指南。 将此实例传递给annotatedWith()绑定子句。 我不明白这个解释。 有什么解释意图? 我学习了两个注释,例如@Paypal(在本文档中)和@name。 但是,如果我想在同一个类中使用两个以上的依赖项,那么我们只能用这两个注释来实现? 现在我很困惑,有人可以解释一下吗?

相当于MyBatis Guice中的MyBatis XML多环境

我正在编写一个需要根据上下文使用不同数据库的服务(一个简单的字符串标签)。 每个数据库都具有完全相同的模式。 数据库列表是动态的。 查看有关多个数据源的MyBatis-Guice文档 ,该示例是预先知道数据源列表的位置,每个数据源都有不同的映射器。 同样, 这里在SO上发现的问题也有相同的要求。 如上所述,我的要求更加动态和流畅。 我们的想法是将所有当前已知的数据库(及其连接信息)放在配置中,并在服务启动时进行解析。 然后,根据任何传入请求的上下文,代码应该为正确的数据库提取SqlSessionFactory。 使用该SqlSessionFactory的所有下游代码完全相同 – 即不依赖于请求上下文。 这意味着无论使用何种数据库,都使用相同的映射器。 我的MyBatis和Guice知识无疑是新的和有限的。 但是,我无法谷歌任何显示MyBatis-Guice等同于MyBatis 的XML配置支持的多环境方法的内容。

使用Guice进行注册表优化

嗨! 今天我想到了一种优化,并有一些问题…… 上下文:我正在使用Guice 2开发Java。 在我的Web应用程序中,我有一个转换器注册表,可以动态转换为某种类型。 转换器描述如下: public class StringToBoolean implements Converter { @Override public Boolean convert(String from) { return Boolean.valueOf(from); } } 我的注册表是地图: public class ConverterRegistryImpl implements ConverterRegistry { private Map<Key,Converter> converterRegistry = new HashMap<Key, Converter>(); @Override public void register(Class fromType, Class toType, Converter converter) { converterRegistry.put(new Key(fromType,toType), converter); } } 最后,我在我的注册表中注册了我的转换器(类:ServletModule:configureServlets()) 我认为这一步可以优化…… ConverterRegistryImpl registry […]

Guice:注入一个字符串的ArrayList

我试图在Guice的帮助下注入String的ArrayList 。 我想显示一个带有许多RadioButtons的面板(例如),用户可以选择一些要激活的服务。 选择后,我想获取所选服务的所有名称并将其添加到列表中,并将此列表注入负责创建服务的经理。 这是一个例子: public class UIProviderModule extends ProviderServiceModule { private ArrayList requestedServices; public UIProviderModule(ArrayList requestedServices) { this.requestedServices = requestedServices; } @Override protected void configure() { bindConstant().annotatedWith(Names.named(Constants.REQ_SERVICES)).to(requestedServices); bind(IParser.class).to(UIParser.class); super.configure(); } } 我看过很多关于Multibindings和提供者的post,但我不明白这对我有什么帮助。 我只想检索名称,因为我不使用必须绑定到接口的类。 我错过了什么吗? 注意:我知道这可能不是使用Guice的好方法,因为我将列表绑定到Module 。

使用guice作为注入类的框架,正确的初始化方法?

我正在尝试编写一个框架,其中任意bean类都使用我的API注入类,并且它们可以与这两个类交互,并且基于定义的注释触发了回调。 这是一个示例bean: @Experiment static class TestExperiment { private final HITWorker worker; private final ExperimentLog log; private final ExperimentController controller; @Inject public TestExperiment( HITWorker worker, ExperimentLog expLog, ExperimentController controller ) { this.worker = worker; this.expLog = expLog; this.controller = controller; } @SomeCallback void callMeBack() { … do something log.print(“I did something”); } } 我正在尝试使用Guice来注入这些bean并处理注入的类之间的相互依赖性。 但是,我有两个问题: 我传入的其中一个类( […]

泽西+吉塞斯不能将非jersey资源与jersey资源混在一起

如何使用非jersey资源和jersey资源搭配guice? 我希望“/”由普通的servlet处理。 但我希望“/ users”由泽西队处理。 假设我有@Path(“/ users”)的泽西资源。 使用以下绑定将无法正常工作,它会尝试使用泽西映射“/”请求,当然这不是泽西资源,我得到404。 protected void configureServlets() { serve(“/”).with(LoginServlet.class); serve(“/*”).with(GuiceContainer.class, params); } 我可以找到的所有泽西/ guice的例子就像serve(“/rest/*”.with(GuiceContainer.class, params);这对我serve(“/rest/*”.with(GuiceContainer.class, params); (“/ rest / users”),但我想要的很好URI没有任何前缀,如’rest’或’ws’。

如何清除ResourceBundle缓存

这是使用Guice在Tomcat上运行的webapp。 根据文档,我们应该能够调用ResourceBundle.clearCache(); 清除ResourceBundle缓存并可能从bundle属性文件中获取最新信息。 我们还尝试了以下方法: Class klass = ResourceBundle.getBundle(“my.bundle”).getClass().getSuperclass(); Field field = klass.getDeclaredField(“cacheList”); field.setAccessible(true); ConcurrentHashMap cache = (ConcurrentHashMap) field.get(null); cache.clear(); // If i debug here I can see the cache is now empty! 和 ResourceBundle.clearCache(this.class.getClassLoader()); 我期待的行为是: 启动tomcat并点击页面,然后显示“Hello World” 将包含“Hello World”的属性文件更改为“Goodbye Earth” 使用servlet清除缓存 点击页面,期待看到’Goodbye Earth’ 所以问题是,ResourceBundle.clearCache()实际上是如何工作的? 还有一些我们需要清除的通用文件缓存吗?

没有实现绑定 – Java Guice

新手在这里试图使用一个虚拟Java Facebook应用程序,使用Guice将数据库dependency injectionFacebook工厂,但继续让Guice错误告诉我: ###在查找使用@ com.example.storage.annotations.SystemDb注释的com.example.storage.Db时,没有使用@ com.example.storage.annotations.SystemDb()注释的com.example.storage.Db的实现。 ()for com.example.facebook.client.exceptions.FacebookExceptionHandlerDb中的参数0,位于com.example.facebook.client.guice.FacebookClientModule.configure ###无法在com.example.facebook.statsd.StatsdClient中找到合适的构造函数。 类必须有一个(也是唯一一个)使用@Inject注释的构造函数或一个非私有的零参数构造函数。 at com.example.facebook.statsd.StatsdClient.class,同时在com.example.facebook.client.exceptions.FacebookExceptionHandlerDb中找到参数1的com.example.facebook.statsd.StatsdClient。 com.example.facebook.client.guice.FacebookClientModule.configure 应用代码: app.java package com.example.facebook; import com.google.inject.Guice; import com.restfb.Connection; import com.restfb.types.Post; import com.example.facebook.client.FacebookClientFactory; import com.example.facebook.client.RobustFacebookClient; import com.example.facebook.client.guice.FacebookClientModule; import com.example.facebook.statsd.StatsdClient; public class App { public static void main ( String[] args ) { final FacebookClientFactory facebookClientFactory = Guice.createInjector(new FacebookClientModule()).getInstance(FacebookClientFactory.class); //error from line above final […]

使用Guice构造函数注入

我有一些使用工厂的示例代码。 我想通过删除工厂来清理代码并使用Guice代替。 我试图这样做,但我遇到了一个小障碍。 我对Guice很新,所以我希望有人可以帮助我。 现有客户端代码(使用工厂): 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(); } } 我试图使用Guice进行重构: //Replaces existing factories public class MailModule extends AbstractModule { @Override protected void configure() { bind(Mail.class) .to(MailImpl.class); bind(MailConfig.class) .to(MailConfigImpl.class); } } public class MailImpl implements Mail { private […]

如何让google guice注入一个自定义记录器,比如一个commons-logging或log4j logger

Google guice具有内置的记录器绑定function 。 但是,如果我想使用commons-logging或log4j记录器呢? 我是否可以注入一个由其创建的日志 LogFactory.getLog(CLASS.class) 但具有与内置绑定相同的行为: 绑定自动将记录器的名称设置为注入Logger的类的名称。 它甚至有意义吗? 或者喊我只是使用内置的java Logger? 或者只使用没有注射的公共记录?