Announcement Announcement Module
Collapse
No announcement yet.
Skipping bean creation Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Skipping bean creation

    Hello.
    I'm not sure what's the best way to design this with Spring. Neither if it's possible or not. I guess it is.
    Basically, I want to be able to NOT create some beans in my application based on some system property.
    Real Example: I have a quartz job, which is declared using the know classes CronTriggerBean and SchedulerFactoryBean. They are all declared in a separated XML file like below:

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
    <beans>	
    	<!-- The cron -->
    	<bean id="myCron" class="a.b.c.MyCron">
    	</bean>
    
    	<!-- The Quartz job -->
    	<bean id="MyCronJob" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean">
    		<property name="targetObject" ref="MyCron" />
    		<property name="targetMethod" value="run" />
    	</bean>
    
    	<!-- The Quartz trigger -->
    	<bean id="MyCronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
    		<property name="jobDetail" ref="MyCronJob" />
    		<property name="cronExpression" value="${MY_CRON_EXPRESSION}" />
    	</bean>
    
    	<!-- The Quartz scheduler -->
    	<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
    		<property name="triggers">
    			<list>
    				<ref bean="MyCronTrigger" />
    			</list>
    		</property>
    	</bean>
    
    </beans>
    The cron is supposed to run only in some servers where the app is deployed. So, I have a property called

    Code:
    MY_CRON_ACTIVE = true/false
    which says if the cron is active or not.
    How is the best way to handle this scenario using Spring?

    It would be good if I could add some custom attribute (like enabled) at bean or "module" level like below:

    Code:
    <!-- skips the bean creation if MY_CRON_ACTIVE is true-->
    <bean name="myCronScheduler" enabled="{MY_CRON_ACTIVE}"/>
    OR

    Code:
    <!-- skips the creation of all beans declared in the XML if MY_CRON_ACTIVE is true-->
    <beans enabled="{MY_CRON_ACTIVE}">
    ...
    </beans>
    That's possible? Or there is an easier/standard way to do this?
    Thanks!

  • #2
    Any suggestions?

    Comment

    Working...
    X