Just as an example of generating a dao-specific applicationContext file:
Code:
<#--
SPRING 2.0 CONFIGURATION FILE TEMPLATE FOR HIBERNATE TOOLS
CREATED: 2006-08-14
PURPOSE: To allow Hibernate Tools to generate a Spring Framework configuration file
RESTRICTIONS:
Spring 2.x declarative transaction style only
ASSUMPTIONS:
Mapping files (*.hbm.xml) files are in the same package as the POJO's
PARAMETERS (name, required, default, valid values):
default_autowire, required: no, default: none, valid: autodetect, byName, byType, constructor, no
- sets the default-autowire attribute on the beans tag if a valid value
default_lazy_init, required: no, default: none, valid: true, false
- sets the default-lazy-init attribute on the beans tag if true
default_merge, required: no, default: none, valid: true, false
- sets the default-merge attribute on the beans tag if true
default_init_method, required: no, default: none, valid: any string
- sets the default-init-method attribute on the beans tag if specified
default_destroy_method, required: no, default: none, valid: any string
- sets the default-destroy-method attribute on the beans tag if specified
sessionfactory_id, required: no, default: "sessionFactory", valid: any string
- sets the Hibernate session factory bean identifier
dao_beans, required: no, default: false, valid: true, false
- if true, will generate bean definitions for DAO classes
dao_id_pattern, required: no, default: empty string, valid: any string
- the bean identifier for each bean, can use {class-name} and {class-name-lowercamel}, each
of which will be replaced by the POJO class name (in lower camel case for the latter).
An empty string will result in no identifier being configured (bean can be referenced by
by FQDN)
dao_class_pattern, required: yes, if dao_beans is true, default: nonsensical string, valid: any string
- a pattern for the java class corresponding to the dao bean implementation, can use {class-name}
and {class-name-lowercamel) as above
additional_bean_data, required: no, default: none, valid: any string
- any additional bean data to be added at the end of the file (note: must be escaped
in the ant build file)
-->
<?xml version="1.0" encoding="utf-8"?>
<#-- ************************************************************** -->
<#-- ************** ROOT LEVEL CONFIGURATION ********************** -->
<#-- ************************************************************** -->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/aop/spring-jee.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"
<#-- autowire the sessionFactory into the DAO beans -->
default-autowire="byName"
<#-- Conditionally add default values to the beans node -->
<#if default_lazy_init?? && default_lazy_init?is_boolean>
default-lazy-init="${default_lazy_init?string}"
</#if>
<#if default_merge?? && default_merge?is_boolean>
default-merge="${default_merge?string}"
</#if>
<#if default_init_method??>
default-init-method="${default_init_method}"
</#if>
<#if default_destroy_method??>
default-destroy-method="${default_destroy_method}"
</#if>
>
<#-- ************************************************************** -->
<#-- ********************** DAO BEANS ***************************** -->
<#-- ************************************************************** -->
<#assign sessionfactory_id = sessionfactory_id!"sessionFactory">
<#assign dao_beans = dao_beans!false>
<#if dao_beans>
<#assign dao_id_pattern = dao_id_pattern!"">
<#assign dao_class_pattern = dao_class_pattern!"org.replaceme.with.a.real.class.pattern.{class-name-lowercamel}Dao">
<!-- DAO Beans -->
<#foreach entity in c2j.getPOJOIterator(cfg.getClassMappings())>
<#if dao_id_pattern == "">
<#assign id_phrase = "">
<#else>
<#assign id_name = dao_id_pattern?replace("{class-name}", entity.getShortName())?replace("{class-name-lowercamel}", entity.getShortName()?uncap_first)>
<#assign id_phrase = "id=\"${id_name}\" ">
</#if>
<bean ${id_phrase}class="org.package.test.framework.dao.hibernate.BaseHibernateDao" >
<property name="persistentClass" value="${entity.qualifiedDeclarationName}"/>
</bean>
</#foreach>
</#if>
<#-- ************************************************************** -->
<#-- ******************* ADDITIONAL BEANS ************************* -->
<#-- ************************************************************** -->
<#if additional_bean_data??>
<!-- Additional Beans -->
${additional_bean_data}
</#if>
</beans>
It generates this:
Code:
<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/aop/spring-jee.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"
default-autowire="byName" default-lazy-init="false" default-merge="false">
<!-- DAO Beans -->
<bean id="referralSourceDao" class="org.package.test.framework.dao.hibernate.BaseHibernateDao">
<property name="persistentClass" value="org.package.test.framework.model.ReferralSource" />
</bean>
<bean id="participantRelationshipDao" class="org.package.test.framework.dao.hibernate.BaseHibernateDao">
<property name="persistentClass" value="org.package.test.framework.model.ParticipantRelationship" />
</bean>
...326 additional beans defined, I have 328 model classes in this project...
</beans>
Note that the generation of this Spring configuration file is highly specific to the application's architecture. I use a BaseHibernateDao which encompasses all basic DAO functionality given a specific model class, which is setter-injected by the above file. The Hibernate SessionFactory is injected byName from another Spring configuration file (applicationContext-hibernate.xml).