当前位置: 首页 > news >正文

Spring XML 常用命名空间配置

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.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsdhttp://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsdhttp://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsdhttp://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框架中常用的命名空间及其典型用法。实际使用时,可以根据项目需求选择性地包含这些命名空间。

http://www.xdnf.cn/news/1018117.html

相关文章:

  • React Native 项目实战 —— 记账本应用开发指南
  • 【React Native 性能优化:虚拟列表嵌套 ScrollView 问题全解析】
  • Java-数组-异常(基础)
  • 包含40个购物网站UI界面的psd适用于电商项目
  • 在 Linux 系统中通过 yum 安装 Sublime Text
  • 平压印刷机设计原理与关键技术研究
  • 网络安全防护:点击劫持
  • Linux 系统设置时区
  • Token 的流动性:为什么它是项目的关键?
  • 传染病传播模拟:基于社会接触网络的疫情预测模型
  • 【设计模式】UML图与工厂模式
  • 德信软件,512天,我的创作纪念日
  • RocketMQ总结
  • Rimini Street宣布将SAP ECC和S/4HANA支持延长至2040年,为企业提供替代路径
  • 基于大模型预测过敏性紫癜的技术方案大纲
  • Rust 学习笔记2025.6.13
  • Excel词典(xllex.dll)文件丢失或损坏导致功能异常?别慌!专业修复策略来了!
  • 排序--计数排序
  • Active Directory Certificate Services(AD CS)攻击基础
  • Java单例模式有几种实现方式
  • 为什么py文件打包后大小会增加很多?
  • 亚马逊云 Lambda 容器化部署教程
  • Rust 学习笔记:关于处理任意数量的 future 的练习题
  • Redis 使用场景、缓存什么数据?怎么保证数据一致性?
  • 自定义线程池 4.0
  • 基于51单片机的简易售货机系统
  • 使用 C/C++ 和 OpenCV 构建智能停车场视觉管理系统
  • 在GIS 工作流中实现数据处理(4)
  • 用Java实现常见排序算法详解
  • 玩转Docker | 使用Docker部署vaultwarden密码管理器