现在的位置: 首页 > 综合 > 正文

Spring基于Annotation为dao装配sessionFactory的问题

2014年02月07日 ⁄ 综合 ⁄ 共 3137字 ⁄ 字号 评论关闭

先来看applicationContext.xml中的配置

<?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:tx="http://www.springframework.org/schema/tx"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="

http://www.springframework.org/schema/beans


http://www.springframework.org/schema/beans/spring-beans-3.0.xsd


http://www.springframework.org/schema/tx


http://www.springframework.org/schema/tx/spring-tx-3.0.xsd


http://www.springframework.org/schema/context


http://www.springframework.org/schema/context/spring-context-3.0.xsd


http://www.springframework.org/schema/aop

          http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
    
          
    <context:property-placeholder location="classpath:/hibernate.properties" />
          
	<bean id="dataSource"
		class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName" value="${dataSource.driverClassName}" />
		<property name="url" value="${dataSource.url}" />
		<property name="username" value="${dataSource.username}" />
		<property name="password" value="${dataSource.password}" />
	</bean>
	
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
		
		<property name="dataSource" ref="dataSource" />
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">${dataSource.dialect}</prop>
				<prop key="hibernate.hbm2ddl.auto">${dataSource.hbm2ddl.auto}</prop>
				<prop key="hibernate.hbm2ddl.auto">update</prop>
			</props>
		</property>
		<property name="packagesToScan">
			<list>
				<value>com.dhy.format.bbs.entity</value>
			</list>
		</property>
	</bean>
	
	
	
	<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>

	<aop:config>
		<aop:advisor pointcut="execution(* com.dhy.format.bbs.service.*Service.*(..))" advice-ref="txAdvice"/>
	</aop:config>

	<tx:advice id="txAdvice" transaction-manager="txManager">
		<tx:attributes>
			<tx:method name="get*" read-only="true"/>
			<tx:method name="query*" read-only="true"/>
			<tx:method name="find*" read-only="true"/>
			<tx:method name="load*" read-only="true"/>
			<tx:method name="*" rollback-for="Exception"/>
		</tx:attributes>
	</tx:advice>
	
</beans>

下面是Dao实现类。

@Repository("userDao")

public class UserDaoImpl extends HibernateDaoSupport implements IUserDao

由于继承了HibernateDaoSupport类。 需要为其注入sessionFactory。

由于我们要用注解为dao注入sessionFactory。

这里有两种解决方案。

第一种是applicationContext.xml中加入 default-autowire="byName" 默认按照名字注入

第二种是是在UserDaoImpl 实现类中加入以下代码:

private SessionFactory mySessionFactory;

	@Autowired
	@Qualifier("sessionFactory")
	public void setMySessionFactory(SessionFactory mySessionFactory) {
		this.mySessionFactory = mySessionFactory;
	}
	
	@PostConstruct
	public void setSuperSessionFactory() {
		super.setSessionFactory(mySessionFactory);
	}

说明一下这里代码的意思。 

UserDaoImpl有个mySessionFactory属性。

在启动项目的时候spring容器会在我们配置的applicationContext.xml中实例化sessionFactory实例。

通过Autowired我们为自己的mySessionFactory注入了sessionFactory实例。

@PostConstruct的意思是在Bean初始化之后被Spring容器执行。(Bean初始化包括实例化Bean和装配Bean的属性。)

因此在调用super.setSessionFactory(mySessionFactory)的时候Bean中的mySessionFactory已经被装配。

所以最终为dao注入了sessionFactory!

抱歉!评论已关闭.