Announcement Announcement Module
Collapse
No announcement yet.
Using SpEL in Depends-On Attribute Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Using SpEL in Depends-On Attribute

    I'm trying to set up some conditional configuration in my context. Essentially, I want my H2 database server to only be intialized when specifically told to do so.

    I have the following bean:

    Code:
    	<bean id="localSharesDbcpDataSource" class="org.apache.commons.dbcp.BasicDataSource"
    		destroy-method="close" lazy-init="true" depends-on="#{systemProperties['h2Server.enabled']?'h2Server':null}">
    		<property name="driverClassName" value="${local.jdbc.driverClassName}" />
    		<property name="url" value="${local.jdbc.url}" />
    		<property name="username" value="${local.jdbc.username}" />
    		<property name="password" value="${local.jdbc.password}" />
    	</bean>
    The idea is that if the h2Server.enabled property is passed in, this bean should depend on my h2Server bean:

    Code:
    <bean id="h2Server" class="org.h2.tools.Server" factory-method="createTcpServer"
    		init-method="start" destroy-method="stop" lazy-init="true">
    		<constructor-arg
    			value="-tcp,-tcpAllowOthers,true,-tcpPort,${local.jdbc.h2.port}" />
    	</bean>
    If h2Server.enabled is not set to true, the bean depends on nothing. Can you use the Spring Expression Language in the depends-on attribute? I'm currently getting:

    Caused by: org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'localSharesDataSource' defined in class path resource [com/fidelity/shares/dal/infrastructureContext.xml]: Cannot resolve reference to bean 'localSharesDbcpDataSource' while setting constructor argument; nested exception is org.springframework.beans.factory.NoSuchBeanDefini tionException: No bean named '#{systemProperties['h2Server.enabled']?'h2Server':null}' is defined
    at org.springframework.beans.factory.support.BeanDefi nitionValueResolver.resolveReference(BeanDefinitio nValueResolver.java:328)
    at org.springframework.beans.factory.support.BeanDefi nitionValueResolver.resolveValueIfNecessary(BeanDe finitionValueResolver.java:106)
    at org.springframework.beans.factory.support.Construc torResolver.resolveConstructorArguments(Constructo rResolver.java:616)
    at org.springframework.beans.factory.support.Construc torResolver.autowireConstructor(ConstructorResolve r.java:148)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.autowireConstructor(Abs tractAutowireCapableBeanFactory.java:1003)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.createBeanInstance(Abst ractAutowireCapableBeanFactory.java:907)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.doCreateBean(AbstractAu towireCapableBeanFactory.java:485)
    at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.createBean(AbstractAuto wireCapableBeanFactory.java:456)
    at org.springframework.beans.factory.support.Abstract BeanFactory$1.getObject(AbstractBeanFactory.java:2 91)
    at org.springframework.beans.factory.support.DefaultS ingletonBeanRegistry.getSingleton(DefaultSingleton BeanRegistry.java:222)
    at org.springframework.beans.factory.support.Abstract BeanFactory.doGetBean(AbstractBeanFactory.java:288 )
    at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:190)
    at org.springframework.beans.factory.support.BeanDefi nitionValueResolver.resolveReference(BeanDefinitio nValueResolver.java:322)
    ... 60 more
    Caused by: org.springframework.beans.factory.NoSuchBeanDefini tionException: No bean named '#{systemProperties['h2Server.enabled']?'h2Server':null}' is defined
    at org.springframework.beans.factory.support.DefaultL istableBeanFactory.getBeanDefinition(DefaultListab leBeanFactory.java:527)
    at org.springframework.beans.factory.support.Abstract BeanFactory.getMergedLocalBeanDefinition(AbstractB eanFactory.java:1083)
    at org.springframework.beans.factory.support.Abstract BeanFactory.doGetBean(AbstractBeanFactory.java:274 )
    at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:190)
    at org.springframework.beans.factory.support.Abstract BeanFactory.doGetBean(AbstractBeanFactory.java:281 )
    at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:190)
    at org.springframework.beans.factory.support.BeanDefi nitionValueResolver.resolveReference(BeanDefinitio nValueResolver.java:322)
Working...
X