基于SSM项目Spring,各种xml配置文件的作用

导入

在我们写ssm项目的时候,经常性的需要写xml配置文件,类如applicationContext.xml,springMVC.xml等。因为Spring本身就是一个轻代码重配置的框架,因此正确的配置,对整个项目尤为重要。

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:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/tx
       http://www.springframework.org/schema/tx/spring-tx.xsd">
    <!-- 开启注解扫描,管理service -->
    <context:component-scan base-package="com.yh.service"></context:component-scan>
    <context:property-placeholder location="classpath:db.properties"/>
    <!-- 配置连接池 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driver}" />
        <property name="jdbcUrl" value="${jdbc.url}" />
        <property name="user" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
    </bean>
    <!-- 把交给IOC管理 SqlSessionFactory -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <!-- 传入PageHelper的插件 -->
        <property name="plugins">
             <array>
                 <!-- 传入插件的对象 -->
                 <bean class="com.github.pagehelper.PageInterceptor">
                     <property name="properties">
                         <props>
                             <prop key="helperDialect">mysql</prop>
                             <prop key="reasonable">true</prop>
                         </props>
                     </property>
                 </bean>
             </array>
         </property>
    </bean>
    <!-- 扫描dao接口 -->
    <bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.yh.dao"/>
    </bean>
    <!-- 配置Spring的声明式事务管理 -->
    <!-- 配置事务管理器 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <tx:annotation-driven transaction-manager="transactionManager"/>
</beans>

这个配置文件主要作用有:
1.配置组件扫描器,使用注解方式开发
2.加载外部的properties配置文件
3.配置数据库连接池
4.配置session工厂 把数据源注入给Session工厂
5.配置mapper接口 MapperScannerConfigurer:配置dao接口的bean,在mybatis-spring.jar包中
6.配置事务管理器
7. 配置AOP

springMVC

web项目启动时,读取web.xml配置文件,首先解析的是applicationContext.xml文件,其次才是sping-mvc.xml文件,sping-mvc.xml文件中主要的工作是:
1.启动注解;
2.扫描controller包注解;
3.静态资源映射;
4.视图解析(defaultViewResolver);
5.文件上传(multipartResolver);
6.返回消息json配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:mvc="http://www.springframework.org/schema/mvc" 
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="
           http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.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.xsd
           http://www.springframework.org/schema/aop
		http://www.springframework.org/schema/aop/spring-aop.xsd
           ">

	<!-- 扫描controller的注解,别的不扫描 -->
	<context:component-scan base-package="com.yh.controller"></context:component-scan>

	<!-- 配置视图解析器 -->
	<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<!-- JSP文件所在的目录 -->
		<property name="prefix" value="/pages/" />
		<!-- 文件的后缀名 -->
		<property name="suffix" value=".jsp" />
	</bean>
	<!-- 设置静态资源不过滤 -->
<!--	<mvc:resources location="/css/" mapping="/css/**" />-->
<!--	<mvc:resources location="/img/" mapping="/img/**" />-->
<!--	<mvc:resources location="/js/" mapping="/js/**" />-->
<!--	<mvc:resources location="/plugins/" mapping="/plugins/**" />-->
	<mvc:default-servlet-handler></mvc:default-servlet-handler>
	<!-- 开启对SpringMVC注解的支持 -->
	<mvc:annotation-driven />
	<!-- 
		支持AOP的注解支持,AOP底层使用代理技术
		JDK动态代理,要求必须有接口
		cglib代理,生成子类对象,proxy-target-class="true" 默认使用cglib的方式
	-->
	<aop:aspectj-autoproxy proxy-target-class="false"/>
</beans>
spring-security.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:security="http://www.springframework.org/schema/security"
       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.xsd
    http://www.springframework.org/schema/security
    http://www.springframework.org/schema/security/spring-security.xsd">

    <!-- 配置不拦截的资源 -->
    <security:http pattern="/login.jsp" security="none"/>
    <security:http pattern="/failer.jsp" security="none"/>
    <security:http pattern="/css/**" security="none"/>
    <security:http pattern="/img/**" security="none"/>
    <security:http pattern="/plugins/**" security="none"/>

    <!--配置具体的规则-->
    <security:http auto-config="true" use-expressions="false">
        <!-- 配置具体的拦截的规则 pattern="请求路径的规则" access="访问系统的人,必须有ROLE_USER的角色" -->
        <security:intercept-url pattern="/**" access="ROLE_USER,ROLE_ADMIN"/>
        <!-- 定义跳转的具体的页面 -->
        <security:form-login
                login-page="/login.jsp"
                login-processing-url="/login"
                username-parameter="username"
                password-parameter="password"
                default-target-url="/index.jsp"
                authentication-failure-url="/failer.jsp"
                authentication-success-forward-url="/pages/main.jsp"
        />
        <!-- 关闭跨域请求 -->
        <security:csrf disabled="true"/>
        <!-- 退出 -->
        <security:logout invalidate-session="true" logout-url="/logout" logout-success-url="/login.jsp" />
    </security:http>
    <!--开启注解的配置文件-->
    <security:global-method-security jsr250-annotations="enabled"/>
<!--    <security:global-method-security secured-annotations="enabled"/>-->
<!--    <security:global-method-security pre-post-annotations="disabled"/>-->
        <!-- 切换成数据库中的用户名和密码 -->
        <security:authentication-manager>
            <security:authentication-provider user-service-ref="userServiceImpl">
                <!-- 配置加密的方式 -->
                <!--<security:password-encoder ref="passwordEncoder"/>-->
            </security:authentication-provider>
        </security:authentication-manager>
    <!-- 配置加密类 -->
    <bean id="passwordEncoder" class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder"/>
    <!-- 提供了入门的方式,在内存中存入用户名和密码
        <security:authentication-manager>
            <security:authentication-provider>
                <security:user-service>
                    <security:user name="admin" password="{noop}admin" authorities="ROLE_USER"/>
                </security:user-service>
            </security:authentication-provider>
        </security:authentication-manager>
    -->
</beans>

在使用spring-security权限认证框架才用到的配置,顾名思义,他的作用非常简单,

1.配置不拦截的资源
2.配置具体的规则
3.开启spring-security注解的配置文件
4.配置加密类等;

5.sqlMapConfig.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!--    <plugins>-->
<!--        <plugin interceptor="com.github.pagehelper.PageInterceptor">-->
<!--            <property name="helperDialect" value="oracle"></property>-->
<!--            <property name="reasonable" value="true"></property>-->
<!--        </plugin>-->
<!--    </plugins>-->
</configuration>

mybatis的配置文件,里面放着数据库连接信息以及自定义sql文件的引用,但是实际上这些内容都交给了Spring去管理,所以有时候空荡荡的,但是他也是不可缺少的配置文件

web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
  <!-- 配置加载类路径的配置文件 classpath*:spring-security.xml -->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:applicationContext.xml,classpath*:spring_security.xml</param-value>
  </context-param>
  <!-- 配置监听器 -->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <!-- 配置监听器,监听request域对象的创建和销毁的 -->
  <!--  <listener>
      <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
    </listener>-->
  <!-- 前端控制器(加载classpath:springmvc.xml 服务器启动创建servlet) -->
  <servlet>
    <servlet-name>dispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <!-- 配置初始化参数,创建完DispatcherServlet对象,加载springmvc.xml配置文件 -->
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:springmvc.xml</param-value>
    </init-param>
    <!-- 服务器启动的时候,让DispatcherServlet对象创建 -->
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>dispatcherServlet</servlet-name>
    <url-pattern>/</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>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>
    <filter-mapping>
    <filter-name>characterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

web.xml文件是用来配置:欢迎页、servlet、filter等的,比如我们上面的各种配置文件,不是写好放在那里就能够使用的,我们需要有监听器,将监听到的信息转发给各个类中
比如:

CharacterEncodingFilter,配置这个是拦截所有的资源并设置好编号格式。

DispatcherServlet,配置这个是拦截所有请求,都交给springmvc转发
SSM项目XML文件配置主要包括以下几个方面: 1. 数据源配置:在Spring中配置数据源,可以使用JDBC、MyBatis等框架。 2. 事务管理器配置:在Spring中配置事务管理器,可以通过注解或XML配置方式进行配置。 3. Spring MVC配置:在Spring中配置Spring MVC,包括控制器、视图解析器、拦截器等。 4. MyBatis配置:在MyBatis中配置数据源、映射器、插件等。 5. 日志配置:在log4j或logback中配置日志输出方式。 下面是一个简单的SSM项目XML配置示例: 1. 数据源配置: ``` <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> <property name="driverClassName" value="${jdbc.driver}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </bean> ``` 2. 事务管理器配置: ``` <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> ``` 3. Spring MVC配置: ``` <context:component-scan base-package="com.example.controller"/> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/views/"/> <property name="suffix" value=".jsp"/> </bean> <mvc:interceptors> <bean class="com.example.interceptor.LoginInterceptor"/> </mvc:interceptors> ``` 4. MyBatis配置: ``` <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="mapperLocations" value="classpath:mapper/*.xml"/> <property name="plugins"> <array> <bean class="com.example.plugin.MyPlugin"/> </array> </property> </bean> <bean id="mapperScanner" class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.example.mapper"/> </bean> ``` 5. 日志配置: ``` <bean id="log4jConfigurer" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean"> <property name="targetClass" value="org.springframework.util.Log4jConfigurer"/> <property name="targetMethod" value="initLogging"/> <property name="arguments"> <list> <value>classpath:log4j.properties</value> </list> </property> </bean> ``` 以上仅是一个简单的示例,实际项目中根据需求可以进行更加详细的配置。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值