Dynamic list properties in spring - spring

I've been googling this for about a hour, with little success.
Suppose that in my web.xml I have :
<Parameter name="hibernate.websitespecific.entityscanpackages" value="com.mystuff.pojo.entities, com.mystuff.otherpackage.pojo.entities"/>
and in my spring context config I have:
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="packagesToScan">
<list>
<value>com.mystuff.somethingelse.pojo</value>
<value>com.mystuff.weirdbeans.domain</value>
</list>
</property>
[...]
I'd like to add hibernate.websitespecific.entityscanpackages to the list of packages to scan in a clean way. How do I do that?

You can reference context parameters in bean definition files using SPEL:
For example:
<property name="foo" value="#{contextParameters.fooParamName}" />
Edit
To merge the both package list:
<property name="packagesToScan" value="#{contextParameters.paramName + ',pk1,pk2,pk3'}"/>
or
<property name="packagesToScan" value="#{contextParameters.paramName + ',' + T(org.springframework.util.StringUtils).collectionToCommaDelimitedString(#someList)}" />
<util:list id="someList">
<value>pk1</value>
<value>pk2</value>
...
</util:list>

Related

How to register hibernate interceptor in Spring

I need to register class which is derived from EmptyInterceptor in Spring. Do someone know how to register this interceptor to Hibernate session? Definition of my SesionFactory is bellow.
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan">
<list>
<value>com.foo</value>
</list>
</property>
<property name="hibernateProperties" ref="hibernateProperties"/>
</bean>
In order to register an interceptor you have to use entityInterceptor property. Please try :
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan">
<list>
<value>com.foo</value>
</list>
</property>
<property name="hibernateProperties" ref="hibernateProperties"/>
<property name="entityInterceptor">
<bean class="foo.bar.MyInterceptor"/>
</property>
</bean>

How to Pass Multiple Packages to packagesToScan property in Spring using XML Configuration

Assume i have two packages com.test1 and com.test2 in different modules called M1 (com.test1) and M2 (com.test2).
Now in the following example i configured module1 package.
<bean id="entityManager" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="packagesToScan" value="com.test1" />
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter" ref="hibernateVendor" />
<property name="jpaPropertyMap" ref="jpaPropertyMap" />
</bean>
But i want to configure Module2 package as well in packagesToScan property. How to configure.
I found answer my self.
<bean id="entityManager" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="packagesToScan">
<array>
<value>com.test1</value>
<value>com.test2</value>
</array>
</property>
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter" ref="hibernateVendor" />
<property name="jpaPropertyMap" ref="jpaPropertyMap" />
</bean>

Programmatically set property of bean

I am trying to figure out how I can adjust the "period" property of the "destroyWorldTask" bean that is defined like this in my list of beans. Is this possible? What is the proper way to do this?
<bean id="mytimerfactory"
class="org.springframework.scheduling.timer.TimerFactoryBean">
<property name="daemon" value="true"/>
<property name="myTimerTasks">
<list>
<bean class="org.springframework.scheduling.timer.ScheduledTimerTask" id="destroyWorldTask">
<property name="delay" value="100"/>
<property name="period" value="10000/>
<property name="runnable">
<bean class="com.scene7.is.util.SafeRunnable">
<constructor-arg ref="destroyWorld"/>
</bean>
</property>
</bean>
</list>
</property>
</bean>
There are two possible answers:
1. If you want the "period" property be set somewhere in the program, you don't need to set in the context configuration. (Which I think not suitable for you, as you are using a spring class, not yours).
2. Extend from org.springframework.scheduling.timer.ScheduledTimerTask and make your edition of the class, something like:
public MyTimeScheduledTimerTast extends ScheduledTimerTask{
//...
}
and set that property in your program. (Now it's in your hand)
Then update your context configuration like this:
<bean id="mytimerfactory" class="org.springframework.scheduling.timer.TimerFactoryBean">
<property name="daemon" value="true"/>
<property name="myTimerTasks">
<list>
<bean class="myPackage.MyScheduledTimerTask" id="destroyWorldTask">
<!-- Set those properties that are not set in your program -->
</bean>
</list>
</property>
</bean>

How to add more hbm to existing mappingResources or existing hbm list in Spring

I have a parent-app, which includes sub-apps.
My Parent app has its own included list of hbms
<bean name="mappingResources"
class="my.xxx.MyListFactoryBean">
<property name="sourceList">
<list>
<value>aaa/bbb/aa.hbm.xml</value>
<value>aaa/bbb/bb.hbm.xml</value>
<value>aaa/bbb/cc.hbm.xml</value>
</list>
</property>
</bean>
My sub-apps want to add its own list of dependent hbms to the parent-app's.
The way it should work is, if it includes this sub-app then it would include the new hbms as well and the child-app would initiate the include.
new hbms to be included could look like
xx/dd.hbm.xml
xx/ee.hbm.xml
How can we do it?
Your Solution could be:
Split up the 'mappingResources' to
<bean name="mappingResources" class="my.xxx.MyListFactoryBean">
<property name="sourceList" ref="hbmSourceList" />
</bean>
<bean id="hbmSourceList" class="java.util.ArrayList">
<constructor-arg>
<list>
<value>aaa/bbb/aa.hbm.xml</value>
<value>aaa/bbb/bb.hbm.xml</value>
<value>aaa/bbb/cc.hbm.xml</value>
</list>
</constructor-arg>
</bean>
In the child-app
refer to the bean "hbmSourceList" and invoke an "addAll" on it with an another list via the "MethodInvokingFactoryBean"
<bean id="hbmSourceListExtender" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetObject"><ref bean="hbmSourceList"/></property>
<property name="targetMethod"><value>addAll</value></property>
<property name="arguments">
<ref local="childAppHbmSourceList"/>
</property>
</bean>
<bean id="childAppHbmSourceList" class="java.util.ArrayList">
<constructor-arg>
<list>
<value>xx/dd.hbm.xml</value>
<value>xx/ee.hbm.xml</value>
</list>
</constructor-arg>
</bean>

Calling spring bean from alfresco activiti process

Within alfresco activiti, could I call a spring bean using the servicetask like :
<serviceTask id="assignApplicationId" name="Assign Application Id"
activiti:expression="${sequenceUtil.getOutboundId(task.id)}"
activiti:resultVariable="OutboundWF_ApplicationNumber"/>
however, in my custom context I declared the sequenceUtil as the following:
<bean id="sequenceUtil" name="sequenceUtil" class="com.tts.mersal.presentation.bean.dialog.util.SequenceUtil">
<property name="searchService">
<ref bean="searchService" />
</property>
<property name="nodeService">
<ref bean="nodeService" />
</property>
<property name="workflowService">
<ref bean="WorkflowService" />
</property>
</bean>
Actually I got the following exception
org.activiti.engine.impl.javax.el.PropertyNotFoundException: Cannot resolve identifier 'sequenceUtil'
at org.activiti.engine.impl.juel.AstIdentifier.eval(AstIdentifier.java:83)
at org.activiti.engine.impl.juel.AstMethod.invoke(AstMethod.java:79)
at org.activiti.engine.impl.juel.AstMethod.eval(AstMethod.java:75)
at org.activiti.engine.impl.juel.AstEval.eval(AstEval.java:50)
at org.activiti.engine.impl.juel.AstNode.getValue(AstNode.java:26)
at org.activiti.engine.impl.juel.TreeValueExpression.getValue(TreeValueExpression.java:114)
at org.activiti.engine.impl.el.JuelExpression.getValue(JuelExpression.java:46)
I got it :)
I have to override the activitiProcessEngineConfiguration bean to include my custom bean within beans property
<!-- -->
<!-- Activiti Process Engine -->
<!-- -->
<bean id="activitiProcessEngineConfiguration"
class="org.alfresco.repo.workflow.activiti.AlfrescoProcessEngineConfiguration">
<property name="dataSource" ref="wrappedDataSource" />
<property name="transactionManager" ref="transactionManager" />
<property name="databaseSchemaUpdate" value="${db.schema.update}" />
<property name="history" value="full" />
<property name="jobExecutorActivate" value="true" />
<!-- Limit the visible beans in expressions -->
<property name="beans">
<map>
<entry key="services" value-ref="ServiceRegistry" />
<entry key="sequenceUtil" value-ref="sequenceUtil" />
</map>
</property>
<property name="customTypes">
<list>
<ref bean="activitiScriptNodeType" />
<ref bean="activitiScriptNodeListType" />
</list>
</property>
<property name="customPreBPMNParseListeners">
<list>
<ref bean="activitiParseListener" />
</list>
</property>
</bean>
There is a much better way to map bean names to el epressions. At least for alfresco 5.2.
Originally activitiProcessEngineConfiguration defined like that:
<bean id="activitiProcessEngineConfiguration" class="org.alfresco.repo.workflow.activiti.AlfrescoProcessEngineConfiguration">
<!-- Limit the visible beans in expressions -->
<property name="beans" ref="activitiBeanRegistry" />
</bean>
Where activitiBeanRegistry defined like that:
<util:map id="activitiBeanRegistry" map-class="java.util.HashMap">
<entry key="services" value-ref="ServiceRegistry" />
</util:map>
So you can easy add your beans with names without touching original activitiProcessEngineConfiguration. Like that:
<bean id="my.activitiBeanRegistry" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean" depends-on="activitiBeanRegistry">
<property name="targetObject">
<ref bean="activitiBeanRegistry" />
</property>
<property name="targetMethod" value="put" />
<property name="arguments">
<list>
<value>sequenceUtil</value>
<ref bean="sequenceUtil" />
</list>
</property>
Open for extension closed for modification :)
Full source can be found here
See also activiti-context.xml

Resources