小编vel*_*ist的帖子

绑定到TypeLiteral是谷歌guice中的好或坏做法

Google guice new TypeLiteral<C<T>>() {}用来克服我们无法使用的事实C<T>.class.

现在通常如下:

bind(new TypeLiteral<C<T>>() {}).to(MyCSubclassTypedToT.class);
Run Code Online (Sandbox Code Playgroud)

然而,想象一下不同的场景.我们有一个通用接口,我们想要注入,我们拥有的实现由泛型类提供.

Guice允许你这样做:

bind(new TypeLiteral<MyGenericInterface<T>>() {}).to(new TypeLiteral<MyGenericClass<T>>() {});
Run Code Online (Sandbox Code Playgroud)

另一种方法是扩展MyGenericClass,如下所示:

MyTypedClass extends MyGenericClass<T>
Run Code Online (Sandbox Code Playgroud)

然后像这样绑定它:

bind(MyGenericInterface<T>>() {}).to(MyTypedClass.class);
Run Code Online (Sandbox Code Playgroud)

如果MyGenericInterface被大量注入(尽管有不同的类型),并且每次我注入它都使用MyGenericClass,后一种方法导致过于冗长的代码.因此,我倾向于使用前者.

我非常希望听到其他人关于在guice绑定的to子句中使用TypeLiteral的意见.我担心我可能会有点短缺,因此不会看到这种方法的缺陷.

java dependency-injection guice

8
推荐指数
1
解决办法
5611
查看次数

使用<Set name ="extraClasspath">设置Jetty类路径时出现问题

我有一个外部jar,我不想与我的webapp捆绑在一起.我把它放在码头的类路径上.不幸的是,我正在部署的环境非常严格,我只能写入两个目录:包含上下文的目录和包含webapps的目录.

我选择将我的外部jar放在webapp目录中.为了让我的webapp可见,我正在配置WebAppContext以查看这个额外的jar.这是我的myAppContext.xml文件

<Configure id="myContext" class="org.mortbay.jetty.webapp.WebAppContext">
<Set name="contextPath">/myApp</Set>
<Set name="war"><SystemProperty name="jetty.home" default="."/>/webapps/myApp.war</Set>
<Set name="defaultsDescriptor"><SystemProperty name="jetty.home" default="."/>/etc/webdefault.xml</Set>
<Set name="extraClasspath"><SystemProperty name="jetty.home" default="."/>/webapps/myExternalJAR.jar</Set>
<New class="com.mycompany.mypackage.MyExternalClass"></New>
Run Code Online (Sandbox Code Playgroud)

最后一行尝试实例化可以在我的外部jar中找到的MyExternalClass.当启动jetty时,我在配置文件的这一行得到一个ClassNotFoundException.

有什么我想念的吗?根据规范,这应该工作.对于如何处理这个问题,我会非常感激.

configuration jetty classpath classnotfoundexception

5
推荐指数
0
解决办法
1091
查看次数