If else in spring context with the prop key - spring

Is there a way to provide another value for key if null? I want to use the source and dest db. But if dest db details are null then use source.
<bean id="dataSource" class="com.zaxxer.hikari.HikariConfig">
<property name="poolName" value="springHikariCP" />
<property name="dataSourceClassName" value="oracle.jdbc.pool.OracleDataSource" />
<property name="maximumPoolSize" value="${maximumPoolSize}" />
<property name="idleTimeout" value="${idleTimeout}" />
<property name="connectionTimeout" value="${connectionTimeout}" />
<property name="dataSourceProperties">
<props>
<prop key="url">${dest.db.url}</prop>
<prop key="user">${dest.db.user}</prop>
<prop key="password">${dest.db.password}</prop>
</props>
</property>
</bean>
What I tried:
<property name="dataSourceProperties">
<props>
<prop key="url">${dest.db.url?:${src.db.url}}</prop>
<prop key="user">${dest.db.user?:${src.db.user}}</prop>
<prop key="password">${dest.db.password?:${src.db.password}}</prop>
</props>
</property>
</bean>
Though the dest db details are not null, the data is being written into the source. Not sure why? Is there something wrong in the way I'm defining it?

You can use ternary operator to specify an if-else condition in spel. Take a look at this:
http://www.mkyong.com/spring3/spring-el-ternary-operator-if-then-else-example/

Related

FreeMarkerViewResolver not render the template when i request use ajax

Sorry everyone. My english is so bad,but i have some questions to ask.
When i try to use ajax get a freemarker view.It's not render just html code.
The following is my config.
springmvc-servlet.xml
<bean id="freemakerViewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<property name="cache" value="true" />
<property name="prefix" value="" />
<property name="suffix" value=".html" />
<property name="contentType" value="text/html;charset=UTF-8"></property>
<property name="requestContextAttribute" value="request" />
<property name="exposeSpringMacroHelpers" value="true" />
<property name="exposeRequestAttributes" value="true" />
<property name="exposeSessionAttributes" value="true" />
</bean>
freeMarkerConfigurer
<bean id="freeMarkerConfigurer"
class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="templateLoaderPath" value="/WEB-INF/views/" />
<property name="freemarkerSettings">
<props>
<prop key="template_update_delay">0</prop>
<prop key="default_encoding">UTF-8</prop>
<prop key="number_format">0.##########</prop>
<prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop>
<prop key="date_format">yyyy-MM-dd</prop>
<prop key="time_format">HH:mm:ss</prop>
<prop key="classic_compatible">true</prop>
<prop key="template_exception_handler">ignore</prop>
</props>
</property>
</bean>
my view
<ul>
<#list items as item>
<li>item.name</li>
</#list>
</ul >
response data
<ul></ul >
It's my wrong.I submit the wrong parameters,so the list is empty.

Log JavaMailSenderImpl

i use Spring to send email in my application.
I want to log the imap server operation, when i send emails.
I try to implement log in my applicationContext.xml as follow:
<bean id="JavaMailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="${smtpHost}" />
<property name="port" value="${smtpPort}" />
<property name="username" value="${userName}" />
<property name="password" value="${password}" />
<property name="session" ref="mailSession" />
<props>
<prop key="mail.debug">true</prop>
</props>
</bean>
but doesn't works.Any suggest?
Thanks
You're missing the outer property.
...
<property name="session" ref="mailSession">
<property name="javaMailProperties">
<props>
<prop key="mail.debug">true</prop>
</props>
</property>
...

Spring same dataSource for different JdbcTemplates

I have different DAO's that have the same class that require different jdbcTemplates who all use the same type of dataSources. Is there a way to consolidate my code, so I don't need to use so much copy and paste.
An example of what I have in xml is:
<bean id="jdbcTemplate1" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="jdbcDataSource1" />
</bean>
<bean id="jdbcDataSource1" class="com.zaxxer.hikari.HikariDataSource"
destroy-method="shutdown">
<constructor-arg>
<bean class="com.zaxxer.hikari.HikariConfig">
<constructor-arg>
<props>
<prop key="dataSource.url">dataSourceUrl
</prop>
<prop key="dataSource.user">user</prop>
<prop key="dataSource.password">password</prop>
</props>
</constructor-arg>
<property name="dataSourceClassName"
value="com.mysql.jdbc.jdbc2.optional.MysqlDataSource" />
</bean>
</constructor-arg>
</bean>
<bean id="jdbcTemplate2" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="jdbcDataSource2" />
</bean>
<bean id="jdbcDataSource2" class="com.zaxxer.hikari.HikariDataSource"
destroy-method="shutdown">
<constructor-arg>
<bean class="com.zaxxer.hikari.HikariConfig">
<constructor-arg>
<props>
<prop key="dataSource.url">dataSourceUrl
</prop>
<prop key="dataSource.user">user</prop>
<prop key="dataSource.password">password</prop>
</props>
</constructor-arg>
<property name="dataSourceClassName"
value="com.mysql.jdbc.jdbc2.optional.MysqlDataSource" />
</bean>
</constructor-arg>
</bean>
As seen by the code, jdbcDataSource1 and jdbcDataSource2 are the same. So is there a way to consolidate the two?
This would be the way to pass the same datasource to the two JDBC templates:
<bean id="jdbcTemplate1" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="jdbcDataSource" />
</bean>
<bean id="jdbcTemplate2" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="jdbcDataSource" />
</bean>
<bean id="jdbcDataSource" class="com.zaxxer.hikari.HikariDataSource"
destroy-method="shutdown">
<constructor-arg>
<bean class="com.zaxxer.hikari.HikariConfig">
<constructor-arg>
<props>
<prop key="dataSource.url">dataSourceUrl
</prop>
<prop key="dataSource.user">user</prop>
<prop key="dataSource.password">password</prop>
</props>
</constructor-arg>
<property name="dataSourceClassName"
value="com.mysql.jdbc.jdbc2.optional.MysqlDataSource" />
</bean>
</constructor-arg>
</bean>
You can keep one template and one datasource in bean definitions. If your dao's template are already predefined and you don't want to change it, you can still use only one jdbc template as follows:
<bean id="yourDao1" class="package.YourDao1">
<property name="jdbcTemplate1" ref="jdbcTemplate" />
</bean>
<bean id="yourDao2" class="package.YourDao2">
<property name="jdbcTemplate2" ref="jdbcTemplate" />
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="jdbcDataSource" />
</bean>
<bean id="jdbcDataSource" class="com.zaxxer.hikari.HikariDataSource"
destroy-method="shutdown">
<constructor-arg>
<bean class="com.zaxxer.hikari.HikariConfig">
<constructor-arg>
<props>
<prop key="dataSource.url">dataSourceUrl
</prop>
<prop key="dataSource.user">user</prop>
<prop key="dataSource.password">password</prop>
</props>
</constructor-arg>
<property name="dataSourceClassName"
value="com.mysql.jdbc.jdbc2.optional.MysqlDataSource" />
</bean>
</constructor-arg>
</bean>

Integrating OSworkflow with Spring and Hibernate

How can I integrate OSworkflow with spring and hibernate.What are the configuration i need to specify in xml files and which jar files are requires for it.
Update
I have defined the applicationContext.xml as follows
<bean id="workflowStore" class="com.opensymphony.workflow.spi.hibernate.SpringHibernateWorkflowStore" autowire="byName"> <property name="resource" value="workflow-defs.xml"/>
<property name="reload" value="true"/> </bean>
<bean id="workflowFactory" class="com.opensymphony.workflow.loader.XMLWorkflowFactory" init-method="initDone"/>
<bean id="workflowConfiguration" class="com.opensymphony.workflow.config.SpringConfiguration">
<property name="store"><ref local="workflowStore"/></property>
<property name="factory"><ref local="workflowFactory"/></property>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="namingStrategy">
<bean class="org.hibernate.cfg.ImprovedNamingStrategy" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
<prop key="hibernate.cache.provider_configuration_file_resource_path">ehcache/ehcache-hibernate-local.xml</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<property name="packagesToScan" value="net.top.*.entity.*" />
<property name="mappingResources">
<list>
<value>com/opensymphony/workflow/spi/hibernate/WorkflowDescriptor.hbm.xml</value>
<value>com/opensymphony/workflow/spi/hibernate/HibernateCurrentStep.hbm.xml</value>
<value>com/opensymphony/workflow/spi/hibernate/HibernateHistoryStep.hbm.xml</value>
<value>com/opensymphony/workflow/spi/hibernate/HibernateWorkflowEntry.hbm.xml</value>
<value>com/opensymphony/module/propertyset/hibernate/PropertySetItemImpl.hbm.xml</value>
</list>
</property>
</bean>
It shows an error as
Could not determine type for: com.opensymphony.workflow.loader.NotNullStringType, for columns: [org.hibernate.mapping.Column(value)]
Did i miss anything?.

velocity not being initiated when triggered by Quartz Job

Velocity Engine is being used in my project for sending mails on user action and on job trigger.
While the mails that are sent on user action are perfectly fine, The mails being sent by jobs are sometimes received with empty content. This is very random and i cannot seem to connect it with any cause. There are no Error logs when this happens. (I did get a Velocity could not be initialized error once, before adding the NullLogChute property. I was advised that this would solve the exception, but all it did was to stop the exception from coming up in the logs. mails still have empty content) Please help!
Earlier exception:
ERROR 2012-07-29 05:00:00,219 com.myProject.util.VelocityUtil - Velocity could not be initialized!
java.lang.RuntimeException: Velocity could not be initialized!
..
..
Caused by: org.apache.velocity.exception.VelocityException: Failed to initialize an instance of org.apache.velocity.runtime.log.AvalonLogChute with the current runtime configuration.
I am setting the velocity config properties in my spring servlet xml :
<bean id="velocityUtil" class="com.myProject.util.VelocityUtil" >
<property name="velocityConfiguration">
<props>
<prop key="resource.loader">class</prop>
<prop key="class.resource.loader.class">org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader</prop>
<prop key="runtime.log.logsystem.class">org.apache.velocity.runtime.log.NullLogChute</prop>
</props>
</property>
Quartz properties set in spring servlet:
<bean class="org.springframework.scheduling.quartz.SchedulerFactoryBean">
<property name="applicationContextSchedulerContextKey"><value>applicationContext</value></property>
<property name="quartzProperties">
<props>
<prop key="org.quartz.scheduler.instanceName">Quartz</prop>
<prop key="org.quartz.scheduler.instanceId">AUTO</prop>
<prop key="org.quartz.scheduler.rmi.export">false</prop>
<prop key="org.quartz.scheduler.rmi.proxy">false</prop>
<prop key="org.quartz.threadPool.class">org.quartz.simpl.SimpleThreadPool</prop>
<prop key="org.quartz.threadPool.threadCount">5</prop>
<prop key="org.quartz.threadPool.threadPriority">1</prop>
<prop key="org.quartz.jobStore.class">org.quartz.impl.jdbcjobstore.JobStoreTX</prop>
<prop key="org.quartz.jobStore.tablePrefix">EES.QRTZ_</prop>
<prop key="org.quartz.jobStore.isClustered">true</prop>
<prop key="org.quartz.jobStore.misfireThreshold">60000</prop>
<prop key="org.quartz.jobStore.driverDelegateClass">org.quartz.impl.jdbcjobstore.MSSQLDelegate</prop>
</props>
</property>
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="triggers">
<list>
<ref bean="Mod1CronTrigger" />
<ref bean="Mod2CronTrigger" />
<ref bean="Mod3CronTrigger" />
<ref bean="Mod4CronTrigger" />
<ref bean="Mod5CronTrigger" />
<ref bean="Mod6CronTrigger" />
</list>
</property>
</bean>
Each of the triggers in the <list> define the jobDetail and CronExpression. for example,
<bean id="Mod1CronTrigger" class="org.springframework.scheduling.quartz.CronTriggerBean">
<property name="jobDetail" ref="Mod1Job"/>
<property name="cronExpression" value="0 30 3 1/1 * ? *"/>
</bean>
and
<bean name="Mod1Job" class="org.springframework.scheduling.quartz.JobDetailBean">
<property name="jobClass" value="com.myProject.jobs.Mod1Job" />
<property name="name" value="Mod1Job" />
</bean>

Resources