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

相关文章:

  • C语言预处理命令详解
  • LeetCode - 904. 水果成篮
  • 《 第三章-招式初成》 C++修炼生涯笔记(基础篇)程序流程结构
  • HE023784R23B530 PP D113 B01-25-111000: AC 800PEC 静态励磁系统UNITROL 6000 X-power
  • 让高端装备“先跑起来”:虚拟仿真验证平台重塑研制流程
  • QT log4qt 无法生成日志到中文的路径中的解决方案
  • 鸿蒙app 开发中 如何 看 app 页面的ui结构
  • 信息学奥赛一本通 1543:【例 3】与众不同
  • mapbox进阶,切片网格生成实现
  • 洛谷 数楼梯 高精度
  • LangChain--(2)
  • 【Python系列】Python 中 yield 关键字
  • SpringCloudAlibaba怎么学?
  • Angular入门的环境准备步骤工作
  • 智能客服系统开发方案:RAG+多智能体技术实现
  • 【插件推荐】WebRTC Protect — 防止 IP 泄漏
  • Vite 及生态环境:新时代的构建工具
  • oceanbase导出导入数据csv
  • 基于 STM32 七段数码管显示模块详解
  • 圆与 π | 从几何之美到数学与物理的奇妙之旅
  • 县级门户网站建设的报告/下载百度网盘app
  • 甘肃城乡建设局网站/买友情链接
  • 网站服务器租赁哪家好/百度做广告怎么收费
  • ie浏览器打不开建设银行网站/职业技能培训有哪些
  • 2022百度seo最新规则/seo实战培训中心
  • 昆山企业网站制作公司/十大最靠谱培训机构