Spring 的 xml 标签大体上分为两类,一种是默认标签,一种是自定义标签。默认标签不用额外导入其他命名空间约束的标签,例如 <bean>标签。自定义标签需要额外引入其他命名空间约束,并通过前缀引用的标签,例如<context:propertyplaceholder/>标签。
<?xml version="1.0" encoding="UTF-8"?><beansxmlns="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"></beans>
该命名空间约束下的默认标签如下:
![](https://i-blog.csdnimg.cn/blog_migrate/3dca39de640bd1ec53ea238e66bb2dba.png)
<beans>标签,除了经常用的做为根标签外,还可以嵌套在根标签内,使用profile属性切换开发环境。
<!-- 配置测试环境下,需要加载的Bean实例 --><beansprofile="test"></beans><!-- 配置开发环境下,需要加载的Bean实例 --><beansprofile="dev"></beans>
指定被激活的环境可以使用以下两种方式:
1.使用命令行动态参数,虚拟机参数位置加载 -Dspring.profiles.active=test
2.使用代码的方式设置环境变量 System.setProperty("spring.profiles.active","test")
<import>标签,用于导入其他配置文件,项目变大后,就会导致一个配置文件内容过多,可以将一个配置文件根 据业务某块进行拆分,拆分后,最终通过<import>标签导入到一个主配置文件中,项目加载主配置文件就连同<import>导入的文件一并加载了。
<!--导入用户模块配置文件--><importresource="classpath:UserModuleApplicationContext.xml"/><!--导入商品模块配置文件--><importresource="classpath:ProductModuleApplicationContext.xml"/>
<alias>标签是为某个Bean添加别名,与在标签上使用name属性添加别名的方式一样,我们为UserServiceImpl指定四个别名:aaa、bbb、xxx、yyy。
<!--配置UserService--><beanid="userService"name="aaa,bbb"class="com.itheima.service.impl.UserServiceImpl"><propertyname="userDao"ref="userDao"/></bean><!--指定别名--><aliasname="userService"alias="xxx"/><aliasname="userService"alias="yyy"/>
断点调试,在beanFactory中维护着一个名为aliasMap的Map<String,String>集合,存储别名和beanName 之间的映射关系。
![](https://i-blog.csdnimg.cn/blog_migrate/41e6c2bd9e2b1a9f63f6e780fd0a7b59.png)
Spring的自定义标签需要引入外部的命名空间,并为外部的命名空间指定前缀,使用 <前缀:标签> 形式的标签,称 之为自定义标签,自定义标签的解析流程也是 Spring xml扩展点方式之一。
<!--默认标签--><beanid="userDao"class="com.itheima.dao.impl.UserDaoImpl"/><!--自定义标签--><context:property-placeholder/><mvc:annotation-driven/><dubbo:applicationname="application"/>