spring mvc学习(二)之spring-servlet.xml和applicationContext.xml配置

上一篇说到了web.xml的配置,下面我说下spring-servlet和applicationContext.xml的配置。

spring-servlet的配置

<?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:mvc="http://www.springframework.org/schema/mvc"    
    xmlns:context="http://www.springframework.org/schema/context"    
    xmlns:util="http://www.springframework.org/schema/util"    
    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/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd    
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">
	   	    	   
	 <context:component-scan base-package="com.mvc.web" />  
	 
	 <mvc:annotation-driven />
	 
	 <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
	 
	 <bean id="viewResolver"
	  class="org.springframework.web.servlet.view.UrlBasedViewResolver">
	  
	  <property name="viewClass"
	   value="org.springframework.web.servlet.view.JstlView" />
	   
	  <property name="prefix" value="/WEB-INF/jsp/" /><!-- 地址前缀 -->
	  <property name="suffix" value=".jsp" /><!-- 地址后缀 -->
	  
	 </bean>
	 
	</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">

	<!-- Root Context: defines shared resources visible to all other web components -->
    <context:annotation-config />
	<context:component-scan base-package="com.mvc" />  <!-- 自动扫描所有注解该路径 -->
	<context:property-placeholder location="classpath:/hibernate.properties" />
	 
    <!-- 配置数据源 -->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
        <!-- 基本属性 url、user、password -->  
        <property name="url" value="${dataSource.url}" />  
        <property name="username" value="${dataSource.username}" />  
        <property name="password" value="${dataSource.password}" /> 
        <property name="driverClassName" value="${dataSource.driverClassName}" />
    </bean>
    
    <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.model</value><!-- 扫描实体类,也就是平时所说的model -->
		    </list>
		</property>			
    </bean>
    
    <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</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 * com.mvc.service.*.*(..))"
			id="businessService" />
		<aop:advisor advice-ref="txAdvice" pointcut-ref="businessService" />
		
		<aop:aspect id="aspect" ref="aspectBean">
		    <aop:pointcut expression="execution(public * com.mvc.service.*.*(..))" id="businessService1"/><!-- 这里为了log4j的日志输出 -->
		    <aop:before pointcut-ref="businessService1" method="doBefore"/>
		    <aop:after-throwing  pointcut-ref="businessService1" throwing="e" method="doAfterThrow" />    
            <aop:after pointcut-ref="businessService1" method="doAfter"/>    
            <aop:around pointcut-ref="businessService1" method="doAround"/>            
		</aop:aspect>
		
	</aop:config>
	<bean id="aspectBean" class="com.mvc.aspect.Aspect" />
	
	<tx:advice id="txAdvice" transaction-manager="txManager">
		<tx:attributes>				
	            <tx:method name="get*" propagation="REQUIRED" read-only="true" />
                    <tx:method name="del*" propagation="REQUIRED" />
                    <tx:method name="save*" propagation="REQUIRED" />
                    <tx:method name="update*" propagation="REQUIRED" />        
                    <tx:method name="add*" propagation="REQUIRED"/>              
		</tx:attributes>
	</tx:advice>   
</beans>

如果有不明的额地方,请及时回复,我将会最快回答你的问题,下篇我将会把log4j.properties和hibernate.properties的配置发出来。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值