SSM框架配置文件详解

SSM框架配置文件详解

在SSM项目当中,所需要的配置文件总共有以下三个:

①web.xml ②applicationContext.xml ③springmvc.xml

以及两个properties文件jdbc.properties和log4j.properties

1.web.xml

web.xml是ssm项目当中最重要的一个配置文件,当服务启动时会首先加载web.xml这个文件,里面包括了对前端控制器、字符编码的配置

案例:

<?xml version="1.0" encoding="UTF-8"?>
<web-app

        version="3.0"  
        xmlns="http://java.sun.com/xml/ns/javaee"  
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
      metadata-complete="true">

  <display-name>Blog</display-name>
  <!--当请求项目名时跳转的页面-->
  <welcome-file-list>
   <welcome-file>a.jsp</welcome-file>
  </welcome-file-list>
    
    <!-- Spring配置文件 -->
  <context-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>classpath:applicationContext.xml</param-value>
  </context-param>
    
  <!-- 各种过滤器的定义:包括编码过滤器、rest风格过滤器和Shiro过滤器等 -->
  <!-- Shiro过滤器定义 -->
  <filter>
   <filter-name>shiroFilter</filter-name>
   <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
   <init-param>
   <!-- 该值缺省为false,表示生命周期由SpringApplicationContext管理,设置为true表示由ServletContainer管理 -->
      <param-name>targetFilterLifecycle</param-name>
      <param-value>true</param-value>
   </init-param>
  </filter>
  <filter-mapping>
   <filter-name>shiroFilter</filter-name>
   <url-pattern>/*</url-pattern>
  </filter-mapping>
  <!-- 编码过滤器 -->
  <filter>
   <filter-name>encodingFilter</filter-name>
   <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
   <async-supported>true</async-supported>
   <init-param>
      <param-name>encoding</param-name>
      <param-value>UTF-8</param-value>
   </init-param>
  </filter>
  <filter-mapping>
   <filter-name>encodingFilter</filter-name>
   <url-pattern>/*</url-pattern>
  </filter-mapping>
    
  <!-- 各种监听器的定义:包括Spring监听器 -->
  <!-- Spring监听器 -->
  <listener>
   <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <listener>
   <listener-class>com.blog.service.impl.InitComponent</listener-class>
  </listener>
  
  <!-- 添加对SpringMVC的支持 -->
  <servlet>
   <servlet-name>springMVC</servlet-name>
   <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
   <init-param>
      <param-name>contextConfigLocation</param-name>
       <!-- 此处不配置 默认找 /WEB-INF/[servlet-name]-servlet.xml -->
      <param-value>classpath:spring-mvc.xml</param-value>
   </init-param>
   <load-on-startup>1</load-on-startup>
   <async-supported>true</async-supported>
  </servlet>
  
  <servlet-mapping>
     <!--
        1:*.do  拦截以.do结尾的请求 (不拦截 jsp png jpg .js .css)
        2:/ 拦截所有请求 (不拦截.jsp) 建议使用此种 方式 (拦截 .js.css .png) (放行静态资源)
        3:/* 拦截所有请求(包括.jsp) 此种方式 不建议使用
        -->
   <servlet-name>springMVC</servlet-name>
   <url-pattern>*.do</url-pattern>
  </servlet-mapping>
  
  <servlet-mapping>
   <servlet-name>springMVC</servlet-name>
   <url-pattern>*.html</url-pattern>
  </servlet-mapping>
  
</web-app>

2.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:p="http://www.springframework.org/schema/p"
   xmlns:aop="http://www.springframework.org/schema/aop"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:jee="http://www.springframework.org/schema/jee"
   xmlns:tx="http://www.springframework.org/schema/tx"
   xsi:schemaLocation="
      http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
      http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
      http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"
>

   <!-- 配置数据源 对应的druid连接池-->
   <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
      <property name="url" value="jdbc:mysql://localhost:3306/db_blog?useUnicode=true&amp;characterEncoding=UTF-8"/>
      <property name="username" value="root"/>
      <property name="password" value="123456"/>
   </bean>

   <!-- Spring整合MyBatis ,配置MyBatis的sqlSessionFactory -->
   <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
      <property name="dataSource" ref="dataSource"/>
      <!-- 自动扫描mapper.xml文件 -->
      <property name="mapperLocations" value="classpath:com/blog/mappers/*.xml"/>
      <!-- MyBatis配置文件 -->
      <property name="configLocation" value="classpath:mybatis-config.xml"></property>
   </bean>
   
   <!-- DAO接口所在包名,Spring会自动扫描这个包下面的类 -->
   <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
      <property name="basePackage" value="com.blog.dao"/>
      <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
   </bean>
   
   <!-- 事务管理 -->
   <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
      <property name="dataSource" ref="dataSource"/>
   </bean>
   
   <!-- 自定义Realm -->
   <bean id="myRealm" class="com.blog.realm.MyRealm"/>
   
   <!-- 安全管理器 -->
   <bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
      <property name="realm" ref="myRealm"/>
   </bean>
   
   <!-- Shiro过滤器 -->
   <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
      <!-- Shiro的核心安全接口,这个属性是必须的 -->
      <property name="securityManager" ref="securityManager"/>
      <!-- 身份认证失败,跳转到登陆页面的配置 -->
      <property name="loginUrl" value="/login.jsp"/>
      <property name="filterChainDefinitions">
         <value>
            /login=anon
            /admin/**=authc
         </value>
      </property>
   </bean>
   
   <!-- 保证实现了Shiro内部lifecycle函数的bean执行 -->
   <bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor"/>
   
   <!-- 开启Shiro注解 -->
   <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" depends-on="lifecycleBeanPostProcessor"/>
   
   <bean class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
      <property name="securityManager" ref="securityManager"/>
   </bean>
   
   <!-- 配置事务通知属性 -->
   <tx:advice id="txAdvice" transaction-manager="transactionManager">
      <!-- 定义事务传播属性 -->
      <tx:attributes>
         <tx:method name="insert*" propagation="REQUIRED"/>
         <tx:method name="update*" propagation="REQUIRED"/>
         <tx:method name="edit*" propagation="REQUIRED"/>
         <tx:method name="save*" propagation="REQUIRED"/>
         <tx:method name="add*" propagation="REQUIRED"/>
         <tx:method name="new*" propagation="REQUIRED"/>
         <tx:method name="set*" propagation="REQUIRED"/>
         <tx:method name="remove*" propagation="REQUIRED"/>
         <tx:method name="delete*" propagation="REQUIRED"/>
         <tx:method name="change*" propagation="REQUIRED"/>
         <tx:method name="check*" propagation="REQUIRED"/>
         <tx:method name="get*" propagation="REQUIRED" read-only="true"/>
         <tx:method name="find*" propagation="REQUIRED" read-only="true"/>
         <tx:method name="load*" propagation="REQUIRED" read-only="true"/>
         <tx:method name="*" propagation="REQUIRED" read-only="true"/>     
      </tx:attributes>
   </tx:advice>
   
   <!-- 配置事务切面 -->
   <aop:config>
      <aop:pointcut expression="execution(* com.blog.service.*.*(..))" id="serviceOperation"/>
      <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation"/>   
   </aop:config>
   
   <!-- 自动扫描 -->
   <context:component-scan base-package="com.blog.service"/>  
</beans>

3.springmvc.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:aop="http://www.springframework.org/schema/aop"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:jee="http://www.springframework.org/schema/jee"
   xmlns:tx="http://www.springframework.org/schema/tx"
   xmlns:mvc="http://www.springframework.org/schema/mvc"
   xsi:schemaLocation="
      http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
      http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
      http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
      http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"
>

<!-- 注解驱动,使得访问路径与方法的匹配可以通过注解配置 -->
<mvc:annotation-driven/>

<!-- 处理静态资源 -->
<mvc:resources location="/static/" mapping="/static/**"/>

<!-- 视图解析器 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
   <property name="prefix" value="/"/>
   <property name="suffix" value=".jsp"/>
</bean>

<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
   <property name="defaultEncoding" value="UTF-8"/>
   <property name="maxUploadSize" value="10000000"></property>
</bean>

<!-- 使用注解的包,包括子集 -->
<context:component-scan base-package="com.blog.controller"/>
</beans>

4.log4j.properties

log4j.rootLogger=DEBUG,Console

log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout=org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=%d [%t] [%p] [%c] - %m%n

log4j.logger.java.sql.ResultSet=INFO
log4j.logger.org.apche=INFO
log4j.logger.java.sql.Connection=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG

5.jdbc.properties(可有可没有,如果没有,直接在applicationContext.xml中配置,如果有的话,则需要在applicationContext.xml中引入)

mysql

driverClass=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql://localhost:3306/c2009
user=root
password=123456

DBPool

initialPoolSize=30
minPoolSize=10
maxPoolSize=100
acquireIncrement=5
maxStatements=1000
maxStatementsPerConnection=10

  • 5
    点赞
  • 71
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值