news 2026/6/23 1:10:23

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/6/15 11:31:28

如何为自研模型定制TensorRT插件?

如何为自研模型定制TensorRT插件&#xff1f; 在AI产品日益追求极致性能的今天&#xff0c;一个训练好的模型能否真正“跑得快”&#xff0c;往往决定了它是否能从实验室走向生产线。尤其是在自动驾驶、实时视频分析或大规模推荐系统中&#xff0c;哪怕几毫秒的延迟差异&#x…

作者头像 李华
网站建设 2026/6/13 1:10:35

TensorRT动态批处理功能详解及其应用场景

TensorRT动态批处理功能详解及其应用场景 在AI服务从实验室走向生产环境的过程中&#xff0c;一个绕不开的挑战是&#xff1a;如何在高并发、低延迟的要求下&#xff0c;依然保持高效的GPU利用率&#xff1f;尤其是在推荐系统、语音识别或自然语言处理这类请求频繁且输入长度不…

作者头像 李华
网站建设 2026/6/18 18:04:56

边缘计算场景下TensorRT的优势与挑战

边缘计算场景下TensorRT的优势与挑战 在智能制造工厂的质检线上&#xff0c;摄像头每秒捕捉数百帧产品图像&#xff0c;后台系统必须在毫秒级内判断是否存在划痕或装配缺陷。若将这些数据传至云端处理&#xff0c;网络延迟可能超过200ms&#xff0c;不仅无法满足实时性要求&am…

作者头像 李华
网站建设 2026/6/13 9:10:20

FP16 vs INT8:TensorRT精度与速度的平衡之道

FP16 vs INT8&#xff1a;TensorRT精度与速度的平衡之道 在当今AI模型日益庞大的背景下&#xff0c;推理效率已成为决定系统能否落地的关键瓶颈。一个训练得再精准的模型&#xff0c;如果在线上服务中响应延迟高达数百毫秒、吞吐量仅个位数FPS&#xff0c;那它的商业价值几乎为…

作者头像 李华
网站建设 2026/6/17 5:33:31

LeetCode 458 - 可怜的小猪

文章目录摘要描述题解答案题解代码分析先搞清楚“一只猪有多少种状态”为什么是指数关系&#xff1f;Swift 实现思路可运行 Swift Demo 代码示例测试及结果与实际场景结合时间复杂度空间复杂度总结摘要 这道题乍一看是个“喂猪试毒”的奇怪问题&#xff0c;但本质其实是一个信…

作者头像 李华