spring的配置

web.xml的配置

<?xml version="1.0" encoding="UTF-8"?>   
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">   
  <display-name>s3h3</display-name>   
   <context-param>     
     <param-name>contextConfigLocation</param-name>     
     <param-value>classpath:*Context.xml</param-value>     
 </context-param>     
  <listener>     
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>     
 </listener>     
  
 <servlet>     
     <servlet-name>spring</servlet-name>     
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>     
     <load-on-startup>1</load-on-startup>     
 </servlet>     
 <servlet-mapping>     
     <servlet-name>spring</servlet-name>  <!-- 这里在配成spring,下边也要写一个名为spring-servlet.xml的文件,主要用来配置它的controller -->   
     <url-pattern>/</url-pattern>     
 </servlet-mapping>     
  <welcome-file-list>   
    <welcome-file>index.jsp</welcome-file>   
  </welcome-file-list>   
</web-app>  

spring-servlet,主要配置controller的信息

<?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"     
   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:annotation-config />   
       <!-- 把标记了@Controller注解的类转换为bean -->     
      <context:component-scan base-package="com.mvc.controller" />     
  <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->     
      <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />     
        
       <!-- 对模型视图名称的解析,即在模型视图名称添加前后缀 -->     
       <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"     
          p:prefix="/WEB-INF/view/" p:suffix=".jsp" />     
           
       <bean id="multipartResolver"     
          class="org.springframework.web.multipart.commons.CommonsMultipartResolver"     
          p:defaultEncoding="utf-8" />     
 </beans>  


applicationContext.xml代码

<?xml version="1.0" encoding="UTF-8"?>   
<beans xmlns="http://www.springframework.org/schema/beans"  
 xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"  
 xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"  
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
 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/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">   
  
 <context:annotation-config />   
 <context:component-scan base-package="com.mvc" />  <!-- 自动扫描所有注解该路径 -->   
  
 <context:property-placeholder location="classpath:/hibernate.properties" />   
  
 <bean id="sessionFactory"  
  class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">   
  <property name="dataSource" ref="dataSource" />   
  <property name="hibernateProperties">   
   <props>   
    <prop key="hibernate.dialect">${dataSource.dialect}</prop>   
    <prop key="hibernate.hbm2ddl.auto">${dataSource.hbm2ddl.auto}</prop>   
    <prop key="hibernate.hbm2ddl.auto">update</prop>   
   </props>   
  </property>   
  <property name="packagesToScan">   
   <list>   
    <value>com.mvc.entity</value><!-- 扫描实体类,也就是平时所说的model -->   
   </list>   
    </property>   
 </bean>   
  
 <bean id="transactionManager"  
  class="org.springframework.orm.hibernate3.HibernateTransactionManager">   
  <property name="sessionFactory" ref="sessionFactory" />   
  <property name="dataSource" ref="dataSource" />   
 </bean>   
  
 <bean id="dataSource"  
  class="org.springframework.jdbc.datasource.DriverManagerDataSource">   
  <property name="driverClassName" value="${dataSource.driverClassName}" />   
  <property name="url" value="${dataSource.url}" />   
  <property name="username" value="${dataSource.username}" />   
  <property name="password" value="${dataSource.password}" />   
 </bean>   
 <!-- Dao的实现 -->   
 <bean id="entityDao" class="com.mvc.dao.EntityDaoImpl">     
  <property name="sessionFactory" ref="sessionFactory" />   
 </bean>   
 <tx:annotation-driven transaction-manager="transactionManager" />   
 <tx:annotation-driven mode="aspectj"/>   
     
    <aop:aspectj-autoproxy/>     
</beans>  


原文地址:https://www.cnblogs.com/zhao123/p/3876822.html