基于hibernate和springmvc中的一些xml文件的配置

首先还是配置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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>TestSSH</display-name>

<!-- 配置自定义的基于spring实现的过滤器 -->
<filter>
<filter-name>loginFilter</filter-name>
<filter-class>com.hpe.filter.LoginFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>loginFilter</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>

<!-- 配置springmvc过滤器的字符过滤功能 -->
<filter>
<filter-name>characterEncodingFilter</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>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- 加载spring的上下文 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 加载spring的监听 -->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>

<!-- 配置springmvc的拦截器 -->
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>




<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>

springMVC-servlet.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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
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.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">

<!--springmvc配置切面 -->
<aop:aspectj-autoproxy expose-proxy="true"></aop:aspectj-autoproxy>

<!-- 开启springmvc的注解 -->
<context:component-scan base-package="com.hpe.*"></context:component-scan>

<!-- 配置springmvc的试图解析器 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:viewClass="org.springframework.web.servlet.view.JstlView"
p:prefix="/jsp/"
p:suffix=".jsp" />

<!-- springmvc不过滤的请求 -->
<!--一些的静态的资源我们不需要过滤奥 -->
<mvc:annotation-driven />
<mvc:resources location="/image/" mapping="/image/**"/>
<mvc:resources location="/css/" mapping="/css/**"/>
<mvc:resources location="/js/" mapping="/js/**"/>
<!-- <mvc:resources location="/jsp/" mapping="/jsp/**"/> -->

<!-- 配置文件上传下载的设置内容 -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 配置上传文件的编码格式 -->
<property name="defaultEncoding" value="utf-8"></property>
<!-- 上传文件的大小 -->
<property name="maxUploadSize" value="102400000"></property>
<!-- 设置上传文件的临时文件夹 -->
<property name="uploadTempDir" value="upload/temp"></property>
</bean>

<!-- 配置springmvc的自定义拦截器 -->
<mvc:interceptors>
<!-- 第一个自定义拦截器 -->
<mvc:interceptor>
<!--配置要拦截的内容 -->
<mvc:mapping path="/**"/>
<bean class="com.hpe.interceptor.LoginInterceptor">
<property name="nofilePath" >
<list>
<value>/login</value>
<value>/register</value>
</list>
</property>
</bean>
</mvc:interceptor>
</mvc:interceptors>

</beans>

applicationContext.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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
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.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">

<!-- 读取jdbc.properties配置文件 -->

<!-- <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations"> <list> <value>classpath:jdbc.properties</value>
</list> </property> </bean> -->
<context:property-placeholder location="classpath:jdbc.properties" />
<!-- 配置c3p0的数据源 -->
<bean id="c3p0Source" destroy-method="close"
class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="user" value="root"></property>
<property name="password" value="123456"></property>
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/java4">
</property>
</bean>

<!-- 开启springmvc的注解 -->
<context:component-scan base-package="com.hpe.*"></context:component-scan>
<!-- 配置hibernate的配置文件 -->
<!-- 如果使用配置文件采用 -->
<!-- <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"></bean> -->

<!--如果使用hibernate注解采用以下写法 -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<!-- 把数据源注入进session工程 -->
<property name="dataSource" ref="c3p0Source"></property>
<!-- 配置hibernate的属性设置 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="show_sql">true</prop>
<prop key="format_sql">true</prop>
</props>
</property>
<!-- 加载hibernate的配置文件hbm映射文件 -->
<!-- <property name="mappingResources"> <list> <value>com/hpe/bean/User.hbm.xml</value>
</list> </property> -->

<!-- hibernate采用注解的形式 配置 -->
<property name="packagesToScan">
<list>
<value>com.hpe.bean</value>
</list>
</property>
</bean>
<!--基于pojo的切面配置 -->
<bean id="ta" class="com.hpe.advice.TestAdvice"></bean>
<aop:config>
<!--配置一个切点 -->
<aop:pointcut expression="execution(* com.hpe.dao.*.*(..))"
id="cut" />
<aop:pointcut expression="execution(* com.hpe.service.*.*(..))"
id="cut1" />
<!--配置一个切面 切面引入的增强 -->
<aop:aspect ref="ta">
<aop:before method="before" pointcut-ref="cut" />
<aop:after method="after" pointcut-ref="cut" />
<aop:after-returning method="afterReturn"
pointcut-ref="cut" returning="ret" />
<aop:after-throwing method="afterThrow"
pointcut-ref="cut" throwing="ex" />
<aop:around method="around" pointcut-ref="cut1" />


</aop:aspect>

</aop:config>


</beans>

 

posted @ 2016-11-21 16:07  倾世【天意】  阅读(1382)  评论(0编辑  收藏  举报