![](/img/trans.png)
[英]How to configure the conditional beans provided in Spring 4 using XML configuration file
[英]how to configure spring beans with null return in context xml file
如果我们可以在spring xml中的java bean下面进行转换,有没有办法
@Bean(name = "tls")
public SslContextFactory getSslContextFactory() {
return null;
}
我尝试如下设置
<bean id="tls" class="abc.SslContextFactory"/>
但这会返回一个对象,且所有值都设置为null。 但是,上述java bean可以正常工作,并按预期返回null。 我尝试了如下设置
<bean id="tls" class="abc.SslContextFactory">
<null/>
</bean>
但这在语法上是错误的。
我是春季的新手,正在从事多个实验以更多地了解春季。 任何建议表示赞赏:)
如果您只是在某个地方自动装配该bean,则可以这样使用-
@Autowired(required = false)
private SslContextFactory tls;
另外,您不必在xml中声明任何内容。 如果未找到任何bean定义,则@Autowired(required = false)
会将其设置为null
。
您需要使用spring FactoryBean。
public class SslContextNullFactory implements FactoryBean<Void> {
public Void getObject() throws Exception {
return null;
}
public Class<? extends Void> getObjectType() {
return null;
}
public boolean isSingleton() {
return true;
}
}
和
<bean id="tls" class = "abc.SslContextNullFactory" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.