SpringMVC之初入门-配置篇

来源:互联网 发布:卫报数据新闻 统计 编辑:程序博客网 时间:2024/05/10 03:51
今天看了springmvc的视频,有如下收获。springmvc的xml配置和注解配置。
1、xml配置
在加入springmvc的jar包后,在web.xml中添加如下代码
<servlet>
  <servlet-name>dispatcherServlet</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/hib-config.xml,/WEB-INF/web-config.xml,/WEB-INF/service-config.xml,/WEB-INF/dao-config.xml</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
  <servlet-name>dispatcherServlet</servlet-name>
  <url-pattern>*.do</url-pattern>
</servlet-mapping>

其中的contextConfigLocation中用于配置文件。
(1)web-config.xml配置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" 
xsi:schemaLocation="
http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

<!-- Controller方法调用规则定义 -->
   <bean id="paraMethodResolver"
       class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
       <property name="paramName" value="action"/>
       <property name="defaultMethodName" value="list"/>
   </bean>
 
  <!-- 页面View层基本信息设定 -->
   <bean id="viewResolver"
         class="org.springframework.web.servlet.view.InternalResourceViewResolver">
       <property name="viewClass"
           value="org.springframework.web.servlet.view.JstlView"/>
       <property name="prefix" value="/jsp/"/>
       <property name="suffix" value=".jsp"/>
   </bean>


<!-- servlet映射列表,所有控制层Controller的servlet在这里定义 -->
   <bean id="urlMapping"
         class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
       <property name="mappings">
           <props>
               <prop key="user.do">userController</prop>
           </props>
       </property>
   </bean>


<bean id="userController" class="cn.dlm.action.UserController">
<property name="userService" ref="userService"></property>
</bean>
</beans>

(2)hib-config.xml配置hibernate的文件:
<?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: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-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/context   
  http://www.springframework.org/schema/context/spring-context-2.5.xsd
">
<context:component-scan  base-package="cn.dlm"/>   
<!-- 支持aop注解 -->
<aop:aspectj-autoproxy />


<bean id="dataSource"  
           class="org.apache.commons.dbcp.BasicDataSource">  
           <property name="driverClassName"  
               value="com.mysql.jdbc.Driver">  
           </property>  
           <property name="url" value="jdbc:mysql://localhost:3306/test"></property>  
           <property name="username" value="root"></property>  
           <property name="password" value="root"></property>
   </bean>  


  <bean id="sessionFactory"  
      class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">  
          <property name="dataSource">  
              <ref bean="dataSource" />  
          </property>
          <property name="hibernateProperties">  
              <props>  
              <!-- key的名字前面都要加hibernate. -->
                  <prop key="hibernate.dialect">  
                      org.hibernate.dialect.MySQLDialect  
                  </prop>  
                  <prop key="hibernate.show_sql">true</prop>
                  <prop key="hibernate.hbm2ddl.auto">update</prop>
              </props>
          </property>
<property name="packagesToScan">
<value>cn.dlm.po</value>
</property>
  </bean>  


<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate" >
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>


<!--配置一个JdbcTemplate实例-->  
<bean id="jdbcTemplate"  class="org.springframework.jdbc.core.JdbcTemplate">   
    <property name="dataSource" ref="dataSource"/>   
</bean>  




<!-- 配置事务管理 -->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" >
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<tx:annotation-driven transaction-manager="txManager" />
<aop:config> 
<aop:pointcut expression="execution(public * cn.dlm.service.impl.*.*(..))" id="businessService"/> 
<aop:advisor advice-ref="txAdvice" pointcut-ref="businessService" /> 
</aop:config> 
<tx:advice id="txAdvice" transaction-manager="txManager" > 
<tx:attributes> 
<tx:method name="find*"  read-only="true" propagation="NOT_SUPPORTED"  /> 
<!-- get开头的方法不需要在事务中运行 。 
有些情况是没有必要使用事务的,比如获取数据。开启事务本身对性能是有一定的影响的--> 
<tx:method name="*"/>    <!-- 其他方法在实务中运行 --> 
</tx:attributes> 
</tx:advice> 


</beans>

(3)service-config.xml用于配置service层的文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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-2.5.xsd">


<bean id="userService" class="cn.dlm.service.impl.UserServiceImpl">
<property name="userDao" ref="userDao"></property>
</bean>
</beans>
(4)dao-config.xml用于dao的配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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-2.5.xsd">

<bean id="userDao" class="cn.dlm.dao.impl.UserDaoImpl">
 <property name="hibernateTemplate" ref="hibernateTemplate"></property>
</bean>
</beans>

2、基于注解的配置文件
spring-service.xml文件
<?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-2.5.xsd
   http://www.springframework.org/schema/context 
   http://www.springframework.org/schema/context/spring-context-2.5.xsd">
    
   <!-- 对web包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能 -->
   <context:component-scan base-package="cn.dlm"/>


   <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 -->
   <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>


   <!--对模型视图名称的解析,即在模型视图名称添加前后缀 -->
   <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver" 
       p:prefix="/jsp/" p:suffix=".jsp"/>
</beans>
hib-config.xml配置hibernate文件
<?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: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-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/context   
  http://www.springframework.org/schema/context/spring-context-2.5.xsd
">
<context:component-scan  base-package="cn.dlm"/>   
<!-- 支持aop注解 -->
<aop:aspectj-autoproxy />


<bean id="dataSource"  
           class="org.apache.commons.dbcp.BasicDataSource">  
           <property name="driverClassName"  
               value="com.mysql.jdbc.Driver">  
           </property>  
           <property name="url" value="jdbc:mysql://localhost:3306/test"></property>  
           <property name="username" value="root"></property>  
           <property name="password" value="root"></property>
   </bean>  


  <bean id="sessionFactory"  
      class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">  
          <property name="dataSource">  
              <ref bean="dataSource" />  
          </property>
          <property name="hibernateProperties">  
              <props>  
              <!-- key的名字前面都要加hibernate. -->
                  <prop key="hibernate.dialect">  
                      org.hibernate.dialect.MySQLDialect  
                  </prop>  
                  <prop key="hibernate.show_sql">true</prop>
                  <prop key="hibernate.hbm2ddl.auto">update</prop>
              </props>
          </property>
<property name="packagesToScan">
<value>cn.dlm.po</value>
</property>
  </bean>  


<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate" >
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>


<!--配置一个JdbcTemplate实例-->  
<bean id="jdbcTemplate"  class="org.springframework.jdbc.core.JdbcTemplate">   
    <property name="dataSource" ref="dataSource"/>   
</bean>  




<!-- 配置事务管理 -->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager" >
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<tx:annotation-driven transaction-manager="txManager" />
<aop:config> 
<aop:pointcut expression="execution(public * cn.dlm.service.impl.*.*(..))" id="businessService"/> 
<aop:advisor advice-ref="txAdvice" pointcut-ref="businessService" /> 
</aop:config> 
<tx:advice id="txAdvice" transaction-manager="txManager" > 
<tx:attributes> 
<tx:method name="find*"  read-only="true" propagation="NOT_SUPPORTED"  /> 
<!-- get开头的方法不需要在事务中运行 。 
有些情况是没有必要使用事务的,比如获取数据。开启事务本身对性能是有一定的影响的--> 
<tx:method name="*"/>    <!-- 其他方法在实务中运行 --> 
</tx:attributes> 
</tx:advice> 


</beans>
0 0
原创粉丝点击