1. 在数据库持久层的hibernate弄好后,接下来使用spring 来托管Hibernate的SessionFactory。
java
为何要这么作呢?对于我来讲也是一个很模糊的概念。
mysql
前面已经使用单例模式来加载Hibernate,确保SessionFactory的构造只进行一次,由于SessionFactory的初始化是一个很复杂的过程,生成的代价比生成一个Session的代价要巨大不少。
web
而后咱们经过对HibernateUtil工具类来把SessionFactory来传进到Dao代码中,这是要手动管理的。
spring
按照Spring的说法就是,能够经过spring来使得接口与实现类的松散耦合,而Spring 经过依赖注入来达到这种松散耦合的效果。
sql
这是如今初步学习的感觉,后期的深刻学习后再来具体感觉一下。
数据库
2. 废话很少说,下面来把Spring加入到项目中。session
首先在maven中的pom.xml的dependencies节点加入dependency子节点添加类库的依赖
app
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.2.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring.orm</artifactId> <version>4.2.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.2.2.RELEASE</version> </dependency>
这里添加的类库有三个,一个是spring的框架类,另外一个是ORM的类库,使得spring能集成hibernate的核心代码,还有一个就是web的依赖类库。
框架
3. 接下来要作的就是编写spring的xml配置文件。下面示例模板maven
<?xml version="1.0" encoding="GBK"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!--这里是启用spring注解--> <context:annotation-config/> <!--这里是注册bean,托管于spring容器管理。这是数据库链接配置--> <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> <property name="url" value="jdbc:mysql://127.0.0.1:3306/tian"></property> <property name="username" value="tian"></property> <property name="password" value="tian8169"></property> </bean> <!--在这里把hibernate注册到spring中,让spring来初始化SessionFactory--> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <property name="configLocation" value="classpath:hibernate.cfg.xml"></property> </bean> <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"/> <property name="dataSource" ref="dataSource"></property> </bean> <bean id="userDaoImpl" class="cn.tian.dao.UserDaoImpl"></bean> </beans>
把这个xml放到WebContent下的WEB-INF目录下,spring就会自动找到这一份xml配置,固然,须要把这个文件命名为applicationContext.xml。具体的参考文档在这里
org.springframework.web.context.ContextLoader
的注释中找到。
那若是我不想放在默认的路径下,我把放到classpath下要怎么办呢?
其实在ContextLoader中的注释中也讲到了,只要在web.xml中添加一个<context-param>就能够自定义配置的路径以及文件名。一样,咱们也须要在web.xml添加一个监听器listener来启动spring
<!--其中前缀classpath:是指明这个配置文件在源码根目录下,若是是file:则是物理路径--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!--spring启动--> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!--启动spring日志服务,加载log4j.properties--> <listener> <listener-class> org.springframework.web.util.Log4jConfigListener </listener-class> </listener>
4. 而后,就只须要在UserDaoImpl中添加一个@AutoWired就OK了,代码以下
package cn.tian.dao; import java.io.Serializable; import java.util.ArrayList; import java.util.List; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; //导入注解 import org.springframework.beans.factory.annotation.Autowired; import cn.tian.vo.User; public class UserDaoImpl implements UserDao{ private SessionFactory factory; //此处注解 @AutoWired public RoleDaoImpl(SessionFactory sessionFactory){ this.factory = sessionFactory; } /* 数据库相关操做代码,省略 */ }
5. 固然,咱们还须要在spring的applicationContext.xml中添加<bean></bean>来告诉这里有这么一个bean须要你来管理。而后就能够直接使用UserDao接口了。