我使用了ssh2来整合,并且想达到零配置的目的,不要xml中写bean,也不要hibernate的实体与表的映射文件。
于是我打算使用spring的注解,和hibernate的注解来实现.
struts2+spring这里省去.
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-2.5.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-2.5.xsd">
    <context:component-scan base-package="com.gy_center"></context:component-scan>   
<!-- 连接SQL Server-->
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName"
value="com.microsoft.sqlserver.jdbc.SQLServerDriver">
</property>
<property name="url"
value="jdbc:sqlserver://localhost:1433;databaseName=gycenter">
</property>
<property name="username" value="sa" />
<property name="password" value="icenter" />
<property name="maxActive" value="300" />
<property name="maxWait" value="300" />
</bean> <!-- session工厂 -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource">
 <ref local="dataSource"/>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.SQLServerDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="packagesToScan"><!--配置扫描实体po-->
<list>
<value>com.gy_center.model</value>
</list>
</property>
</bean> <!-- 事务管理器-->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 事务特性 方法的事务隔离级别 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />

</tx:attributes>
</tx:advice>
<!-- 事务作用的层   -->
<aop:config>
    <aop:pointcut id="allManagerMethod" expression="execution(* com.gy_center.service..*(..))"/>
    <aop:advisor advice-ref="txAdvice" pointcut-ref="allManagerMethod"/>
    </aop:config>
 
  </beans>
下面是hibernate:(DAo层)
  @Repository
public class SysUserDAO  implements ISysUserDao {
 @Resource(name="sessionFactory")
         private SessionFactory sessionFactory;
        .....................
}
我不想使用spring提供的hibernateTample模板。于是想把sessionFactory注入进来
Dao如上。
但是程序运行抱出一个错误:
 No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here
说明没有注入进来?请问为什么?我记得使用@Resource注解是可以不需要set方法的。我加了set方法测试还是这个错误。
是不是我的SessionFactory 工厂不能使用AnnotationSessionFactoryBean。如果不使用这个,那么下面的packagesToScan,就无法扫描po实体了。s2shHibernateSpringBean