SSH(Spring MVC + Spring + hibernate)中配置文件解析

来源:互联网 发布:图书数据哪里最多 编辑:程序博客网 时间:2024/04/29 17:34

1. 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"    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"    id="WebApp_ID" version="3.0">    <display-name>test</display-name>    <!-- 设置待加载的资源:Spring XML配置文件,Spring安全配置及各类资源文件 -->     <context-param>        <param-name>contextConfigLocation</param-name>        <param-value>            classpath*:applicationContext-*.xml        </param-value>    </context-param>    <!--监听器类实例:spring监听器,用于在启动Web容器时,自动装配ApplicationContext的配置信息-->     <listener>        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>    </listener>    <!-- 过滤器配置,原理类似AOP -->    <!-- 使用Spring中的过滤器解决(所有)请求和应答中的编码问题 -->     <filter>        <filter-name>CharacterEncoding</filter-name>        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>        <init-param>            <param-name>encoding</param-name>            <param-value>utf8</param-value>        </init-param>    </filter>    <filter-mapping>        <filter-name>CharacterEncoding</filter-name>        <url-pattern>/*</url-pattern>    </filter-mapping>    <!-- 验证码生成插件:JCaptcha`s adminlogin filter,登录中起作用 -->    <filter>        <filter-name>AdminjcaptchaFilter</filter-name>        <filter-class>com.test.security.AdminJCaptchaFilter</filter-class>        <init-param>            <param-name>failureUrl</param-name>            <param-value>/login?et=12</param-value>        </init-param>    </filter>    <filter-mapping>        <filter-name>AdminjcaptchaFilter</filter-name>        <url-pattern>/adminjcaptcha.jpg</url-pattern>    </filter-mapping>    <filter-mapping>        <filter-name>AdminjcaptchaFilter</filter-name>        <url-pattern>/loginhandler</url-pattern>    </filter-mapping>    <!-- Spring Secutiry 过滤器链配置,用于认证(http://elim.iteye.com/blog/2156765)、鉴权 ,该filter一定要定义在其它如SpringMVC等拦截请求之前  -->     <filter>        <!-- 在 Spring Security 的配置文件中使用命名空间时,会自动创建springSecurityFilterChain的关联 -->        <filter-name>springSecurityFilterChain</filter-name>        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>    </filter>    <filter-mapping>        <filter-name>springSecurityFilterChain</filter-name>        <url-pattern>/*</url-pattern>    </filter-mapping>      <!-- 将请求转换为标准的http方法,使得支持GET、POST、PUT与DELETE请求,注:HiddenHttpMethodFilter必须作用于dispatcher前 -->    <filter>        <filter-name>HiddenHttpMethodFilter</filter-name>        <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>    </filter>    <filter-mapping>        <filter-name>HiddenHttpMethodFilter</filter-name>        <servlet-name>test</servlet-name>    </filter-mapping>       <!-- DispatcherServlet:前端控制器设计模式的实现,提供Spring MVC的集中访问点,主要用于完成请求分发及视图渲染等工作 -->    <servlet>        <servlet-name>test</servlet-name>        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>        <!-- 加载DispatcherServlet所需的配置文件 -->        <init-param>            <param-name>contextConfigLocation</param-name>            <param-value>classpath*:spring-base.xml</param-value>        </init-param>        <!-- 标记容器在启动的时候就加载这个servlet(实例化并调用其init()方法) -->        <load-on-startup>1</load-on-startup>    </servlet>    <servlet-mapping>        <servlet-name>test</servlet-name>        <url-pattern>/</url-pattern>    </servlet-mapping>    <!-- 请求URL不存在时,默认访问路径 -->    <welcome-file-list>        <welcome-file>/</welcome-file>    </welcome-file-list>    <!-- 出现500错误时跳转的URL -->    <error-page>          <error-code>500</error-code>          <location>/error</location>      </error-page>       <!-- 出现404错误时跳转的URL -->     <error-page>          <error-code>404</error-code>          <location>/error</location>      </error-page> </web-app>

2. applicationContext-default.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:tx="http://www.springframework.org/schema/tx"      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/tx             http://www.springframework.org/schema/tx/spring-tx-3.0.xsd             http://www.springframework.org/schema/aop             http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">      <!-- 配置数据源 -->    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">           <property name="configLocation">                <value>classpath:hibernate.cfg.xml</value>           </property>       </bean>       <!-- 配置事务管理器,加载sessionFactory-->    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">        <property name="sessionFactory">            <ref local="sessionFactory" />        </property>    </bean>    <!-- 配置事务管理器,指定规则 -->    <tx:advice id="txAdvice" transaction-manager="transactionManager">        <tx:attributes>            <tx:method name="create*" propagation="REQUIRED"/>            <tx:method name="update*" propagation="REQUIRED"/>            <tx:method name="delete*" propagation="REQUIRED"/>            <tx:method name="get*" propagation="REQUIRED"/>            <tx:method name="find*" propagation="REQUIRED"/>            <tx:method name="*" propagation="REQUIRED"/>        </tx:attributes>    </tx:advice>    <!-- 配置切面  -->    <aop:config proxy-target-class="true">        <aop:advisor pointcut="execution(* com.test.dao.impl.*.*(..))"  advice-ref="txAdvice" />    </aop:config>           <!--  配置Dao -->    <bean id="UserDao" class="com.test.dao.impl.UserDaoImpl">        <property name="sessionFactory">            <ref local="sessionFactory"/>        </property>    </bean> </beans>

3. applicationContext-security.xml

<?xml version="1.0" encoding="UTF-8"?><b:beans xmlns="http://www.springframework.org/schema/security"    xmlns:b="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-3.0.xsd      http://www.springframework.org/schema/security       http://www.springframework.org/schema/security/spring-security-3.2.xsd">    <!-- 配置方法保护 ,并使能@Secured注解  -->    <global-method-security secured-annotations="enabled"></global-method-security>    <!-- 配置不用过滤的资源 -->    <http pattern="/js/**" security="none" />    <http pattern="/image/**" security="none" />    <http pattern="/css/**" security="none" />    <!--配置URL保护 :        auto-config: 为false时,用户可以自定义过滤器链;        use-expressions:FilterSecurityInterceptor所需要的配置,用于使能EL表达式(hasRole()),完成鉴权;        access-denied-page:鉴权失败时跳转的页面;        authentication-manager-ref:FilterSecurityInterceptor所需要的配置,用于用户认证,当访问受保护的资源且Authentication.isAuthenticated()返回false或者FilterSecurityInterceptor的alwaysReauthenticate属性为true时被调用;        name:http规则的别名;        entry-point-ref:当发生认证或访问异常时,ExceptionTransactionFilter捕获相应异常,并交由entry-point-ref指向的bean完成跳转;        -->    <http auto-config="false" use-expressions="true"        access-denied-page="/warning" authentication-manager-ref="adminAuthManager"        name="adminSecurity" entry-point-ref="adminauthenticationEntryPoint">        <intercept-url pattern="/admin/users" access="hasRole('ROLE_ADMIN')" />        <intercept-url pattern="/admin/mrole" access="hasRole('ROLE_ADMIN')" />                 <!-- 配置自定义的Filter,并通过position将其放在FORM_LOGIN_FILTER节点,就会替换掉原来的FORM_LOGIN_        FILTER节点 -->        <custom-filter ref="adminloginProcessFilter" position="FORM_LOGIN_FILTER" />        <logout invalidate-session="true" logout-success-url="/login"            logout-url="/logout" />    </http>    <!-- 认证管理  -->    <authentication-manager id="adminAuthManager">        <!-- 真正进行认证的provider -->        <authentication-provider user-service-ref="adminDetailsService"></authentication-provider>    </authentication-manager>    <!-- 认证并获取用户详细信息 -->    <b:bean id="adminDetailsService" class="com.test.security.AdminUserDetailsService">        <b:property name="UserDao" ref="UserDao"></b:property>        <b:property name="RoleAuthsDao" ref="RoleAuthsDao"></b:property>    </b:bean>    <!-- 登录处理Filter:用于处理来自表单提交的认证 -->    <b:bean id="adminloginProcessFilter"        class="com.test.security.AdminUsernamePasswordAuthenticationFilter">        <b:property name="usernameParameter" value="username" />        <b:property name="passwordParameter" value="password" />        <b:property name="curl" value="curl" />        <b:property name="filterProcessesUrl" value="/loginhandler" />        <b:property name="authenticationManager" ref="adminAuthManager" />        <b:property name="authenticationSuccessHandler" ref="adminsuccessHandler" />        <b:property name="authenticationFailureHandler" ref="adminfailureHandler" />    </b:bean>    <!-- 登录成功处理 -->    <b:bean id="adminsuccessHandler"        class="com.test.security.AdminAuthenticationSuccessHandler">    </b:bean>    <!-- 登录失败处理 -->    <b:bean id="adminfailureHandler"        class="org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler">        <b:property name="defaultFailureUrl" value="/login?et=4" />    </b:bean>    <!-- 登录入口 -->    <b:bean id="adminauthenticationEntryPoint"        class="com.test.security.AdminLoginUrlAuthenticationEntryPoint">        <b:property name="loginFormUrl" value="/login" />    </b:bean>     <!-- Jcaptcha相关的配置 -->      <b:bean id="AdmincaptchaService"          class="com.octo.captcha.service.image.DefaultManageableImageCaptchaService">          <b:property name="captchaEngine">              <b:bean class="com.test.security.GMailEngine" />          </b:property>       </b:bean>  </b:beans>

4. spring-base.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:tx="http://www.springframework.org/schema/tx"    xmlns:context="http://www.springframework.org/schema/context"    xmlns:mvc="http://www.springframework.org/schema/mvc"    xmlns:p="http://www.springframework.org/schema/p"    xsi:schemaLocation="http://www.springframework.org/schema/beans                         http://www.springframework.org/schema/beans/spring-beans-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/aop                         http://www.springframework.org/schema/aop/spring-aop-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">    <!-- 开启注解扫描功能 -->     <mvc:annotation-driven />               <context:component-scan base-package="com.test" />    <!-- 配置视图解析器 -->    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"           p:prefix="/WEB-INF/jsp/" p:suffix=".jsp">        <!-- 配置返回的视图类型,因使用了JSP标准标签库,所以必须配置为JstlView -->           <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />     </bean>    <!-- 返回json格式数据的配置,并配置数据编码格式UTF-8 -->    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">              <property name="messageConverters">                  <list >                      <ref bean="mappingJacksonHttpMessageConverter" />                  </list>              </property>          </bean>     <bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">            <property name="supportedMediaTypes">                <list>                    <value>application/json;charset=UTF-8</value>               </list>            </property>      </bean>     <!-- 文件上传相关 -->    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">         <property name="maxUploadSize" value="104857600" />         <property name="maxInMemorySize" value="4096" />         <property name="defaultEncoding" value="UTF-8"></property>    </bean>      <!-- 配置静态资源,使DispatcherServlet不要拦截下面声明的目录 -->      <mvc:resources location="/html/" mapping="/html/**"/>    <mvc:resources location="/css/" mapping="/css/**"/>    <mvc:resources location="/images/" mapping="/images/**"/>     <mvc:resources location="/image/" mapping="/image/**"/>     <mvc:resources location="/js/" mapping="/js/**"/></beans>

5. spring-base.xml

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE hibernate-configuration PUBLIC        "-//Hibernate/Hibernate Configuration DTD 3.0//EN"        "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><hibernate-configuration>    <session-factory>        <!-- 数据库基础配置 -->        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>                <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/test?useUnicode=true&amp;characterEncoding=utf-8</property>        <property name="hibernate.connection.username">root</property>        <property name="hibernate.connection.password">000</property>        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>        <property name="connection.autocommit">true</property>         <!-- 配置C3P0连接池  -->        <property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>        <property name="hibernate.c3p0.max_size">20</property>        <property name="hibernate.c3p0.min_size">5</property>        <property name="hibernate.c3p0.timeout">120</property>        <property name="automaticTestTable">Test</property>        <property name="hibernate.c3p0.max_statements">100</property>        <property name="hibernate.c3p0.idle_test_period">120</property>        <property name="hibernate.c3p0.acquire_increment">1</property>        <property name="c3p0.testConnectionOnCheckout">true</property>        <property name="c3p0.idleConnectionTestPeriod">18000</property>        <property name="c3p0.maxIdleTime">25000</property>        <property name="c3p0.idle_test_period">120</property>        <!-- 配置model与数据库的映射文件 -->        <mapping resource="com/test/database/User.hbm.xml" />           </session-factory></hibernate-configuration>
0 0
原创粉丝点击