Spring XML 常用命名空间配置

Spring XML 常用命名空间配置

下面是一个综合性的Spring XML配置样例,展示了各种常用命名空间的使用方式:

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框架中常用的命名空间及其典型用法。实际使用时,可以根据项目需求选择性地包含这些命名空间。

相关推荐
Resean02231 小时前
Spring 源码分析(六)循环依赖与三级缓存
java·spring·缓存
在爪洼山玩泥巴1 小时前
fastjson2 JSONPath 取不存在路径,为什么会抛 “not support”?
java·开发语言·jsonpath·fastjson
星落梦生11 小时前
SpringAOP基础笔记(7)
java·开发语言
金銀銅鐵12 小时前
带您看懂字节码指令(四):一条指令里有什么
java·jvm
最强小杰12 小时前
某 OpenAI 兼容网关上的 `kimi-k3.1-pro`:Cline、Zed 接入与 SSE 排错示例
java·服务器·网络·ai
Flynn Liang13 小时前
流量治理不是限流那么简单:I/O 密集型 Java 服务的 6 层防线
java·微服务·高并发·限流·熔断·系统稳定性·resilience4j
学长毕设13 小时前
基于SpringBoot智慧养老服务平台设计与实现-附源码
java·spring boot·后端
曹牧14 小时前
Java:“刷新雪崩”
java·开发语言
秋天的怀念_David14 小时前
算法一千零一夜 · 第三十三夜 · 跳表
java·程序人生·算法
Forget the Dream15 小时前
Spring AI 2.0 系列(二):第一个 ChatClient——从注入到一次完整调用
java·人工智能·spring boot·ai·springai