<?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.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://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">//申明schema <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure --> <!--註解驅動 Enables the Spring MVC @Controller programming model --> <!-- 須要訪問數據庫,因此引入事務管理 --> <bean id="userDao" class="me.chanjar.weixin.cp.dao.impl.UserinfoDaoimpl" > <property name="sessionFactory" ref="sessionFactory"/> </bean> <!-- 配置c3p0 sqlserver數據源 --> <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> <!-- 配置hibernate的sessionfactory bean 由spring 的localsessionfactorybean提供--> <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> <!-- 配置數據源 --> <property name="dataSource" ref="dataSource"></property> <!-- 配置hibernate的配置文件和名稱 --> <property name="configLocation" value="classpath:hibernate.cfg.xml"></property> <!-- 配置hibernate映射文件 --> <property name="mappingLocations" value="classpath:me/chanjar/weixin/cp/po/*.hbm.xml"></property> </bean> <!-- 配置spring的事務管理器 --> <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- 通知配置事務屬性,須要事務屬性管理器transactionmanager --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <!-- 事務的傳播屬性 --> <tx:attributes> <!-- all methods starting with get are read-only --> <tx:method name="get*" read-only="true"/> <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> <!-- 配置事務的aop,而且把事務屬性和切點關聯起來 --> <aop:config> <!-- 配置切點 -execution(*返回值,impl文件下面的某個類的全部方法--> <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-->
如上是application-Context.xml配置文件。下面看一下web.xml html
<?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> <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>
以下是springmvc-servlet.xml的配置文件。知道爲啥叫spring-mvc的配置文件嗎?是由於我在web.xml配置文件裏面說明了,dispatch-servlet 的名稱爲springmvc的緣由嗎????java
<?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.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://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 --> <!--註解驅動 Enables the Spring MVC @Controller programming model --> <mvc:annotation-driven></mvc:annotation-driven> <!-- 掃描器 --> <context:component-scan base-package="me" /> <!-- 配置視圖解析器。Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- 前綴 --> <property name="prefix" value="/" /> <!-- 後綴 --> <property name="suffix" value=".jsp" /> </bean> <!-- Spring分段文件上傳所必須的 ,用於檢查請求中是否包含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> <!--添加spring mvc intercepters--> <mvc:interceptors> <bean class="me.chanjar.weixin.cp.Interceptor.MyHandlerInterceptor"> </bean> </mvc:interceptors> <!-- 添加intercepters結束 --> <import resource="classpath:applicationContext.xml"/> </beans><!--springmvc-servlet.xml-->
首先須要引入aop 以及tx 的申明和schema
web
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" //申明通知
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">//申明schema
<context:componet-scan base-package=""/>
注意:spring 經過註解實例化對象,默認生成的bean是非限定的類名,好比:@Component,@Repository,@Service,@Controller 註解的對象,調用的時候只須要使用首字母小寫的類 getBean("首字母小寫的類名");spring
由於我在springmvc-servlet.xml文件裏面申明瞭掃描包路徑,而且將applicationContext.xml引入。因此就不須要在applicationContext.xml申明瞭。
sql