Setting spring bean property value using ref-bean - java

I am trying to set a property value using spring.
<bean id="velocityPropsBean" class="com.test.CustomProperties" abstract="false" singleton="true" lazy-init="false" autowire="default" dependency-check="default">
<property name="properties">
<props>
<prop key="resource.loader">file</prop>
<prop key="file.resource.loader.cache">true</prop>
<prop key="file.resource.loader.class">org.apache.velocity.runtime.resource.loader.FileResourceLoader</prop>
<prop key="file.resource.loader.path">NEED TO INSERT VALUE AT STARTUP</prop>
</props>
</property>
</bean>
<bean id="velocityResourcePath" class="java.lang.String" factory-bean="velocityHelper" factory-method="getLoaderPath"/>
Now what i need to do is insert the result from getLoaderPath into file.resource.loader.path. The value of getLoaderPath changes so it has to be loaded at server startup.
Any thoughts how i can inset the velocityResourcePath value to the property?

Using Spring 3, you can skip the middle stage, and invoke the factory directly using SpringEL:
<prop key="file.resource.loader.path">#{ velocityHelper.loaderPath }</prop>
or perhaps
<prop key="file.resource.loader.path">#{ velocityHelper.getLoaderPath() }</prop>
This would let you remove the velocityResourcePath bean.

Below code might help you.
<import resource="classpath:/DaoContext.xml"/>
<bean id="ClientMasterDao" class="dao.hibernate.impl.ClientMasterImpl">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="ClientMasterServices" class="client.ClientMasterServices">
<property name="clientDao" ref="ClientMasterDao"/>
</bean>

Related

Is it possible to reuse context-property placeholder as a supplier for an attribute that expects a <props> list?

I'm defining my text properties by means of
<context:property-placeholder location="application.properties"/>
and then, I got something like
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" p:jpaProperties-ref="*****"/>
I like to feed a reference to the context property-placeholder, instead of using
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="...
Is it possible?
Yeah, you can use PropertiesFactoryBean like this:
<bean id="jpaProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="location" value="classpath:/jpa.properties"/>
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" p:jpaProperties-ref="japProperties"/>

How to create a hibernate session bean for both Annotated and hbm.xml configured entities

I have legacy hbm.xml configured persistent classes and Annotated persistent classes, so currently we have to specify per which session factory is being used in the DAO bean.
Unfortunately, that means that I'm running into an issue where we have a mix of DAO's that I'd like to use, aren't all associated to a single session factory.
I'd like to combine both into a single session factory bean since we can't just move everything over all at once.
How would I go about doing this?
Note: my current workaround is to Annotate some of the xml configured ones and then create two DAO beans: one for each Session Factory.
HBM.XML:
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mappingLocations">
<value>hbm/path/*.hbm.xml</value>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
<prop key="hibernate.show_sql">false</prop>
</props>
</property>
</bean>
Annotated:
<bean id="annotatedSessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan" value="path.batch.persistent"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
<prop key="hibernate.show_sql">false</prop>
</props>
</property>
</bean>
Thanks M.Deinum and orid:
I was overthinking this.
There was never a need for two session factories.
So I just had to refactor the context file to:
<bean id="annotatedSessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan" value="path.batch.persistent"/>
<property name="mappingLocations">
<value>hbm/path/*.hbm.xml</value>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
<prop key="hibernate.show_sql">false</prop>
</props>
</property>
</bean>

Using Spring Util Schema to Override Bean Settings In App Context

First: yes, I've seen the docs.
They tell me how to create a util:list, util:set, etc. I get that part.
However, I have a library with an application context that contains a bean (specifically a Hibernate Session Factory bean) with settings I'd like the option of overriding. Several services use this library, not every service needs the same annotated classes.
The session factory bean currently looks something like this:
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop>
<prop key="hibernate.jdbc.use_get_generated_keys">true</prop>
<prop key="hibernate.cglib.use_reflection_optimizer">true</prop>
</props>
</property>
<property name="annotatedClasses" >
<list>
<value>com.example.model.Person</value>
<value>com.example.model.Section</value>
</list>
</property>
</bean>
I would like to replace the annotatedClasses property with a list defined like this (in the app context of the service using the library):
<util:list id="serviceSpecificAnnotatedClasses">
<value>com.example.model.Person</value>
<value>com.example.model.Section</value>
<value>com.example.model.Location</value>
</util:list>
Do I simply have to name the util:list "annotatedClasses" and it will be automatically overriden?
No it wont automatically be overriden. You would have to declare the bean and wire it by default.
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop>
<prop key="hibernate.jdbc.use_get_generated_keys">true</prop>
<prop key="hibernate.cglib.use_reflection_optimizer">true</prop>
</props>
</property>
<property name="annotatedClasses" ref="annotatedClasses" />
</bean>
<util:list id="annotatedClasses">
<value>com.example.model.Person</value>
<value>com.example.model.Section</value>
<value>com.example.model.Location</value>
</util:list>
No others can simply override the list annotatedClasses.
But why not simply use a property-placeholder to specify the classes and add a comma delimited list to a properties file?
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop>
<prop key="hibernate.jdbc.use_get_generated_keys">true</prop>
<prop key="hibernate.cglib.use_reflection_optimizer">true</prop>
</props>
</property>
<property name="annotatedClasses" value="${service.annotatedClasses}" />
</bean>
Assuming that each service has its own property file for configuration they simply need to add the value for service.annotatedClasses.

Hibernate doesn't start

I have the following hibernate config on spring and the server starts after a long time but doesn't connect to DB (no schema on DB). So It was supose to give a error message or create the schema with <prop key="hibernate.hbm2ddl.auto">create</prop>.
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method = "close">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql://dburl:3306"/>
<property name="user" value="user"/>
<property name="password" value="pass!"/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="mappingResources">
<list>
<value>waf/resources/User.hbm.xml</value>
<value>waf/resources/Post.hbm.xml</value>
<value>waf/resources/Position.hbm.xml</value>
<value>waf/resources/Comment.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">validate</prop>
<!-- C3P0 CONNECTION POOL -->
<prop key="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</prop>
<prop key="c3p0.acquire_increment">1</prop>
<prop key="c3p0.idle_test_period">100</prop>
<prop key="c3p0.max_size">20</prop>
<prop key="c3p0.max_statements">50</prop>
<prop key="c3p0.min_size">1</prop>
<prop key="c3p0.timeout">10</prop>
</props>
</property>
<property name="dataSource">
<ref bean="dataSource"/>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
Can you guys help me out?
Hibernate does not create schemas with hbm2ddl.auto. It just creates | creates-drop | etc tables.
Going through the HBM files, you have given as below..
Validate instead of Create
I hope this will not create the DDL. can you please check that? Or is it a typo in question?
Adding to what others have proposed if you are using connection pool (which you are as c3po is mentioned) then while the session Factory is created it will try to use the connection pool backed datasource which in turn will connect to database to pre-create and pool connection. When you say that it does not connect to database - how do you know that ? Is there error in logs? I have seen that if Hibernate session factory is not able to configure itself it throws an error in the logs.

JMS template credentials default value

Hello everyone I'm trying to work with jboss messaging, does anyone knows the default value for these java.naming.security.principal and java.naming.security.credentials or how can I set them?
<bean id="jndiTemplate" class="org.springframework.jndi.JndiTemplate">
<property name="environment">
<props>
<prop key="java.naming.factory.initial">org.jnp.interfaces.NamingContextFactory</prop>
<prop key="java.naming.provider.url">jnp://localhost:8080</prop>
<prop key="java.naming.factory.url.pkgs">org.jboss.naming:org.jnp.interfaces</prop>
<prop key="java.naming.security.principal">value</prop>
<prop key="java.naming.security.credentials">value</prop>
</props>
</property>
</bean>
I'm trying to instansiate the <bean id="connectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean"> I'm guessing that this is the cause why jboss timeouts when starts
Its probably admin/admin or guest/guest

Categories