Spring框架——applicationContext.xml配置文件头部xmlns

这是一个正常spring框架的applicationContext.xml所包含的头部: 

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx" 
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc" 
       xmlns:task="http://www.springframework.org/schema/task"
       xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.1.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd
        http://www.springframework.org/schema/task
        http://www.springframework.org/schema/task/spring-task-3.1.xsd">

上述包含了常用的spring框架所需要用到内容,主要分为两部分:

(1)xmlns部分:

 这个是每个配置文件必须的部分,也就是spring的根本。

 声明xml文件默认的命名空间,表示未使用其他命名空间的所有标签的默认命名空间。

 声明XML Schema 实例,声明后就可以使用 schemaLocation 属性了。

xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  

这个就是spring配置文件里面需要使用到aop的标签,声明前缀为aop的命名空间,后面的URL用于标示命名空间的地址不会被解析器用于查找信息。

其惟一的作用是赋予命名空间一个惟一的名称。当命名空间被定义在元素的开始标签中时,所有带有相同前缀的子元素都会与同一个命名空间相关联。然后其他比如context(针对组件标签)、MVC(针对mvc标签)、tx(针对事务标签)都一样的意思。

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

(2)xsi:schemaLocation部分:

http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd

是为上面配置的命名空间指定xsd规范文件,这样你在进行下面具体配置的时候就会根据这些xsd规范文件给出相应的提示,比如说每个标签是怎么写的,都有些什么属性是都可以智能提示的,以防配置中出错而不太容易排查,在启动服务的时候也会根据xsd规范对配置进行校验。但是这里需要为你上面xmlns里面配置的mvc、aop、tx等都配置上xsd规范文件。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
SpringapplicationContext.xml 配置文件是一个非常重要的文件,它用于配置Spring应用程序的各种组件和依赖关系。这个文件通常位于Spring项目的 classpath 下,因此Spring容器可以轻松地加载和解析它。 下面是一个简单的 applicationContext.xml 配置文件示例: ```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"> <!-- 配置一个名为 "userService" 的 bean,其类为 com.example.UserService --> <bean id="userService" class="com.example.UserService"/> <!-- 配置一个名为 "userRepository" 的 bean,其类为 com.example.UserRepository --> <bean id="userRepository" class="com.example.UserRepository"/> <!-- 配置一个名为 "userController" 的 bean,其类为 com.example.UserController --> <bean id="userController" class="com.example.UserController"> <!-- 将 userService 和 userRepository 注入到 userController 中 --> <property name="userService" ref="userService"/> <property name="userRepository" ref="userRepository"/> </bean> </beans> ``` 在这个示例中,我们定义了三个 bean: - 名为 `userService` 的 bean,它的类为 `com.example.UserService`。 - 名为 `userRepository` 的 bean,它的类为 `com.example.UserRepository`。 - 名为 `userController` 的 bean,它的类为 `com.example.UserController`。我们使用 `<property>` 元素将 `userService` 和 `userRepository` 注入到 `userController` 中。 通过这种方式,我们可以定义和组装Spring应用程序中的各种组件和依赖关系。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值