SSM框架配置文件详解

1.web.xml配置

启动spring的容器

<!--启动spring的容器-->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

springMvc前端控制器

<!--SpringMvc的前端控制器-->
    <servlet>
        <servlet-name>springMvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!--可以自定义servlet.xml配置文件的位置和名称,默认为WEB-INF目录下,名称为[<servlet-name>]-servlet.xml,如spring-servlet.xml-->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springMvc-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springMvc</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>

字符编码过滤器

<!--字符编码过滤器-->
    <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>
    </filter>
    <filter-mapping>
        <filter-name>characterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

2.spring配置文件

完整配置

<?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:context="http://www.springframework.org/schema/context"
       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-4.3.xsd
                           http://www.springframework.org/schema/context
                           http://www.springframework.org/schema/context/spring-context-4.3.xsd
                           http://www.springframework.org/schema/mvc
                           http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
                           http://www.springframework.org/schema/aop
                           http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
                           http://www.springframework.org/schema/tx
                           http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">

    <!--开启注解模式-->
    <context:annotation-config></context:annotation-config>
    <!--dao配置 start-->
    <!--①加载配置文件-->
    <context:property-placeholder location="classpath:*.properties" />
    <!--②配置数据库连接池-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
        <property name="url" value="${jdbc.url}" />
        <property name="driverClassName" value="${jdbc.driver}" />
    </bean>
    <!-- ③让spring管理sqlsessionfactory 使用mybatis和spring整合包中的 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <!--mybatis配置文件-->
        <property name="configLocation" value="classpath:SqlMapConfig.xml" />
        <!--mapper 配置文件映射-->
    </bean>
    <!--④mapper扫描器-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="lxp.spring.security.mapper" />
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
    </bean>
    <!--dao配置 end-->

    <!--service配置 start-->
    <!--①事务管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!--数据源-->
        <property name="dataSource" ref="dataSource" />
    </bean>
    <!--②通知-->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="save*" propagation="REQUIRED" />
            <tx:method name="insert*" propagation="REQUIRED" />
            <tx:method name="add*" propagation="REQUIRED" />
            <tx:method name="create*" propagation="REQUIRED" />
            <tx:method name="delete*" propagation="REQUIRED" />
            <tx:method name="update*" propagation="REQUIRED" />
            <tx:method name="find*" propagation="SUPPORTS" read-only="true" />
            <tx:method name="select*" propagation="SUPPORTS" read-only="true" />
            <tx:method name="get*" propagation="SUPPORTS" read-only="true" />
        </tx:attributes>
    </tx:advice>
    <!--③切面-->
    <!--<aop:config>-->
        <!--<aop:advisor advice-ref="txAdvice" pointcut-ref="execution(* lxp.spring.security.service.*.*(..))" />-->
    <!--</aop:config>-->
    <!--service配置 end-->

    <!--html视图解析器,必须先配置freemarkerConfig,并且html没有prefix前缀属性-->
    <bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
        <property name="templateLoaderPath">
            <value>/WEB-INF/static/templates/</value>
        </property>
        <property name="freemarkerSettings">
            <props>
                <prop key="template_update_delay">0</prop>
                <prop key="default_encoding">UTF-8</prop>
                <prop key="number_format">0.##########</prop>
                <prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop>
                <prop key="classic_compatible">true</prop>
                <prop key="template_exception_handler">ignore</prop>
            </props>
        </property>
    </bean>
</beans>

开启注解模式

<!--开启注解模式-->
<context:annotation-config></context:annotation-config>

加载属性文件


<!--①加载配置文件-->
 <context:property-placeholder location="classpath:*.properties" />

注:路径填写你项目的属性文件地址

配置数据库连接池

<!--②配置数据库连接池-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
	<property name="username" value="${jdbc.username}" />
	<property name="password" value="${jdbc.password}" />
	<property name="url" value="${jdbc.url}" />
	<property name="driverClassName" value="${jdbc.driver}" />
</bean>

注:${jdbc.*}对应properties文件配置的值

配置sqlSessionFactory

<!-- ③让spring管理sqlsessionfactory 使用mybatis和spring整合包中的 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
     <property name="dataSource" ref="dataSource" />
     <!--mybatis配置文件-->
     <property name="configLocation" value="classpath:SqlMapConfig.xml" />
     <!--mapper 配置文件映射-->
 </bean>

配置mapper扫描器

<!--④mapper扫描器-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <property name="basePackage" value="lxp.spring.security.mapper" />
    <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>

配置事务

<!--①事务管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <!--数据源-->
        <property name="dataSource" ref="dataSource" />
    </bean>
    <!--②通知-->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="save*" propagation="REQUIRED" />
            <tx:method name="insert*" propagation="REQUIRED" />
            <tx:method name="add*" propagation="REQUIRED" />
            <tx:method name="create*" propagation="REQUIRED" />
            <tx:method name="delete*" propagation="REQUIRED" />
            <tx:method name="update*" propagation="REQUIRED" />
            <tx:method name="find*" propagation="SUPPORTS" read-only="true" />
            <tx:method name="select*" propagation="SUPPORTS" read-only="true" />
            <tx:method name="get*" propagation="SUPPORTS" read-only="true" />
        </tx:attributes>
    </tx:advice>
    <!--③切面-->
    <!--<aop:config>-->
        <!--<aop:advisor advice-ref="txAdvice" pointcut-ref="execution(* lxp.spring.security.service.*.*(..))" />-->
    <!--</aop:config>-->

配置模板引擎

<!--html视图解析器,必须先配置freemarkerConfig,并且html没有prefix前缀属性-->
<bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
    <property name="templateLoaderPath">
        <value>/WEB-INF/static/templates/</value>
    </property>
    <property name="freemarkerSettings">
        <props>
            <prop key="template_update_delay">0</prop>
            <prop key="default_encoding">UTF-8</prop>
            <prop key="number_format">0.##########</prop>
            <prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop>
            <prop key="classic_compatible">true</prop>
            <prop key="template_exception_handler">ignore</prop>
        </props>
    </property>
</bean>

注:更换对应模板引擎实现类

3.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" xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       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-4.3.xsd
                        http://www.springframework.org/schema/mvc
                        http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
                        http://www.springframework.org/schema/context
                        http://www.springframework.org/schema/context/spring-context-4.3.xsd
                        http://www.springframework.org/schema/aop
                        http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
                        http://www.springframework.org/schema/tx
                        http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
        <!--配置包扫描路径-->
        <context:component-scan base-package="lxp.spring.security" />


        <bean id="htmlViewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
                <property name="suffix" value=".html" />
                <property name="order" value="0" />
                <property name="contentType" value="text/html;charset=UTF-8" />
                <property name="requestContextAttribute" value="request" />
                <property name="exposeSpringMacroHelpers" value="true" />
                <property name="exposeRequestAttributes" value="true" />
                <property name="exposeSessionAttributes" value="true" />
        </bean>
        <!--jsp视图解析器-->
        <!--<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">-->
                <!--<property name="prefix" value="/WEB-INF/static/templates" />-->
                <!--<property name="suffix" value=".jsp" />-->
                <!--<property name="order" value="2" />-->
        <!--</bean>-->

        <!--配置注解驱动-->
        <mvc:annotation-driven />
        <mvc:default-servlet-handler />
        <!--配置拦截器-->

        <!-- 定义全局异常处理器 -->

        <!--配置上传文件数据解析器-->

        <!--静态资源处理-->
        <mvc:resources mapping="/css/**" location="/css/" />
        <mvc:resources mapping="/js/**" location="/js/" />
        <mvc:resources mapping="/images/**" location="/images/" />
</beans>

配置包扫描路径

<context:component-scan base-package="lxp.spring.security" />

配置htmlViewResolver

<bean id="htmlViewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
        <property name="suffix" value=".html" />
        <property name="order" value="0" />
        <property name="contentType" value="text/html;charset=UTF-8" />
        <property name="requestContextAttribute" value="request" />
        <property name="exposeSpringMacroHelpers" value="true" />
        <property name="exposeRequestAttributes" value="true" />
        <property name="exposeSessionAttributes" value="true" />
</bean>

配置注解驱动

<mvc:annotation-driven />
<mvc:default-servlet-handler />

静态资源处理

<mvc:resources mapping="/css/" location="/css/" />
<mvc:resources mapping="/js/
" location="/js/" />
<mvc:resources mapping="/images/**" location="/images/" />

注:路径需换成你项目对应的静态资源路径

数据库连接配置文件 db.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/base?useUnicode=true&characterEncoding=utf-8
jdbc.username=root
jdbc.password=root

freemarker模板属性配置文件

tag_syntax=auto_detect  
template_update_delay=60  
default_encoding=UTF-8  
output_encoding=UTF-8  
locale=zh_CN  
date_format=yyyy-MM-dd  
time_format=HH:mm:ss  
datetime_format=yyyy-MM-dd HH:mm:ss  
classic_compatible=true  
template_exception_handler=ignore  

log4j.properties日志配置文件

### set log levels - for more verbose logging change 'info' to 'debug' ###
log4j.rootLogger=debug,console

### direct log messages to stdout ###
#log4j.appender.stdout = org.apache.log4j.ConsoleAppender
#log4j.appender.stdout.Target = System.out
#log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
#log4j.appender.stdout.layout.ConversionPattern = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n
#控制台输出配置项
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.Threshold=DEBUG
log4j.appender.console.ImmediateFlush=true
log4j.appender.console.Target=System.out
log4j.appender.console.layout.ConversionPattern=[%p] [security-web] %d(%r)--%l: %m%n
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值