Spring XML 命名空间:我如何找到它们背后的实现类?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11174286/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 05:11:32  来源:igfitidea点击:

Spring XML namespaces : How do I find what are the implementing classes behind them?

springspring-mvcspring-security

提问by electrotype

In my Spring 3.1 application, I sometime need to change the default behavior of some of the Spring namespaces in my context files. To do that, I create custom classes that implement some interfaces or that extend the default classes Spring uses.

在我的 Spring 3.1 应用程序中,我有时需要更改上下文文件中某些 Spring 命名空间的默认行为。为此,我创建了实现某些接口或扩展 Spring 使用的默认类的自定义类。

But I find it hard to know exactly what are those classes that Spring uses behind its namespaces! What is the steps required to find them?

但是我发现很难确切地知道 Spring 在其命名空间后面使用的那些类是什么!找到它们需要哪些步骤?

For example, the security namespace :

例如,安全命名空间:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:sec="http://www.springframework.org/schema/security"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
                           http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
                           http://www.springframework.org/schema/security 
                           http://www.springframework.org/schema/security/spring-security-3.1.xsd">

and something like :

和类似的东西:

<sec:http>
    ...
    <sec:logout />
</sec:http>

How do I find what classes are used by the "<sec:logout />" namespace? I don't find the information by looking at http://www.springframework.org/schema/security/spring-security-3.1.xsd!

如何找到“<sec:logout />”命名空间使用的类?我没有通过查看http://www.springframework.org/schema/security/spring-security-3.1.xsd找到信息!

Where should I look?

我应该在哪里看?

回答by Tomasz Nurkiewicz

Every Spring namespace has an associated NamespaceHandlerimplementation. The namespace schemas are mapped to schema files inside Spring JARs in various spring.schemasfiles (see also Spring DI applicationContext.xml how exactly is xsi:schemaLocation used?).

每个 Spring 命名空间都有一个关联的NamespaceHandler实现。命名空间模式映射到 Spring JAR 中各种spring.schemas文件中的模式文件(另请参阅Spring DI applicationContext.xml xsi:schemaLocation 究竟是如何使用的?)。

The XML schema namespaces are also mapped to handler classes in spring.handlersfiles (several as each Spring JAR might introduce different namespaces). For your convenience here is a list of most common namespaces:

XML 模式命名空间也映射到spring.handlers文件中的处理程序类(有几个,因为每个 Spring JAR 可能引入不同的命名空间)。为方便起见,这里列出了最常见的命名空间:

Spring core

弹簧芯

Spring Security

春季安全

Spring integration

弹簧集成

If you browse to the source of each of these classes you will quickly discover various BeanDefinitionParserimplementations responsible for parsing actual XML definitions.

如果您浏览每个类的源代码,您将很快发现BeanDefinitionParser负责解析实际 XML 定义的各种实现。

回答by sourcedelica

Almost all of them are named *BeanDefinitionParser. First step is, using your browser, bring up the Spring JavaDocs and hit <Ctrl>-F(or <Command>-F). Search for BeanDefinitionParser. Of the classes that match, one or two will, by their names, look like they handle the namespace elements you are researching. It may take a bit of looking at the source code to those classes but you'll ultimately find what you want.

几乎所有的人都被命名了*BeanDefinitionParser。第一步是,使用您的浏览器,打开 Spring JavaDocs 并点击<Ctrl>-F(或<Command>-F)。搜索 BeanDefinitionParser。在匹配的类中,一两个类的名称看起来像是处理您正在研究的命名空间元素。可能需要查看这些类的源代码,但您最终会找到您想要的。

Alternatively, in your IDE, you can browse to BeanDefinitionParser.javaand then do "Find Usages" (IntelliJ) or "Type Hierarchy" (Eclipse) to find all of the implementers of that interface. There are a handful of classes that don't follow the *BeanDefinitionParsernaming convention. Your IDE will give you a comprehensive list.

或者,在您的 IDE 中,您可以浏览BeanDefinitionParser.java并执行“Find Usages”(IntelliJ)或“Type Hierarchy”(Eclipse)以查找该接口的所有实现者。有少数类不遵循*BeanDefinitionParser命名约定。您的 IDE 将为您提供一个完整的列表。

I think that Spring should actually document, in the Namespace section of their reference docs, the names of the classes that handle each element.

我认为 Spring 实际上应该在其参考文档的 Namespace 部分中记录处理每个元素的类的名称。

回答by Ritesh

Every XML namespace is parsed by its NamespaceHandler. The handler class for security namespace is SecurityNamespaceHandler, which is specified in spring.handlers file inside META-INF of spring-security-config-XXX.jar file.

每个 XML 命名空间都由其NamespaceHandler解析。安全命名空间的处理程序类是SecurityNamespaceHandler,它在 spring-security-config-XXX.jar 文件的 META-INF 内的 spring.handlers 文件中指定。

The http element is parsed by HttpSecurityBeanDefinitionParserand its child element logout is parsed by org.springframework.security.config.http.LogoutBeanDefinitionParser(package-protected).

http 元素由HttpSecurityBeanDefinitionParser解析,其子元素 logout 由org.springframework.security.config.http.LogoutBeanDefinitionParser(package-protected)解析。

Also see notes in Coding a NamespaceHandlerand advice in What beans are registered by the spring security namespace?if you are planning to customize it.

另请参阅Coding a NamespaceHandler 中的注释和spring security 命名空间注册哪些 bean?如果您打算自定义它。

回答by jbbarquero

In the case of Spring Security, it's well documented: see Appendix?B.?The Security Namespace

在 Spring Security 的情况下,它有很好的文档记录:请参阅附录?B.?安全命名空间

See also this blog post: BEHIND THE SPRING SECURITY NAMESPACE

另请参阅此博客文章:弹簧安全命名空间的背后