Spring - setting property value from JNDI - spring

It is a bit of followup to my previous question
Spring and Hibernate - changing dialect
if for example I have this piece of .xml
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
<property name="persistenceUnitName" value="SpringMVCTest" />
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect" >
</prop>
</props>
</property>
</bean>
now, I wanted to set hibernate.dialect to value that is exposed by jndi by jdbc/dialect, but when I put <jee:jndi-lookup jndi-name="jdbc/MyDataSource"/> I am getting Invalid content was found starting with element 'jee:jndi-lookup'. No child element is expected at this so I suspect that I can't put any tags in prop.
So, is there any way I can insert jndi resource to this property?

Not completely sure, but you should be able to use Spring-EL here, like this:
<jee:jndi-lookup id="dialect" jndi-name="..." />
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
<property name="persistenceUnitName" value="SpringMVCTest" />
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect" >#{dialect}</prop>
</props>
</property>
</bean>

Related

Configuring auto_escape in freemarker spring-mvc Application

I am using spring-mvc with freemarker-2.3.27-incubating.jar.
And bean configuration for view-resolver like below...
<bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="templateLoaderPath" value="/"/>
<property name="freemarkerSettings">
<props>
<prop key="template_exception_handler">rethrow</prop>
<prop key="number_format">0.########</prop>
<prop key="date_format">dd/MM/yyyy</prop>
</props>
</property>
</bean>
<bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<property name="suffix" value=".ftl"/>
<property name="exposeSpringMacroHelpers" value="true"/>
<property name="exposeRequestAttributes" value="true"/>
<property name="exposeSessionAttributes" value="true"/>
<property name="requestContextAttribute" value="rc"/>
</bean>
Where and how can I use <#ftl output_format="HTML"> or <#ftl output_format="HTML" auto_esc=true> to enable HTML auto-scape ?
The #ftl tags you just add at the beginning of the template files themselves. But I would recommend setting recognize_standard_file_extensions to true in the freemarkerSettings properties, and then use .ftlh file extension instead of .ftl. Or, even better, you set incompatible_improvements to 2.3.27 there instead, which enables recognize_standard_file_extensions and some fixes. (Last not least, in case you want this for .ftl for some reason, you can set output_format to HTMLOutputFormat in freemarkerSettings properties too.)

Spring JPA with Hibernate Persistence provider and setting javax.persistence.schema-generation properties

I am using Spring JPA with Hibernate as the persistence provider and Derby as the database. (Specifically, the version of jars are Spring version 4.3.4, Hibernate Entity Manager version 5.2.10 and Derby 10.13.1.1).
I want to check the correct approach for
The schema generation in the JPA properties.
How to use the schema generated from step 1 to create the database schema.
(Note this is not a Spring Boot application.)
My application config file is this:
<context:annotation-config />
<context:component-scan base-package="org.service"/>
<context:component-scan base-package="org.repository"/>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="packagesToScan">
<array>
<value>org.model</value>
<value>org.repository</value>
<value>org.service</value>
</array>
</property>
<property name="dataSource" ref="dataSource"/>
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.DerbyTenSevenDialect</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.format_sql">false</prop>
<prop key="hibernate.hbm2ddl.auto">none</prop>
</props>
</property>
<property name="persistenceProvider">
<bean class="org.hibernate.jpa.HibernatePersistenceProvider"></bean>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="dataSource" ref="dataSource"/>
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.apache.derby.jdbc.EmbeddedDriver"/>
<property name="url" value="jdbc:derby:C:\\tmp\\testdb"/>
<property name="username" value="test"/>
<property name="password" value="test"/>
</bean>
<jpa:repositories base-package="org.repository"></jpa:repositories>
For step (1), my investigation and testing suggests that it's possible to set the persistence schema generation inside the jpaProperties property as follows:
<prop key="javax.persistence.schema-generation.scripts.action">create</prop>
<prop key="javax.persistence.schema-generation.scripts.create-target">create.sql</prop>
This will generate a create.sql file. For step (2), I used
<prop key="javax.persistence.schema-generation.create-source">script</prop>
<prop key="javax.persistence.schema-generation.create-script-source">create.sql</prop>
<prop key="javax.persistence.schema-generation.database.action">create</prop>
However do I need to still set create=true in the URL jdbc:derby:C:\tmp\testdb? Do the above lines for javax.persistence.schema-generation assume the database has been created?

Cannot get current session

`
classpath:database.properties
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<property name="packagesToScan" value="org.entity">
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<tx:annotation-driven/>
<!-- not working -->
<!-- <context:component-scan base-package="org.service"></context:component-scan> -->
`
this is my spring configuration file. Problem is that i am not getting current session in my repository classes which are marked with #Repository, I have annotated my service methods with #Transactional annotation.
However all the autowiring is working fine, the only problem is when i try to get current session in repository classes, and if I use bean tag to instantiate my repository and service beans it works. What am i doing wrong here?
Use the OpenSessionInView Filter or similar, that way you should have a Session available during the request lifecycle (Controller/ View rendering).

Reading Enviornment variables in Spring application context

I have three enviornment variables.
MY_TOPIC
MY_CONTEXT_FACTORY
MY_LDAP
First one - MY_TOPIC works like this
<bean id="myPublishTopic" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiTemplate">
<ref bean="jndiTemplate"/>
</property>
<property name="jndiName">
<value>fxClientDestinationUID=${MY_TOPIC}</value>
</property>
</bean>
Last two does not work reading from enviornment variables. How do it make this work?
<bean id="jndiTemplate" class="org.springframework.jndi.JndiTemplate">
<property name="environment">
<props>
<prop key="java.naming.factory.initial">${MY_CONTEXT_FACTORY}</prop>
<prop key="java.naming.provider.url">${MY_LDAP}</prop>
</props>
</property>
</bean>
Last two works only if i read it from properties file which i want to avoid.
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location"><value>/WEB-INF/classes/springConfig-devel.properties</value></property>
</bean>
If you are using Spring 3+ you can use Spring Expression Language.
Use #{systemEnvironment['NAME']} to access environment variables
<bean id="jndiTemplate" class="org.springframework.jndi.JndiTemplate">
<property name="environment">
<props>
<prop key="java.naming.factory.initial">#{systemEnvironment['MY_CONTEXT_FACTORY']}</prop>
<prop key="java.naming.provider.url">#{systemEnvironment['MY_LDAP']}</prop>
</props>
</property>
</bean>
Use #{systemProperties['value']} for Java system properties.

How to set properly the loader path of velocity

i would like that my velocityengine look for templates from a designed path.
i did this :
<bean id="velocityEngine" class="org.springframework.ui.velocity.VelocityEngineFactoryBean">
<property name="velocityProperties">
<value>
resource.loader=class
class.resource.loader.class=org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader
class.resource.loader.resourceLoaderPath=/mytemplates
</value>
</property>
but is still looking for templates in the classes folder.
any idea?
As illustrated in the spring documentation, you could try the following:
<bean id="velocityEngine" class="org.springframework.ui.velocity.VelocityEngineFactoryBean">
<property name="velocityProperties">
<props>
<prop key="resource.loader">file</prop>
<prop key="file.resource.loader.class">
org.apache.velocity.runtime.resource.loader.FileResourceLoader
</prop>
<prop key="file.resource.loader.path">${webapp.root}/WEB-INF/velocity</prop>
<prop key="file.resource.loader.cache">false</prop>
</props>
</property>
</bean>
Alternately, you could declare these properties in a velocity.properties and specify that
<bean id="velocityEngine" class="org.springframework.ui.velocity.VelocityEngineFactoryBean">
<property name="configLocation" value="/WEB-INF/velocity.properties"/>
</bean>
Try this:
<bean id="velocityEngine" class="org.springframework.ui.velocity.VelocityEngineFactoryBean">
<property name="resourceLoaderPath" value="/email_templates/"/>
</bean>
<bean name="mailTest" class="com.crisil.web.MailTestController">
<property name="velocityEngine" ref="velocityEngine"/>
</bean>
Try with Resource Loader as
org.apache.velocity.runtime.resource.loader.FileResourceLoader
Hope this helps.

Resources