zoukankan      html  css  js  c++  java
  • 【深入JAVA EE】Spring配置文件解析

    在阅读的过程中有不论什么问题,欢迎一起交流

    邮箱:1494713801@qq.com   

    QQ:1494713801

     

    一、Spring头信息

    Spring配置文件的头部信息通常是固定不变的。但每个标签都有自己的含义。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"

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

                    xmlns:cache="http://www.springframework.org/schema/cache"
                  
     xmlns:p="http://www.springframework.org/schema/p
                    
    xmlns:task="http://www.springframework.org/schema/task"
                   
    xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsd
      http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsd
      http://www.springframework.org/schema/taskhttp://www.springframework.org/schema/task/spring-task-3.1.xsd
      http://www.springframework.org/schema/cachehttp://www.springframework.org/schema/cache/spring-cache-3.1.xsd"> 

    <!— xml配置内容 -->

    </beans>

     

    1XML Schema命名空间作用:
    1)、避免命名冲突,像Java中的package一样

    2)、将不同作用的标签分门别类(像context命名空间针对组件的标签)

    2、代码解释:

    1)、xmlns="http://www.springframework.org/schema/beans"

    声明xml文件默认的命名空间,表示未使用其它命名空间的全部标签的默认命名空间。

    2)、xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    声明XML Schema 实例,声明后就能够使用 schemaLocation属性了

    3)、xmlns:context="http://www.springframework.org/schema/context"

     

    4)、xmlns:cache="http://www.springframework.org/schema/cache"


    5
    )、xmlns:p="http://www.springframework.org/schema/p

    给XML配置文件"减肥"的还有一个选择就是使用p名称空间。当我们採用了p名称空间。我们就能够在bean元素中使用属性(attribute)来描写叙述bean的property值。


    6)、 xmlns:task="http://www.springframework.org/schema/task"

    7)、xmlns:aop="http://www.springframework.org/schema/mvc"

    声明前缀为mvc的命名空间,后面的URL用于标示命名空间的地址不会被解析器用于查找信息。其惟一的作用是赋予命名空间一个惟一的名称。

    当命名空间被定义在元素的開始标签中时,全部带有同样前缀的子元素都会与同一个命名空间相关联。

    8)、xsi:schemaLocation="

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

    这个从命名能够看出个大概。指定Schema的位置这个属性必须结合命名空间使用。这个属性有两个值,第一个值表示须要使用的命名空间。第二个值表示供命名空间使用的 XML schema的位置

     

    所以我们须要什么样的标签的时候,就引入什么样的命名空间和Schema定义就能够了。

     

    二、Spring配置文件结构

    beans标签中能够包括4个标签:

      <alias>为一个定义过的bean起一个别名

      <bean>Spring容器中定义bean元素

      <description>用来描写叙述Spring context或每一个bean元素。尽管他会被Spring容器所忽略,但<description>元素能够通过工具生成属于你的Spring context文档

      <import>导入其它Spring context的定义

     

    1、bean命名空间:

    1)、标签bean中能够包括例如以下元素:

      <constructor-arg>bean的构造函数注入值或引用,即构造函数注入

      <description>beans中作用同样。用来描写叙述Spring context或每一个bean元素,尽管他会被Spring容器所忽略,但<description>元素能够通过工具生成属于你的Spring context文档

      <lookup-method>用法来取代getter注入,指定一个方法。他会在执行被复写从而返回一个指定的bean,即getter注入

      <meta>同意为你的bean进行meta配置,仅在一些特殊场合下实用

      <property>bean的特定属性注入一个值或者引用。这就是我们常说的setter注入

      <replaced-method>用一个新的实现来取代bean的某个方法

    2)、标签bean中能够包括例如以下属性:

     

     2、Context命名空间

    1)、Context标签:

    <context:component-scan/>具体解释

    .假设不想在xml文件里配置bean。能够给我们的类加上spring组件注解,仅仅需再配置下spring的扫描器<context:component-scan/>就能够实现bean的自己主动载入。

    然后在程序中增加注解便可自己主动载入bean,@Component是全部受Spring管理组件的通用形式;而@Repository@Service@Controller则是@Component的细化,用来表示更详细的用例(比如,分别相应了持久化层、服务层和表现层)。

    也就是说,你能用@Component来注解你的组件类,但假设用@Repository@Service@Controller来注解它们,你的类或许能更好地被工具处理,或与切面进行关联。

    <context:component-scan>提供两个子标签:<context:include-filter>和<context:exclude-filter>各代表引入和排除的过滤。

    有了<context:component-scan>,还有一个<context:annotation-config/>标签根本能够移除掉。由于已经被包括进去了。

    3、AOP命名空间

    1)、AOP标签

    4、JEE命名空间

    1)、JEE标签

  • 相关阅读:
    加密模块
    Flask_Blueprint(蓝图)
    Python中__get__ ,__getattr__ ,__getattribute__用法与区别?
    为什么要使用数据库连接池?以及用法(DBUtils)
    Flask_配置文件
    CRM知识点汇总(未完💩💩💩💩💩)
    popUp
    Django_调查问卷
    Django_form
    Numpy
  • 原文地址:https://www.cnblogs.com/yutingliuyl/p/7253867.html
Copyright © 2011-2022 走看看