Spring3.0 configuration setup

Source: Internet
Author: User

Original post address: http://apps.hi.baidu.com/share/detail/20255244

After a whole day, spring3.0.2 + hibernate3 + tiles2.1.3 was finally combined to build a project environment. Below I will put My configuration file up, hoping to help those interested in spring3.

ApplicationContext-mvc-controller.xml Configuration

<? XML version = "1.0" encoding = "UTF-8"?>
<Beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xmlns: P = "http://www.springframework.org/schema/p" xmlns: AOP = "http://www.springframework.org/schema/aop"

Xmlns: Tx = "http://www.springframework.org/schema/tx" xmlns: context = "http://www.springframework.org/schema/context"
Xsi: schemalocation = "http://www.springframework.org/schema/beans
Http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
Http://www.springframework.org/schema/aop
Http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
Http://www.springframework.org/schema/tx
Http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
Http://www.springframework.org/schema/context
Http://www.springframework.org/schema/context/spring-context-3.0.xsd>

<Context: component-scan base-package = "lylt. MVC. Action"/>
<Bean class = "org. springframework. Web. servlet. MVC. annotation. defaultannotationhandlermapping">
<Property name = "interceptors">
<List>
<Ref bean = "opensessioninviewinterceptor"/>
<Ref bean = "userrightinterceptor"/>
</List>
</Property>
</Bean>

<! -- Spring transaction management -->
<Bean class = "org. springframework. Web. servlet. MVC. annotation. annotationmethodhandleradapter"/>
<Bean id = "opensessioninviewinterceptor"
Class = "org. springframework. Orm. hibernate3.support. opensessioninviewinterceptor">
<Property name = "sessionfactory">
<Ref bean = "sessionfactory"/>
</Property>
</Bean>

<! -- Spring interceptor configuration -->
<Bean id = "userrightinterceptor" class = "lylt. Interceptor. userrightinterceptor"> </bean>

<! -- Spring error and exception page definition -->
<Bean class = "org. springframework. Web. servlet. handler. simplemappingexceptionresolver">
<Property name = "exceptionmappings">
<Props>
<Prop key = "org. springframework. Dao. dataaccessexception"> error </prop>
<Prop key = "org. springframework. transaction. transactionexception"> error </prop>
</Props>
</Property>
</Bean>

<! -- Tiles definition -->
<Bean id = "tilesconfigurer" class = "org. springframework. Web. servlet. View. tiles2.tilesconfigurer">
<Property name = "definitions">
<List>
<Value>/WEB-INF/tiles/*. xml </value>
</List>
</Property>
<Property name = "preparerfactoryclass" value = "org. springframework. Web. servlet. View. tiles2.springbeanpreparerfactory"/>
</Bean>
<Bean id = "viewresolver" class = "org. springframework. Web. servlet. View. urlbasedviewresolver">
<Property name = "viewclass" value = "org. springframework. Web. servlet. View. tiles2.tilesview"/>
</Bean>
<Bean id = "messagesource" class = "org. springframework. Context. Support. resourcebundlemessagesource" P: basename = "messages"/>
</Beans>
The applicationContext-hibernate.xml configuration here uses DBCP to connect to the database

<? XML version = "1.0" encoding = "UTF-8"?>
<Beans xmlns = "http://www.springframework.org/schema/beans"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xmlns: P = "http://www.springframework.org/schema/p"
Xmlns: context = "http://www.springframework.org/schema/context"
Xmlns: Jee = "http://www.springframework.org/schema/jee"
Xmlns: Tx = "http://www.springframework.org/schema/tx"
Xsi: schemalocation ="
Http://www.springframework.org/schema/beans
Http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
Http://www.springframework.org/schema/context
Http://www.springframework.org/schema/context/spring-context-3.0.xsd
Http://www.springframework.org/schema/jee
Http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
Http://www.springframework.org/schema/tx
Http://www.springframework.org/schema/tx/spring-tx-3.0.xsd>
<Context: annotation-config/>
<Bean id = "placeholderconfig"
Class = "org. springframework. Beans. Factory. config. propertyplaceholderconfigurer">
<Property name = "locations">
<List>
<Value> classpath: init. properties </value>
</List>
</Property>
</Bean>
<Bean id = "datasource" class = "org. Apache. commons. DBCP. basicdatasource" Destroy-method = "close">
<Property name = "driverclassname">
<Value >$ {datasource. driverclassname} </value>
</Property>
<Property name = "url">
<Value >$ {datasource. url} </value>
</Property>
<Property name = "username">
<Value >$ {datasource. Username} </value>
</Property>
<Property name = "password">
<Value >$ {datasource. Password} </value>
</Property>
<Property name = "maxactive">
<Value >$ {JDBC. maxactive} </value>
</Property>
<Property name = "maxidle">
<Value >$ {JDBC. maxidle} </value>
</Property>
<Property name = "maxwait">
<Value >$ {JDBC. maxwait} </value>
</Property>
<Property name = "defaultautocommit">
<Value >$ {JDBC. defaultautocommit} </value>
</Property>
</Bean>
<Bean id = "sessionfactory"
Class = "org. springframework. Orm. hibernate3.localsessionfactorybean">
<Property name = "datasource">
<Ref local = "datasource"/>
</Property>
<Property name = "mappingdirectorylocations">
<List>
<Value> classpath: lylt/hibernate/data/</value>
</List>
</Property>
<Property name = "hibernateproperties">
<Props>
<Prop key = "hibernate. dialect">
$ {Hibernate. dialect}
</Prop>
<Prop key = "hibernate. show_ SQL">
$ {Hibernate. show_ SQL}
</Prop>
<Prop key = "hibernate. JDBC. fetch_size">
$ {Hibernate. JDBC. fetch_size}
</Prop>
<Prop key = "hibernate. JDBC. batch_size">
$ {Hibernate. JDBC. batch_size}
</Prop>
<Prop key = "hibernate. cache. provider_class">
Org. hibernate. cache. ehcacheprovider
</Prop>
<Prop key = "hibernate. cache. use_query_cache"> true </prop>
</Props>
</Property>
</Bean>
</Beans>

The database uses the book Postgres. Below are some configuration files.

Datasource. type = s
Datasource. driverclassname = org. PostgreSQL. Driver
Datasource. url = JDBC: PostgreSQL: // 192.168.1.203: 5432/dbname
Datasource. Username = s
Datasource. Password = Postgres

Datasource. maxactive = 10
Datasource. maxidle = 2
Datasource. maxwait = 120000
Datasource. whenexhaustedaction = 1
Datasource. validationquery = select 1 from dual
Datasource. testonborrow = true
Datasource. testonreturn = false

# DBCP
JDBC. maxactively = 100
JDBC. maxidle = 20
JDBC. maxwait = 5
JDBC. defaultautocommit = true

Hibernate. dialect = org. hibernate. dialect. postgresqldialect

Hibernate. JDBC. batch_size = 25
Hibernate. JDBC. fetch_size = 50
Hibernate. show_ SQL = true
Hibernate. Connection. release_mode = after_transaction

ApplicationContext-service.xml Configuration

<? XML version = "1.0" encoding = "UTF-8"?>
<Beans xmlns = "http://www.springframework.org/schema/beans"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xmlns: P = "http://www.springframework.org/schema/p"
Xmlns: context = "http://www.springframework.org/schema/context"
Xmlns: Jee = "http://www.springframework.org/schema/jee"
Xmlns: Tx = "http://www.springframework.org/schema/tx"
Xsi: schemalocation ="
Http://www.springframework.org/schema/beans
Http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
Http://www.springframework.org/schema/context
Http://www.springframework.org/schema/context/spring-context-3.0.xsd
Http://www.springframework.org/schema/jee
Http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
Http://www.springframework.org/schema/tx
Http://www.springframework.org/schema/tx/spring-tx-3.0.xsd>
<Context: annotation-config/>

<! -- Here is the annotation configuration of spring -->
<Context: component-Scan
Base-package = "lylt. hibernate. Dao, lylt. Service, lylt. MVC"/>
<TX: annotation-driven/>
<Bean id = "transactionmanager"
Class = "org. springframework. Orm. hibernate3.hibernatetransactionmanager"
P: sessionfactory-ref = "sessionfactory"/>
</Beans>

Add the following configuration to Web. xml:

<Servlet>
<Servlet-Name> mvcaction </servlet-Name>
<Servlet-class> org. springframework. Web. servlet. dispatcherservlet </servlet-class>
<Init-param>
<Param-Name> contextconfiglocation </param-Name>
<Param-value>/WEB-INF/applicationContext-mvc-controller.xml </param-value>
</Init-param>
<Load-on-startup> 2 </load-on-startup>
</Servlet>
<Servlet-mapping>
<Servlet-Name> mvcaction </servlet-Name>
<URL-pattern> *. DO </url-pattern>
</Servlet-mapping>

<Context-param>
<Param-Name> log4jconfiglocation </param-Name>
<Param-value> classpath: log4j. properties </param-value>
</Context-param>
<Listener>
<Listener-class> org. springframework. Web. util. log4jconfiglistener </listener-class>
</Listener>
<Context-param>
<Param-Name> contextconfiglocation </param-Name>
<Param-value> classpath *: applicationcontext *. xml </param-value>
</Context-param>

This is basically the case. It is not necessary to say anything else that is basic.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.