我尝试使用java.sql.DataSource
方法和@Provides
提供BindingAnnotation
。如何告诉Guice扫描包或类以获取这些注释?我是否必须设置我自己的Guice(servlet-)模块或由Magnolia处理?据我所知,有可能将类型与Magnolia模块描述符xml中的实际实现绑定,但我不认为这在这种情况下是可行的。到目前为止,我有一个提供者方法:
@Provides
@MembershipDS
public DataSource createDataSource() {
// retreive the data source over JNDI and return it
// (calling this method manually returns an actual DataSource)
}
注释:
@BindingAnnotation
@Target({ FIELD, PARAMETER, METHOD })
@Retention(RUNTIME)
public @interface MembershipDS {
}
但是当我在某处注入它时,它为null并且不调用provider方法:
@Inject
@MembershipDS
private DataSource membershipDS;
请注意,我尝试了javax.inject.Inject
和com.google.inject.Inject
,两者都会产生相同的结果。
答案 0 :(得分:0)
您也可以通过编程方式配置这些内容。 Magnolia有一种机制,您可以将配置器添加到模块描述符xml文件中。如果该类是Guice模块,它将被使用并且可以使用所有Guice配置选项。
<components>
<id>main</id>
<configurer>
<class>com.mycompany.MyConfigurer</class>
</configurer>
...
</components>
和班级
public class MyConfigurer extends AbstractGuiceComponentConfigurer {
// your Guice bindings goes here
}