Spring管理hibernate
(a)配置spring数据源
hibernate推荐: c3p0连接池
spring官方推荐使用连接池:DBCP连接池
(b)添加DBCP连接池类库
commons-pool.jar
commons-dbcp.jar
(c)连接池类:
BasicDataSource.java
(d)在spring配置文件中配置连接池
initialSize :连接池启动时创建的初始化连接数量(默认值initialSize :连接池启动时创建的初始化连接数量(默认值为0)
maxActive :连接池中可同时连接的最大的连接数(默认值为8 ,调整为20,高峰单机器在20并发左右,自己根据应用场景定)
maxIdle: 连接池中最大的空闲的连接数,超过的空闲连接将被释放,如果设置为负数表示不限制(默认为8个)
minIdle:连接池 中最小的空闲的连接数,低于这个数量 会被创建新的连接(默认为0,调整为5,该参数越接近maxIdle,性能越好,因为连接的创建和销 毁,都是需要消耗资源的,但是不能太大,因为在机器很空闲的时候,也会创建低于minidle个数的连接)
maxWait :最大等待时间,当没有可用 连接时,连接池等待连接释放的最大时间,超过该时间限制会抛出异常,如果设 置-1表示无限等待(默认为无限, 调整为60000ms,避免因线程池不够用,而导致 请求被无限制挂起)
removeAbandonedTimeout :超过时间限制,回收没有用(废弃)的连接 (默认为 300秒,调整为180)
removeAbandoned :超过 removeAbandonedTimeout时间后,是否进行没用连接(废弃)的回收(默认为 false,调整为true)
注意:连接池的大小需要根据压力测试报告信息来设置
在spring配置里完全管理hibernate配置文件:
删除hibernate.cfg.xml文件
删除hibernate.cfg.xml文件
<!-- spring管理hibernate名字必须使用:sessionFactory-->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!-- spring管理数据源 -->
<property name="dataSource" ref="dataSource" />
<!-- spring管理hibernate属性 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<!-- 管理orm映射文件 -->
<property name="mappingLocations">
<list>
<value>
classpath:config/hibernate/hbm/User.hbm.xml
</value>
</list>
</property>
</bean>
spring Test 使用注解方式
(a)添加spring 测试类库
spring-test.jar, junit-4.4.jar
注意: 本版本只支持spring2.5.6
(b)参看UserDaoImplTest.java类
//添加测试类
@RunWith(SpringJUnit4ClassRunner.class)
//加载spring容器中的对象
@ContextConfiguration(locations = { "classpath:config/spring/applicationContext.xml" })
//set注入:相当于显示提供set方法
@Autowired
private IUserDao iuserDao;
注解优势:编写方便
缺点:注解依赖代码类,后期维护需要修改代码,还需要重新对java类进行编译和大量测试, 注解目前不成熟和稳定,版本变化比较快,存在兼容性问题
建议:在测试代码中可以使用注解
分层管理spring配置文件
applicationContext.xml:spring公共信息
applicationContext-web.xml管理struts2的action
applicationContext-biz.xml管理业务层的Bean
applicationContext-dao.xml管理数据访问层Bean
方式一引入:
在applicationContext.xml中引入其他spring配置文件
<import resource="applicationContext-web.xml"/>
<import resource="applicationContext-biz.xml"/>
<import resource="applicationContext-dao.xml"/>
方式二引入:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
<!--可以使用通配符号*-->
classpath:config/spring/applicationContext*.xml
</param-value>
</context-param>
spring如何解决hibernate延迟问题
以前解决延迟:
(1)Lazy不使用延迟,让hibernate立即加载
(2)自定一个拦截器或者过滤器
(3)可以使用spring内部提供openSessionInViewFilter来解决懒加载问题
(a)在web.xml文件中配置过滤器
<filter>
<filter-name>openSessionInViewFilter</filter-name>
<filter-class>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
</filter-class>
<init-param>
<!--openSessionInViewFilter默认数据源名字叫sessionFactory
如果数据源名字不叫sessionFactory,必须指定 sessionFactoryBeanName的value值-->
<param-name>sessionFactoryBeanName</param-name>
<param-value>sessionFactory</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>openSessionInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
spring中文过滤器
(1)自定一个过滤器
(2)struts2
(3)其他框架(除struts2框架)
使用spring过滤器,在web.xml文件中配置filter
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>
org.springframework.web.filter.CharacterEncodingFilter
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
spring读取属性文件
(1)注入spring读取属性类(appliactionContext.xml)
<bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:config/props/db.properties</value>
</list>
</property>
</bean>
(2)创建config/props/db.properties文件
(3)通过key==属性文件中(value)执行
${key}=value