Spring XML 常用命名空间配置

发布于:2025-06-14 ⋅ 阅读:(19) ⋅ 点赞:(0)

Spring XML 常用命名空间配置

下面是一个综合性的Spring 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:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:cache="http://www.springframework.org/schema/cache"
       xmlns:task="http://www.springframework.org/schema/task"
       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/aop 
           http://www.springframework.org/schema/aop/spring-aop.xsd
           http://www.springframework.org/schema/tx 
           http://www.springframework.org/schema/tx/spring-tx.xsd
           http://www.springframework.org/schema/mvc 
           http://www.springframework.org/schema/mvc/spring-mvc.xsd
           http://www.springframework.org/schema/cache 
           http://www.springframework.org/schema/cache/spring-cache.xsd
           http://www.springframework.org/schema/task 
           http://www.springframework.org/schema/task/spring-task.xsd">

    <!-- 1. 核心beans命名空间示例 -->
    <bean id="userService" class="com.example.service.UserServiceImpl">
        <property name="userDao" ref="userDao"/>
    </bean>
    
    <bean id="userDao" class="com.example.dao.UserDaoImpl"/>

    <!-- 2. context命名空间示例 - 组件扫描和属性文件加载 -->
    <context:component-scan base-package="com.example"/>
    <context:property-placeholder location="classpath:config.properties"/>

    <!-- 3. AOP命名空间示例 -->
    <aop:config>
        <aop:aspect ref="loggingAspect">
            <aop:pointcut id="serviceMethods" expression="execution(* com.example.service.*.*(..))"/>
            <aop:before pointcut-ref="serviceMethods" method="logBefore"/>
            <aop:after-returning pointcut-ref="serviceMethods" method="logAfterReturning"/>
        </aop:aspect>
    </aop:config>
    
    <bean id="loggingAspect" class="com.example.aspect.LoggingAspect"/>

    <!-- 4. 事务管理命名空间示例 -->
    <tx:annotation-driven transaction-manager="transactionManager"/>
    
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

    <!-- 5. MVC命名空间示例 -->
    <mvc:annotation-driven/>
    <mvc:resources mapping="/resources/**" location="/resources/"/>
    <mvc:view-controller path="/" view-name="index"/>

    <!-- 6. 缓存命名空间示例 -->
    <cache:annotation-driven cache-manager="cacheManager"/>
    
    <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
        <property name="cacheManager" ref="ehcache"/>
    </bean>
    
    <bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
        <property name="configLocation" value="classpath:ehcache.xml"/>
    </bean>

    <!-- 7. 任务调度命名空间示例 -->
    <task:annotation-driven executor="taskExecutor" scheduler="taskScheduler"/>
    <task:executor id="taskExecutor" pool-size="5"/>
    <task:scheduler id="taskScheduler" pool-size="10"/>
    
    <!-- 8. 数据源配置 -->
    <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
        <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>

    <!-- 9. 其他bean配置 -->
    <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basenames">
            <list>
                <value>messages</value>
            </list>
        </property>
    </bean>
</beans>

各命名空间说明:

  1. 核心beans命名空间 (xmlns="http://www.springframework.org/schema/beans")

    • 定义和管理Spring bean
    • 使用<bean>元素配置组件
  2. context命名空间 (xmlns:context="http://www.springframework.org/schema/context")

    • <context:component-scan>: 自动扫描组件
    • <context:property-placeholder>: 加载属性文件
    • <context:annotation-config>: 激活注解配置
  3. aop命名空间 (xmlns:aop="http://www.springframework.org/schema/aop")

    • 配置面向切面编程
    • 定义切面、切点和通知
  4. tx命名空间 (xmlns:tx="http://www.springframework.org/schema/tx")

    • 声明式事务管理
    • <tx:annotation-driven>: 启用注解驱动的事务管理
  5. mvc命名空间 (xmlns:mvc="http://www.springframework.org/schema/mvc")

    • Spring MVC配置
    • <mvc:annotation-driven>: 启用MVC注解
    • <mvc:resources>: 静态资源处理
  6. cache命名空间 (xmlns:cache="http://www.springframework.org/schema/cache")

    • 声明式缓存配置
    • <cache:annotation-driven>: 启用缓存注解
  7. task命名空间 (xmlns:task="http://www.springframework.org/schema/task")

    • 任务调度和异步方法执行
    • 配置任务执行器和调度器
  8. util命名空间 (未在示例中展示)

    • 提供实用工具配置元素
    • 如集合、常量等配置
  9. jdbc命名空间 (未在示例中展示)

    • 简化JDBC配置
    • 嵌入式数据库支持

这个配置样例展示了Spring框架中常用的命名空间及其典型用法。实际使用时,可以根据项目需求选择性地包含这些命名空间。