【j2ee spring】10、整合SSH框架(3)

整合SSH框架(3)

Spring4+hibernate4+Struts2的整合,整合完成后我会把这个项目上传上去,但是我的建议是最好还是自己在自己的电脑上自己整合一下,我不保证一定没问题

前面那个,我们已经基本整合了SSH框架,但是还是有一些小小的瑕疵,

比如:PersonAction.java里面的

//获取实例,方法1ServletContext sc = ServletActionContext.getRequest().getSession().getServletContext();WebApplicationContext wac = (WebApplicationContext) sc.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);//方法2WebApplicationContext webApplicationContext = WebApplicationContextUtils.getRequiredWebApplicationContext(ServletActionContext.getServletContext());if(wac == webApplicationContext){System.out.println("ok!!!");}PersonService personService = (PersonService) wac.getBean("personServiceBean"); 这个,得到ServletContext每次都要写的话,会很麻烦,以后如果获取的bean改了的话,更麻烦,所以这里改进一下,我们用spring的依赖注入的方式获取personService类

1、在PersonAction类里面添加数据成员

@Resource private PersonService personService;//先按名字注入,如果找不到的话就按类型注入用注解的方式获取这个类

2、吧action交个spring管理

要用spring的依赖注入的话,我们就得把action交给spring管理了

在spring中添加

<bean id="PersonAction" class="cn.cutter_point.web.action.PersonAction" autowire="byName" />

3、这里我们为了提高数据库的利用效率,这里得设置数据库的二级缓存

spring中修改bean

<!– hibernate二级缓存的配置 –><bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"><!– configuration elided for brevity –><property name="dataSource" ref="myDataSource" /><property name="mappingResources"><list><!– 映射文件 –><value>cn/cutter_point/bean/Person.hbm.xml</value></list></property><property name="hibernateProperties"><!– 用来配置hibernate的属性配置 –><value>hibernate.dialect=org.hibernate.dialect.MySQLDialecthibernate.hbm2ddl.auto=update <!–其他取值 create、create-drop、update、validate none –>hibernate.show_sql=truehibernate.format_sql=true<!– 开启二级缓存功能 –>hibernate.cache.use_second_level_cache = truehibernate.cache.use_query_cache = falsehibernate.cache.region.factory_class = org.hibernate.cache.ehcache.EhCacheRegionFactory<!– hibernate3的二级缓存配置 –><!– <property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property> –></value></property></bean>注意,我使用的是hibernate4,4和3有些不一样,4中没有org.hibernate.cache.EhCacheProvider这个玩意,并且我查找官方文档的时候,坑爹的是文档里面写的还是hibernate.cache.provider_class这个属性,最后只能问问度娘,才发现官方文档哪里是有错的,没有改过来,但是使用的jar包已经改了。

!!!!!!!!

我勒个擦,感情这个官方文档就是给我们看的,官方的人都是不看的啊

加入几个jar包

这三个一个都别想偷懒了,都得加,我是亲身经历了的,实在懒得动结果一个一个地加,反而更麻烦,早知道管他什么鸡巴毛,一把抓进去好了

4、我们配置一下二级缓存的xml文件

ehcache.xml

<!– ~ Hibernate, Relational Persistence for Idiomatic Java ~ ~ Copyright (c) 2007, Red Hat Middleware LLC or third-party contributors as ~ indicated by the @author tags or express copyright attribution ~ statements applied by the authors. All third-party contributions are ~ distributed under license by Red Hat Middleware LLC. ~ ~ This copyrighted material is made available to anyone wishing to use, modify, ~ copy, or redistribute it subject to the terms and conditions of the GNU ~ Lesser General Public License, as published by the Free Software Foundation. ~ ~ This program is distributed in the hope that it will be useful, ~ but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY ~ or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License ~ for more details. ~ ~ You should have received a copy of the GNU Lesser General Public License ~ along with this distribution; if not, write to: ~ Free Software Foundation, Inc. ~ 51 Franklin Street, Fifth Floor ~ Boston, MA 02110-1301 USA –><ehcache><!– Sets the path to the directory where cache .data files are created.If the path is a Java System Property it is replaced byits value in the running VM.The following properties are translated:user.home – User's home directoryuser.dir – User's current working directoryjava.io.tmpdir – Default temp file path –><!– 缓存文件存放到硬盘哪里 –><diskStore path="G:\Workspaces\MyEclipse Professional 2014\ssh\cacheFile" /><!–Default Cache configuration. These will applied to caches programmatically created throughthe CacheManager.The following attributes are required for defaultCache:maxInMemory- Sets the maximum number of objects that will be created in memoryeternal- Sets whether elements are eternal. If eternal, timeouts are ignored and the elementis never expired.timeToIdleSeconds – Sets the time to idle for an element before it expires. Is only usedif the element is not eternal. Idle time is now – last accessed timetimeToLiveSeconds – Sets the time to live for an element before it expires. Is only usedif the element is not eternal. TTL is now – creation timeoverflowToDisk – Sets whether elements can overflow to disk when the in-memory cachehas reached the maxInMemory limit.–><!–defaultCache节点为缺省的缓存策略maxElementsInMemory 内存中最大允许存在的对象数量eternal 设置缓存中的对象是否永远不过期overflowToDisk 把溢出的对象存放到硬盘上timeToIdleSeconds 指定缓存对象空闲多长时间就过期,过期的对象会被清除掉timeToLiveSeconds 指定缓存对象总的存活时间diskPersistent 当jvm结束是是否持久化对象diskExpiryThreadIntervalSeconds 指定专门用于清除过期对象的监听线程的轮询时间 –><defaultCachemaxElementsInMemory="10000"eternal="false"timeToIdleSeconds="120"timeToLiveSeconds="120"overflowToDisk="true"/><!– 特殊配置项目 –><cache name="cn.cutter_point.bean.Person" maxElementsInMemory="100" eternal="false" timeToIdleSeconds="300" timeToLiveSeconds="600"overflowToDisk="true" /></ehcache>不要问我为什么英语突然变那么好了,,拷贝官方的,你懂的

5、修改一下Person.hbm.xml文件强者能同命运的风暴抗争。

【j2ee spring】10、整合SSH框架(3)

相关文章:

你感兴趣的文章:

标签云: