java—我有一个bean列表,当属性文件没有值时,我不想创建这些bean,如何实现这一点?

hm2xizp9  于 2021-07-13  发布在  Java
关注(0)|答案(0)|浏览(216)

我希望只有在配置文件中设置了jdbc.url jdbc.username jdbc.password值时,才能创建下面的所有bean。
如何通过基于xml的spring实现这一点?
这里testodsdatasource是主bean,下面的所有bean都依赖于它,因此必须根据属性创建所有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>

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题