Spring Transaction Management

Source: Internet
Author: User

<?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:mvc=" Http://www.springframework.org/schema/mvc " xmlns:context=" Http://www.springframework.org/schema/context " xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"  xmlns:tx= "Http://www.springframework.org/schema/tx"  //declaration Notice xsi:schemalocation= "http://www.springframework.org/schema/mvc http://www.springframework.org/ schema/mvc/spring-mvc.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/ schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org /schema/context/spring-context.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 ">//Declaration schema<!--  dispatcherservlet context: defines this servlet ' s request-processing  infrastructure --><!--Notes driver  Enables the Spring MVC  @Controller   programming model -->        <!--  need access to the database, So introduce transaction management  -->    <bean id= "Userdao"  class= " Me.chanjar.weixin.cp.dao.impl.UserinfoDaoimpl " ><property name=" Sessionfactory " ref=" Sessionfactory "/></bean><!--  Configure c3p0 sqlserver data source  --><bean id=" DataSource " class=" Com.mchange.v2.c3p0.ComboPooledDataSource ">   <property name=" Driverclass " value=" Com.microsoft.sqlserver.jdbc.SQLServerDriver "></property>     <property name= "Jdbcurl"  value= "Jdbc:sqlserver://loCalhost:1433;databasename=getsalary "></property>   <property name=" user "  Value= "sa" ></property>   <property name= "password"  value= "sa123" ></ property></bean><!--  Configuration Hibernate sessionfactory bean  by spring  The Localsessionfactorybean provides--><bean id= "Sessionfactory"  class= " Org.springframework.orm.hibernate4.LocalSessionFactoryBean ">  <!--  Configure data sources  -->   <property name= "DataSource"  ref= "DataSource" ></property>     <!--  Configure hibernate configuration file and name  -->  <property name= "Configlocation"   Value= "Classpath:hibernate.cfg.xml" ></property>  <!--  Configure hibernate mapping files  -->   <property name= "Mappinglocations"  value= "Classpath:me/chanjar/weixin/cp/po/*.hbm.xml" ></property></bean><!--  Configure Spring's transaction manager &NBSP;--&GT;<bean id= "TransactionManager"  class= " Org.springframework.orm.hibernate4.HibernateTransactionManager ">   <property name=" Sessionfactory " ref=" sessionfactory "></property></bean><!--  Notification configuration transaction Properties, Requires transaction property manager transactionmanager --><tx:advice id= "Txadvice"  transaction-manager= " TransactionManager "><!--  Transaction propagation Properties  -->  <tx:attributes>  <!--  all methods starting with get are read-only -->     <tx:method name= "get*"    read-only= "true"/&GT;&NBSP;&NBSP;&NBSP;&NBSP;&LT;TX: Method name= "query*"  read-only= "true"/>    <tx:method name= "ad*"     propagation= "REQUIRED"/>    <tx:method name= "updat*"  propagation= "REQUIRED"/>    <tx:method name= "del*"    propagation= "REQUIRED"/>  </tx:attributes></tx:advice><!--  Configure AOP for transactions and associate transaction properties with Pointcuts  --><aop:config><!--  Configure tangency  -execution (* return value, Impl all methods of a class below the file-->       <aop:pointcut expression= "Execution (* me.chanjar.weixin.cp.dao.impl.*.* (..))"  id= "Pointcut"  />      <aop:advisor  advice-ref= " Txadvice " pointcut-ref=" pointcut "/></aop:config></beans><!--application-context.xml-- >

as above is the Application-context.xml configuration file. Here's a look at web.xml 

<?xml version= "1.0"  encoding= "Utf-8"?><!-- Licensed to the Apache  software foundation  (ASF)  under one or more  contributor license  agreements.  see the notice file distributed with  this  work for additional information regarding copyright ownership.   The asf licenses this file to you under the apache license,  Version 2.0   (the  "License"); you may not use this  file except in compliance with  the license.  you may  obtain a copy of the license at      http:// Www.apache.org/licenses/LICENSE-2.0  Unless required by applicable law or  agreed to in writing, software  distributed under the license is distributed  on an  "As is"  basis,  without warranties or conditions  OF ANY KIND, either express or implied.  See the  license for the specific language governing permissions and   Limitations under the license.--><web-app xmlns= "Http://java.sun.com/xml/ns/javaee"   xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"   xsi:schemalocation= "http// java.sun.com/xml/ns/javaee                       http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd "   version= "3.0"   metadata-complete= "true" >  <display-name>Welcome to  Tomcat</display-name> &nbSp;<description>     welcome to tomcat  </description ><servlet><servlet-name>Test</servlet-name> <servlet-class> Me.chanjar.weixin.cp.cpservlet.mpservlet</servlet-class></servlet> <servlet-mapping>  <servlet-name>Test</servlet-name> <url-pattern>/justdoit</url-pattern>  </servlet-mapping> <!-- the definition of the root spring  Container shared by all Servlets and Filters --><!--  Creates the spring container shared by all servlets and filters  --><servlet><servlet-name>springmvc</servlet-name><servlet-class> org.springframework.web.servlet.dispatcherservlet</servlet-class><load-on-startup>1</ Load-on-startup></servlet><servlet-mapping><servlet-name>springmvc</servlet-name><url-pattern>*.do</url-pattern></servlet-mapping>< Filter><filter-name>encodingfilter</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></filter> <filter-mapping><filter-name>encodingfilter</filter-name><url-pattern>/*</ Url-pattern></filter-mapping></web-app>

below is the Springmvc-servlet.xml configuration file. Do you know why the configuration file is called Spring-mvc? Because I explained in the Web. XML config file, is the name of Dispatch-servlet Springmvc????

<?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:mvc=" Http://www.springframework.org/schema/mvc " xmlns:context=" Http://www.springframework.org/schema/context " xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"  xmlns:tx= "Http://www.springframework.org/schema/tx"  xsi:schemalocation= "Http://www.springframework.org/schema/mvc http://www.springframework.org/schema /mvc/spring-mvc.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/ Beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema /context/spring-context.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 "><!--  Dispatcherservlet context: defines this servlet ' s request-processing  infrastructure --><!--Notes driver  Enables the Spring MVC  @Controller   programming model --><mvc:annotation-driven></mvc:annotation-driven><!--  Scanners  -->    <context:component-scan base-package= "Me"  />   <!--  Configure view resolver. resolves views selected for rendering by  @Controllers  to .jsp  Resources in the /web-inf/views directory --><bean class= " Org.springframework.web.servlet.view.InternalResourceViewResolver "><!--  prefix  --><property  name= "prefix"  value= "/"  /><!--  suffix  --><property name= "suffix"   Value= ". JSP"  /></bean><!-- spring the required &nbsp for the multipart file upload, to check if the request contains multipart      see: http:// www.html.org.cn/books/springreference/ch13s08.html      -->       <bean id= "Multipartresolver"  class= " Org.springframework.web.multipart.commons.CommonsMultipartResolver ">            <property name= "Maxuploadsize"  value= "100000"/>     </bean>     <!--Add spring mvc intercepters-->      <mvc:interceptors>      <bean class= " Me.chanjar.weixin.cp.Interceptor.MyHandlerInterceptor ">      </bean>     </mvc:interceptors>    <!--  Add intercepters end  -- ><import resource= "Classpath:applicationContext.xml"/></beans><!--springmvc-servlet.xml--> 
First, spring Business declaration steps1.1 declaring namespaces and schemas first

First, we need to introduce the declaration and schema of AOP and TX

xmlns:aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" xmlns:tx= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/TX"/Declaration Notice
Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd ">// Declaration schema
1.2 Declaring package paths for component scans
<context:componet-scan base-package= ""/>

Because I stated the scan packet path in the Springmvc-servlet.xml file and introduced the Applicationcontext.xml. So there is no need to declare it in Applicationcontext.xml.


Spring Transaction Management

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.