如何解决当属性文件没有值时,我有不想创建的 bean 列表,如何实现?
只有在配置文件中设置了 jdbc.url jdbc.username jdbc.password 值时,我才希望创建以下所有 bean。
如何通过基于 xml 的 spring 实现这一点?
这里 testODSDataSource 是主 bean,下面的所有 bean 都依赖于它,因此必须根据属性创建所有这些或不创建它们,如果 jdbc.url 为空,则不应创建。
<beans>
<bean id="testODSDataSource" class="com.company.abc.datasource.pool.WrappedDataSource" scope="prototype"
destroy-method="shutdown" lazy-init="true" >
<property name="wrappedDataSource" >
<!--Using an inner bean here,no need to expose the factory result -->
<bean
class="com.company.abc.datasource.pool.WrappedDataSourceFactory">
<property name="jdbcUrl" value="${jdbc.url:#{null}}" />
<property name="jdbcUser" value="${jdbc.username:#{null}}" />
<property name="jdbcPassword" value="${jdbc.password:#{null}}" />
</bean>
</property>
<property name="minPoolSize" value="5" />
<property name="initialPoolSize" value="10" />
<property name="maxPoolSize" value="${.jdbc.maxPoolSize}" />
<property name="maxPoolBehavIoUr" value="${.jdbc.maxPoolBehavIoUr}" />
<property name="maxIdleTime" value="300" />
<!--Cycle every minute the clean up -->
<property name="maintenanceCycle" value="60" />
<property name="acquireRetryAttempts" value="${.jdbc.acquireRetryAttempts}" />
<property name="acquireRetryDelay" value="${.jdbc.acquireRetryDelay}" />
<property name="sqlTestQuery" value="" />
</bean>
<bean id="testODSRepositoryInfo" class="com.company.abc.repositoryutils.common.Repositoryinformation" lazy-init="true" >
<description>
Holds the information about the Meta-data repository. The information can be directly accessed within the bean configuration
using Spring Expression Language (http://docs.spring.io/spring/docs/3.0.0.M3/spring-framework-reference/html/ch07s04.html).
For example: #{testODSRepositoryInfo.getRepositoryType()}
</description>
<constructor-arg name="MetaType" value="ODS"/>
<property name="dataSource" ref="testODSDataSource"/>
<property name="jdbcUrl" value="${jdbc.url:#{null}}"/>
</bean>
<bean id="testODSSequenceGenerator" class="com.company.abc.persistency.generator.SequenceGeneratorFactory" lazy-init="true"
>
<property name="datasource" ref="testODSDataSource" />
<property name="hibernateDialectClassName" value="#{testODSRepositoryInfo.getDialectClassName()}" />
</bean>
<bean id="testODSHibernateSessionFactory" class="org.springframework.orm.hibernate4.LocalSessionfactorybean" lazy-init="true">
<property name="dataSource" ref="testODSDataSource" />
<property name="hibernateProperties">
<bean class="com.company.abc.utils.spring.MapToPropertiesConverter">
<property name="properties">
<map key-type="java.lang.String" value-type="java.lang.String">
<entry key="hibernate.dialect" value="#{testODSRepositoryInfo.getDialectClassName()}"/>
<entry key="net.sf.ehcache.configurationResourceName" value="#{testODSRepositoryInfo.getCacheConfiguration()}"/>
</map>
</property>
</bean>
</property>
<property name="configLocation" value="classpath:meta-inf/hibernate.ods.cfg.xml" />
</bean>
<!--DeFinition of the different ODS persister beans-->
<bean id="testODSAbstractPersistencyBean" class="com.company.abc.persistency.AbstractPersister" abstract="true"
lazy-init="true">
<description>
The bean has to be defined abstract,as otherwise the Spring Container may accidentally try to instantiate it.
Other OSD persistency beans should use this bean as template.
</description>
<property name="sessionFactory" ref="testODSHibernateSessionFactory" />
</bean>
<bean id="testODSSLAViolationPersister" parent="testODSAbstractPersistencyBean"
class="com.company.abc.persistency.ods.impl.ODSSLAViolationPersisterImpl" lazy-init="true" >
<property name="repositoryType" value="#{testODSRepositoryInfo.getRepositoryType()}"/>
</bean>
<bean id="testODSEventPersister" parent="testODSAbstractPersistencyBean"
class="com.company.abc.persistency.ods.impl.ODSEventPersisterImpl" lazy-init="true" >
<property name="repositoryType" value="#{testODSRepositoryInfo.getRepositoryType()}"/>
</bean>
<bean id="testOdsTxManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager" lazy-init="true" >
<property name="sessionFactory" ref="testODSHibernateSessionFactory" />
<property name="dataSource" ref="testODSDataSource"/>
</bean>
<bean id="testDashboardStatisticsService"
class="com.company.abc.persistency.event.impl.OdsDashboardEventsServiceImpl" lazy-init="true" >
<property name="odsEventPersister" ref="testODSEventPersister"/>
<property name="odsSLAViolationPersister" ref="testODSSLAViolationPersister"/>
</bean>
</beans>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。