Spring基于Annotation为dao装配sessionFactory的问题
2012-05-05 15:27
399 查看
先来看applicationContext.xml中的配置
下面是Dao实现类。
@Repository("userDao")
public class UserDaoImpl extends HibernateDaoSupport implements IUserDao
由于继承了HibernateDaoSupport类。 需要为其注入sessionFactory。
由于我们要用注解为dao注入sessionFactory。
这里有两种解决方案。
第一种是applicationContext.xml中加入 default-autowire="byName" 默认按照名字注入
第二种是是在UserDaoImpl 实现类中加入以下代码:
说明一下这里代码的意思。
UserDaoImpl有个mySessionFactory属性。
在启动项目的时候spring容器会在我们配置的applicationContext.xml中实例化sessionFactory实例。
通过Autowired我们为自己的mySessionFactory注入了sessionFactory实例。
@PostConstruct的意思是在Bean初始化之后被Spring容器执行。(Bean初始化包括实例化Bean和装配Bean的属性。)
因此在调用super.setSessionFactory(mySessionFactory)的时候Bean中的mySessionFactory已经被装配。
所以最终为dao注入了sessionFactory!
<?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!
相关文章推荐
- Spring 基于 annotation 的自动装配
- Spring-Bean之基于注解(Annotation)装配解析
- spring基础-Bean的装配方式(二)基于Annotation的配置
- Spring容器拆分、自动装配和依赖检查机制,基于注解的sessionFactory
- Spring3与Hibernate4整合,Spring中配置Hibernate基于XML和annotation的sessionFactory方式。
- hibernate annotation+spring 级联更新问题解决
- Spring中JdbcDaoSupport的DataSource注入问题
- 8.4.5: Spring的AOP---基于Annotation的零配置方式
- Spring基于XML和annotation的配置
- Struts2.1&Hibernate3.2&Spring2.5集成[基于Annotation]--代码
- 在SSH框架下使用基于配置的Spring3的调度任务每次执行2次的问题
- 基于Spring、Hibernate的通用DAO层与Service层的实现
- Spring基于 Annotation 的简单介绍
- Hibernate基于Spring模板的DAO基类
- 处理问题的思路,基于SSM项目(spring,springmvc,mybatis)
- 【JavaWeb-23】spring、IoC控制反转和DI依赖注入入门、基于XML的Bean装配、基于注解的Bean装配
- Spring中AOP基于Annotation配置常用声明
- idea关于找不到包的问题,比如:Java:程序包org.springframework.beans.factory.annotation不存在
- Spring AOP使用(基于Annotation)
- mapper注入失败问题解决 {@org.springframework.beans.factory.annotation.Autowired(required=true)}