Integrated SSH Framework (3)
SPRING4+HIBERNATE4+STRUTS2 integration, integration after the completion of the project I will upload it, but my advice is best or oneself in their own computer integration, I do not guarantee that there will be no problem
In front of that, we have basically integrated the SSH framework, but there are some minor flaws,
For example: Inside the Personaction.java
Get the instance, Method 1 ServletContext sc = servletactioncontext.getrequest (). GetSession (). Getservletcontext (); Webapplicationcontext WAC = (webapplicationcontext) sc.getattribute (webapplicationcontext.root_web_application_ Context_attribute); Method 2 Webapplicationcontext webapplicationcontext = webapplicationcontextutils. Getrequiredwebapplicationcontext (Servletactioncontext.getservletcontext ()); if (WAC = = Webapplicationcontext) { System.out.println ("OK!!!"); } Personservice Personservice = (personservice) wac.getbean ("Personservicebean");
This, get servletcontext every time to write, will be very troublesome, later if the bean to get changed, more trouble, so here to improve, we use spring's dependency injection way to get Personservice class
1. Adding data members to the Personaction class
@Resource Private Personservice personservice;//is injected first by name and injected by type if not found.
Get this class in an annotated way
2, bar action to a spring management
To use spring's dependency injection, we have to give the action to spring management.
Add in Spring
<bean id= "personaction" class= "cn.cutter_point.web.action.PersonAction" autowire= "ByName"/>
3, here we to improve the efficiency of database utilization, here to set the database level two cache
Modify Beans in Spring
<!--hibernate level two cache configuration--><bean id= "Sessionfactory" class= " Org.springframework.orm.hibernate4.LocalSessionFactoryBean "><!--configuration elided for brevity-->< Property Name= "DataSource" ref= "myDataSource"/><property name= "mappingresources" ><list><!--mapping file --><value>cn/cutter_point/bean/person.hbm.xml</value></list></property><property Name= "Hibernateproperties" ><!--the property configuration used to configure Hibernate--><value>hibernate.dialect= Org.hibernate.dialect.mysqldialecthibernate.hbm2ddl.auto=update <!--Other values Create, Create-drop, update, validate None-->hibernate.show_sql=truehibernate.format_sql=true<!--turn on level two cache function-->hibernate.cache.use_second_ Level_cache = Truehibernate.cache.use_query_cache = Falsehibernate.cache.region.factory_class = Org.hibernate.cache.ehcache.EhCacheRegionFactory <!--hibernate3 Two-level cache configuration-<!--<property Nam E= "Hibernate.cache.provider_class" >org.hibernate.cache.eHcacheprovider</property>-</value></property></bean>
Note that I'm using hibernate4,4 and 3 somewhat differently, There was no org.hibernate.cache.EhCacheProvider in 4, and when I looked for the official documentation, it was written in the document or Hibernate.cache.provider_class.
This attribute, finally can only ask degree Niang, only then found official document where is wrong, did not change over, but use of jar package has changed.
!!!!!!!!
I take a rub, the feelings of this official document is to show us, the official people are not looking at
Add a few jar packages
These three one do not want to lazy, all have to add, I was personally experienced, really lazy to move results a one to add, but more trouble, early know tube he what dick hair, cluth in good
4, we configure the level two cache XML file
Ehcache.xml
<!--~ Hibernate, relational persistence for idiomatic Java ~ ~ Copyright (c), Red Hat middleware LLC or third- Party contributors as ~ indicated by the @author tags or express copyright attribution ~ Statements applied by the Autho Rs. All third-party contributions is ~ distributed under license by Red Hat middleware LLC. ~ ~ This copyrighted material are 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 was distributed in the hope that it'll 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 ~ to more details. ~ You should has received a copy of the GNU Lesser general public License ~ along with this distribution; If not, write to: ~ Free Software Foundation, Inc. ~Wuyi Franklin Street, Fifth Floor ~ Boston, MA 02110-1301 USA--><ehcache> <!--sets the path to the dire Ctory where cache. data files are created. If the path is a Java System, the It is replaced by its value in the running VM. The following properties are Translated:user.home-user ' s home directory User.dir-user's current Worki ng Directory Java.io.tmpdir-default temp file path--<!--cache files to hard drive where--<diskstore PA Th= "G:\Workspaces\MyEclipse Professional 2014\ssh\cachefile"/> <!--Default Cache configuration. These would applied to caches programmatically created through the CacheManager. The following attributes is required for defaultcache:maxinmemory-sets the maximum number of objects that 'll is created in memory eternal-sets whether elements is eternal. If Eternal, timeouts is ignored and the element is never expired. Timetoidleseconds-sets the time to idle for an element before it expires. Was only used if the element was not eternal. Idle time is now-last accessed time timetoliveseconds-sets the time-to-live for a element before it expires. Was only used if the element was not eternal. TTL is now-creation time overflowtodisk-sets whether elements can overflow to disk time the In-memory cache has reached the maxinmemory limit. -<!--defaultcache node is the default cache policy maxelementsinmemory the maximum allowable number of objects in memory eternal sets whether objects in the cache will never expire ov Erflowtodisk the overflow object to the hard disk timetoidleseconds the specified cache object is idle for an extended period of time, the expired object is erased timetoliveseconds the total lifetime of the specified cache object diskpers Istent the end of the JVM is whether to persist the object diskexpirythreadintervalseconds specifies the polling time of the listener thread dedicated to purging expired objects--<defaultcache Maxelem Entsinmemory= "10000" eternal= "false" timetoidleseconds= "120"Timetoliveseconds=" overflowtodisk= "true"/> <!--Special Configuration items--<CA Che name= "Cn.cutter_point.bean.Person" maxelementsinmemory= "eternal=" false "timetoidleseconds=" 300 " timetoliveseconds= "overflowtodisk=" "true"/> </ehcache>
Don't ask me why English has suddenly become so good, copy the official, you know
5, modify the Person.hbm.xml file
<?xml version= "1.0" encoding= "UTF-8"? ><! DOCTYPE hibernate-mapping Public "-//hibernate/hibernate mapping DTD 3.0//en" "http://www.hibernate.org/dtd/ Hibernate-mapping-3.0.dtd ">
6. Finally, we're going to add a test for the level two cache@Testpublic void Testgetperson () {Person person = Personservice.getperson (2); System.out.println (Person.getname ()); SYSTEM.OUT.PRINTLN ("Close data area =========="), try {thread.sleep (1000*15);//15 second} catch (Interruptedexception e) { E.printstacktrace ();} SYSTEM.OUT.PRINTLN ("Get data for the second time");p Erson = Personservice.getperson (2); System.out.println (Person.getname () + " ++++++++++");}
Middle we shut down MySQL service
The service is open March 30, 2015 19:07
Let's start testing.
Next we close the service
Next, let's look at the console output
The result is an output.
March 30, 2015 19:11 or so, for me, that sleep time is prolonged.
Actually, we can do that. Change the cache property to see where the cached file is.
"Java EE Spring" 10, Integrated SSH framework (3)