1)Hibernate + Spring 将hibernate 配置文件全部 整合到 Spring 配置文件中
a)整合数据源 可以通过 属性文件
jdbc.driver_class=com.mysql.jdbc.Driver
jdbc.connection_url=jdbc:mysql://localhost:3306/springdata
jdbc.username=root
jdbc.password=sa
属性文件在spring 配置文件配置
<bean id="db"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations"> <list>
<value>classpath*:database.properties</value>
</list>
</property>
</bean>
根据属性文件得到数据源
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driver_class}"></property>
<property name="url" value="${jdbc.connection_url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
b)创建sessionFactory
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean ">
<property name="configLocation"
value="classpath:hibernate.cfg.xml">//这里面包含了实体类的配置文件 也可以不这个配置文件 直接配置在spring配置中
</property>
<!-- Spring 中配置hibernate 注解的实体类
<!--
<property name="annotatedClasses">
<list>
<value></value>
</list>
</property>
-->
<property name="packagesToScan">
<list>
<value>实体类的包</value>
</list>
</property>
-->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
</props>
</property>
<!-- 添加数据库连接 -->
<property name="dataSource">
<ref local="dataSource" />
</property>
</bean>
Hiberante xml 配置
<bean class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="myDataSource"/>
<property name="mappingResources">
<list>
<value>product.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<value>
hibernate.dialect=org.hibernate.dialect.HSQLDialect
</value>
</property>
</bean>
c)Spring Hibernate 声明事物管理
<tx:advice id="studentadvice" transaction-manager="txmanger">
<tx:attributes>
<tx:method name="*"/>
</tx:attributes>
</tx:advice>
<bean id="txmanger" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<aop:config>
<aop:pointcut expression="execution(* com.hibernate.server.*.*(..))" id="studentpointcut"/>
<aop:advisor advice-ref="studentadvice" pointcut-ref="studentpointcut"/>
</aop:config>
d)使用hibernateTemplate 进行hibernate 操作
<bean id="hibernaterTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
2)Struts2 + Spring
a)application 启动是 加载Spring 的配置文件初始话Bean
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
b)添加 struts2-spring-plugin-2.2.3.1.jar 将action Bean的产生交给 Spring 容器 这样你就可以直接在action中注入Spring
容器中的Bean 了 struts2.xml 的配置还是跟以前的一样
struts的action在生成时跟配置文件中的bean并不是在同一个容器中的。其实action中你不用添加注解照样OK。
因为一切都是自动注入的,可以参考下文档,上面有解释的,autowire,默认是byName的。但是如果想由自己控制注入,
也可以实现。那就是给Action添加一个@Component注解,同时struts配置文件中class的配置名字跟注解的名字一致,这样,
注入的控制权就又回到了自己手里。