news 2026/4/23 13:15:51

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/23 9:45:21

AI推理平台构建:为何离不开TensorRT?

AI推理平台构建&#xff1a;为何离不开TensorRT&#xff1f; 在自动驾驶的感知系统中&#xff0c;每毫秒都关乎安全&#xff1b;在电商大促的推荐引擎里&#xff0c;每一次响应延迟都可能意味着订单流失。当深度学习模型走出实验室&#xff0c;进入真实业务场景时&#xff0c;人…

作者头像 李华
网站建设 2026/4/23 9:44:36

如何验证TensorRT转换后模型的准确性?

如何验证TensorRT转换后模型的准确性&#xff1f; 在自动驾驶系统中&#xff0c;一个原本准确率高达99.2%的目标检测模型&#xff0c;部署到车载T4推理卡上后&#xff0c;突然开始频繁漏检行人。排查发现&#xff0c;并非模型本身问题&#xff0c;而是经过TensorRT的INT8量化后…

作者头像 李华
网站建设 2026/4/23 9:46:23

大模型推理资源调度策略与TensorRT集成

大模型推理资源调度策略与TensorRT集成 在当今大模型加速落地的背景下&#xff0c;一个尖锐的问题摆在工程团队面前&#xff1a;如何让千亿参数的模型既跑得快、又省资源&#xff1f;很多团队最初直接将训练好的PyTorch模型部署上线&#xff0c;结果发现单请求延迟动辄上百毫秒…

作者头像 李华
网站建设 2026/4/23 11:12:40

深度访谈:10位文化行业大佬谈提示工程的价值

当AI遇见文化&#xff1a;10位行业大佬揭秘提示工程如何重构内容创作与传承 摘要 凌晨3点&#xff0c;作家林深盯着电脑屏幕上的空白文档发呆——这是他连续一周卡在小说大纲里了。直到他输入一行提示词&#xff1a;“以民国旧书店为背景&#xff0c;生成包含悬疑元素的故事大纲…

作者头像 李华
网站建设 2026/4/23 11:27:55

大模型推理服务降本增效:TensorRT实战案例

大模型推理服务降本增效&#xff1a;TensorRT实战案例 在大模型落地生产环境的今天&#xff0c;一个现实问题正困扰着众多AI团队&#xff1a;明明训练效果惊艳&#xff0c;但一上线就“卡成PPT”。某推荐系统跑BERT-base&#xff0c;单次推理延迟45ms&#xff0c;QPS刚过200&a…

作者头像 李华
网站建设 2026/4/23 5:23:20

TensorRT与原生PyTorch性能对比实验报告

TensorRT与原生PyTorch性能对比实验报告 在现代AI系统部署中&#xff0c;一个训练好的模型从实验室走向生产环境时&#xff0c;往往面临“推理效率”的严峻考验。以图像分类任务为例&#xff0c;ResNet50 在 PyTorch 中轻松跑通测试后&#xff0c;一旦接入实时视频流服务&#…

作者头像 李华