news 2026/5/1 9:05:11

Spring的命名空间

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Spring的命名空间

Spring的命名空间

一、总述

二、具体实验

2.1引入自定义命名空间

首先要加入要引入的东西的坐标(也就是pom文件中的依赖,以引入springmvc为例)

<dependency><groupId>com.alibaba</groupId><artifactId>druid</artifactId><version>1.1.23</version></dependency>

其次是改Spring的配置文件:

刚开始是这样:

<?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:util="http://www.springframework.org/schema/util"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd“><bean id="userService"class="com.itheima.service.impl.UserServiceImpl2"autowire="byType"></bean><bean id="userDao"class="com.itheima.dao.impl.UserDaoImpl"/></beans>

要引入mvc,只需要加三个地方(其实如果是Spring的组件的话都是类似的,可以复制粘贴,然后改不同的即可,不是Spring的组件就得自己去找命名空间及命名空间约束了)

命名空间

xmlns:mvc="http://www.springframework.org/schema/mvc"

命名空间、schane地址(jar包命名空间对应的虚拟地址,下面两个必须是成对的):

http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd

最后是这样:

<?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:util="http://www.springframework.org/schema/util"xmlns:mvc="http://www.springframework.org/schema/mvc"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd"><bean id="userService"class="com.itheima.service.impl.UserServiceImpl2"autowire="byType"></bean><bean id="userDao"class="com.itheima.dao.impl.UserDaoImpl"/></beans>

然后就可以引入mvc标签了:

<?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:util="http://www.springframework.org/schema/util"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd<mcv:annotation-driven/><bean id="userService"class="com.itheima.service.impl.UserServiceImpl2"autowire="byType"></bean><bean id="userDao"class="com.itheima.dao.impl.UserDaoImpl"/></beans>

2.2标签配置不同的环境

使用方式:

1、在配置文件中使用beans的profile属性来进行声明:

<beans profile="dev"><!--再在环境里面像正常一样配置bean--><beanclass="com.itheima.service.impl.UserServiceImpl2"id="userServiceImpl2"/></beans><!--再配置一个测试环境--><beans profile="test"><beanclass="com.itheima.dao.impl.UserDaoImpl"id="userDao"/></beans>

2、使用System.setProperty(“spring.profiles.active”,“环境名来进行调用”),例如:

System.setProperty("spring.profiles.active","test");
System.setProperty("spring.profiles.active","dev");

测试:

packagecom.itheima.test;importcom.itheima.dao.impl.UserDaoImpl;importcom.itheima.service.UserService;importorg.springframework.beans.factory.support.DefaultListableBeanFactory;importorg.springframework.beans.factory.xml.XmlBeanDefinitionReader;importorg.springframework.context.ApplicationContext;importorg.springframework.context.support.ClassPathXmlApplicationContext;importjavax.xml.bind.annotation.XmlAccessOrder;publicclassBeanFactoryTest{publicstaticvoidmain(String[]args){System.setProperty("spring.profiles.active","test");// 直接使用ApplicationContex来进行加载ApplicationContextapplicationContext=newClassPathXmlApplicationContext("applicationContext_beanfac.xml");System.out.println(applicationContext.getBean("userDao1"));}}
### 2.3使用标签将子配置文件导入主配置文件中

使用方式:

1、在子配置文件中定义bean

applicationContextOrder.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"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><bean id="userService"class="com.itheima.service.impl.UserServiceImpl"></bean></beans>

applicationContextUser.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"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"><bean id="userDao"class="com.itheima.dao.impl.UserDaoImpl"></bean></beans>

2、在主配置文件中使用标签

<?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:util="http://www.springframework.org/schema/util"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd<importresource="classpath:applicationContextOrder.xml"/><importresource="classpath:applicationContextUser.xml"/></beans>

测试及结果:

packagecom.itheima.test;importcom.itheima.dao.impl.UserDaoImpl;importcom.itheima.service.UserService;importorg.springframework.beans.factory.support.DefaultListableBeanFactory;importorg.springframework.beans.factory.xml.XmlBeanDefinitionReader;importorg.springframework.context.ApplicationContext;importorg.springframework.context.support.ClassPathXmlApplicationContext;importjavax.xml.bind.annotation.XmlAccessOrder;publicclassBeanFactoryTest{publicstaticvoidmain(String[]args){ApplicationContextapplicationContext=newClassPathXmlApplicationContext("applicationContext_beanfac.xml");System.out.println(applicationContext.getBean("userService"));System.out.println(applicationContext.getBean("userDao"));}}
### 2.4使用标签进行取别名

但是使用name属性也可以,只不过 是一个单独的标签而已。

实验:

<beanclass="com.itheima.dao.impl.UserDaoImpl"id="userDao"name="aaa,bbb"/><!--使用alias标签起别名--><alias name="userDao"alias="xxx"/><alias name="userDao"alias="yyy"/>

结果:

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/29 21:49:36

如何监控和调优TensorRT推理引擎的性能?

如何监控和调优TensorRT推理引擎的性能&#xff1f; 在构建高并发、低延迟的AI服务时&#xff0c;一个常见的挑战是&#xff1a;为什么训练精度达标的模型&#xff0c;部署后却跑不快&#xff1f;明明GPU利用率显示还有余量&#xff0c;推理延迟却始终下不来。这背后往往不是硬…

作者头像 李华
网站建设 2026/4/24 23:17:23

自动驾驶中的实时推理需求与TensorRT解决方案

自动驾驶中的实时推理需求与TensorRT解决方案 在一辆L4级自动驾驶汽车穿梭于城市街道的瞬间&#xff0c;它的“大脑”每秒要处理来自多个摄像头、激光雷达和毫米波雷达的海量数据。从识别前方突然闯出的行人&#xff0c;到预测邻车变道意图&#xff0c;每一个决策背后都依赖深度…

作者头像 李华
网站建设 2026/4/22 4:50:02

基于TensorRT的多模态大模型推理架构设计

基于TensorRT的多模态大模型推理架构设计 在智能客服、内容推荐和自动驾驶等前沿场景中&#xff0c;多模态大模型正逐步成为核心技术支柱。像CLIP、Flamingo、Qwen-VL这类能够同时理解图像与文本的模型&#xff0c;虽然具备强大的语义建模能力&#xff0c;但其庞大的参数量和复…

作者头像 李华
网站建设 2026/5/1 8:22:52

基于TensorRT的A/B测试平台构建方法

基于TensorRT的A/B测试平台构建方法 在推荐系统、广告排序和语音交互等实时性要求极高的AI服务中&#xff0c;模型上线前的决策不能再仅依赖离线指标。一个新版本模型即便在测试集上准确率提升了0.5%&#xff0c;如果导致线上P99延迟翻倍&#xff0c;也可能被直接否决。这种“…

作者头像 李华
网站建设 2026/5/1 1:13:14

超详细教程:使用Docker运行TensorRT镜像

使用Docker运行TensorRT镜像&#xff1a;从零构建高性能推理环境 在当今AI系统部署的实践中&#xff0c;一个常见的困境是&#xff1a;模型在实验室里表现优异&#xff0c;一旦上线却频频出现延迟高、吞吐低、资源占用大等问题。更令人头疼的是&#xff0c;“在我机器上能跑”…

作者头像 李华
网站建设 2026/5/1 3:44:41

2025技术实战总结:大模型如何重塑软件开发与硬件设计—从百页文档秒变代码到芯片抗干扰设计

摘要&#xff1a;2025年&#xff0c;大模型技术已从通用的对话助手进化为垂直领域的深度生产力工具。本文通过两个硬核技术实战案例——基于108页复杂接口文档自动生成工业级通信代码&#xff0c;以及辅助抗微波RFID芯片天线设计&#xff0c;深度复盘了大模型在软件工程与硬件研…

作者头像 李华