Java 使用类似于Spring的GUI将属性注入类,如VelocityEngineFactoryBean
我正试图使用Guice在代码中注入org.springframework.ui.velocity.VelocityEngineFactoryBean的实例。但不确定如何指定此Bean的属性velocityProperties 目前在春季,我可以使用Java 使用类似于Spring的GUI将属性注入类,如VelocityEngineFactoryBean,java,spring,dependency-injection,guice,velocity,Java,Spring,Dependency Injection,Guice,Velocity,我正试图使用Guice在代码中注入org.springframework.ui.velocity.VelocityEngineFactoryBean的实例。但不确定如何指定此Bean的属性velocityProperties 目前在春季,我可以使用 <bean id="velocityEngine" class="org.springframework.ui.velocity.VelocityEngineFactoryBean"> <proper
<bean id="velocityEngine"
class="org.springframework.ui.velocity.VelocityEngineFactoryBean">
<property name="velocityProperties">
<props>
<prop key="resource.loader">class</prop>
<prop key="class.resource.loader.class">
org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
</prop>
</props>
</property>
</bean>
班
org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
类VelocityEngineFactoryBean没有@Inject annotation的此属性
public class VelocityEngineFactoryBean extends VelocityEngineFactory
implements FactoryBean<VelocityEngine>, InitializingBean, ResourceLoaderAware {
private VelocityEngine velocityEngine;
公共类VelocityEngineFactoryBean扩展了VelocityEngineFactory
实现FactoryBean、初始化Bean、ResourceLoaderWare{
私人VelocityEngine VelocityEngine;
它也没有为此提供设置器
这就是我实现它的方式
@Provides
VelocityEngine velocityEngine() throws Exception {
Properties props = new Properties();
props.put("resource.loader", "class");
props.put("class.resource.loader.class", ClasspathResourceLoader.class.getName());
return new VelocityEngine(props);
}
Guice将注入VelocityEngine
如果我想使用VelocityEngineFactoryBean,可以使用反射并提供
来创建VelocityEngineFactoryBean的实例。
然而,这看起来更像是一种黑客行为,所以我想我将直接创建Apache引擎的实例,而不是使用Spring。这是我实现它的方式
@Provides
VelocityEngine velocityEngine() throws Exception {
Properties props = new Properties();
props.put("resource.loader", "class");
props.put("class.resource.loader.class", ClasspathResourceLoader.class.getName());
return new VelocityEngine(props);
}
Guice将注入VelocityEngine
如果我想使用VelocityEngineFactoryBean,可以使用反射并提供
来创建VelocityEngineFactoryBean的实例。
然而,这看起来更像是一个黑客行为,所以我想我将直接创建Apache引擎的实例,而不是使用Spring。您尝试了遵循Guice指南,但它对您无效?是的,我遵循了它。但是找不到任何可以执行此操作的东西。有
bindProperties
,但需要使用相应的类>@Inject
来绑定属性。由于这是作为jar编译的,所以我不能这么做。我正在做的工作是为实际的VelocityEngine编写一个包装器,或者使用Guice的反射。但是这两种方法似乎都是一种很有技巧的方法。您尝试了遵循Guice指南,但它对您无效?是的,我遵循了它。但是找不到任何可以执行此操作的东西。有bindProperties
,但需要相应的类使用@Inject
来绑定属性。因为这是作为jar编译的,所以我无法做到。我正在做的工作是为实际的VelocityEngine编写一个包装器或使用Guice的反射。但是这些方法似乎是一种老套的做事方式。