Spring Best Configuration

You might need this instant guide.

 

Transactions with Manager and Advice

<tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
        <tx:method name="get*" read-only="true" />
        <tx:method name="*" />
    </tx:attributes>
</tx:advice>
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>
 

Transactions with Pointcuts

<aop:config>
    <aop:pointcut id="projectServiceOperation" expression="execution(* <package>.*.*(..))" />
    <aop:advisor advice-ref="txAdvice" pointcut-ref="projectServiceOperation" />
</aop:config>
 

Hibernate Session Factory

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="mappingResources">
        <list>
            <value><classpath></value>
        </list>
    </property>
    <property name="mappingDirectoryLocations">
		<list>
			<value>classpath:hbm</value>
		</list>
	</property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
            <prop key="hibernate.generate_statistics">false</prop>
        </props>
    </property>
    <property name="dataSource">
        <ref bean="dataSource"/>
    </property>
    <property name="eventListeners">
        <map>
            <entry key="merge">
                <bean class="org.springframework.orm.hibernate3.support.IdTransferringMergeEventListener" />
            </entry>
        </map>
    </property>
</bean>

 

Hibernate DAO and Service

<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
    <property name="sessionFactory">
        <ref local="sessionFactory" />
    </property>
</bean>
<bean id="(dao)" class="(daoClass)">
    <property name="sessionFactory" ref="sessionFactory" />
</bean>
<bean id="(service)" class="(serviceClass)">
    <property name="hibernateTemplate" ref="hibernateTemplate" />
</bean>

Best Practice

<tx:advice id="txAdvice" transaction-manager="transactionManager">
    <tx:attributes>
		<tx:method name="*" propagation="NOT_SUPPORTED" read-only="true" />
	</tx:attributes>
</tx:advice>
<bean id="sessionFactory"
	class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
	<property name="hibernateProperties">
		<props>
			<prop key="hibernate.dialect">
				org.hibernate.dialect.SQLServerDialect
			</prop>
			<prop key="hibernate.connection.autocommit">false</prop>
			<prop key="hibernate.show_sql">false</prop>
		</props>
	</property>
	<property name="mappingDirectoryLocations">
		<list>
			<value>classpath:hbm</value>
		</list>
	</property>
	<property name="dataSource">
		<ref bean="dataSource" />
	</property>
	<property name="entityInterceptor">
		<ref local="hibernateInterceptor" />
	</property>
</bean>
<bean id="dataSource"
	class="org.springframework.jndi.JndiObjectFactoryBean">
	<property name="jndiName">
		<value>java:comp/env/jdbc/foo</value>
	</property>
</bean>
<bean id="transactionManager"
	class="org.springframework.orm.hibernate3.HibernateTransactionManager">
	<property name="sessionFactory">
		<ref bean="sessionFactory" />
	</property>
	<property name="nestedTransactionAllowed">
		<value>true</value>
	</property>
</bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
    <property name="sessionFactory">
	    <ref bean="sessionFactory" />
	</property>
	<property name="cacheQueries">
	    <value>true</value>
	</property>
</bean>
 

 

<TBC>

你可能感兴趣的:(spring,AOP,Hibernate,bean,orm)