Java Spring框架有多少种配置方式?它们在技术上有什么区别?(没有优点或缺点..)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35807056/
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-08-11 17:08:46  来源:igfitidea点击:

How many ways are there to configure the Spring framework? What are the differences between them technically? (Not pros or cons..)

javaxmlspring

提问by Koray Tugay

I am studying thisbook (which I would highly recommend), and I am confused about how the authors explain the way Spring framework can be configured.

我正在学习本书(我强烈推荐),我对作者如何解释 Spring 框架的配置方式感到困惑。

You can see some code examples that are used in the book here. (They are available to anyone..) The code I am referring the will be the code from chapter 2, if you would like to take a look.

您可以在此处查看本书中使用的一些代码示例。(任何人都可以使用它们......)我所指的代码将是第 2 章中的代码,如果你想看一看的话。

Anyway, the book states that there are 3 ways to configure the Spring Container.

无论如何,这本书指出有3 种方法可以配置 Spring Container



XML - based configuration

基于 XML 的配置

This will require an xml file that resembles something like this:

这将需要一个类似于以下内容的 xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" ...>

    <bean id="accountService" class="com.wiley.beginningspring.ch2.AccountServiceImpl">
        <property name="accountDao" ref="accountDao"/>
    </bean>

    <bean id="accountDao" class="com.wiley.beginningspring.ch2.AccountDaoInMemoryImpl">
    </bean>

</beans>

And then in order to bootstrap Spring, the code that will be used will be:

然后为了引导 Spring,将使用的代码将是:

ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("/com/wiley/beginningspring/ch2/ch2-beans.xml");

I do not have any confusions at this moment.

此刻我没有任何困惑。



Java Based Configuration

基于 Java 的配置

In this Configuration method, there will be a class for the configuration as follows:

在此 Configuration 方法中,将有一个用于配置的类,如下所示:

@Configuration
public class Ch2BeanConfiguration {

    @Bean
    public AccountService accountService() {
        AccountServiceImpl bean = new AccountServiceImpl();
        bean.setAccountDao(accountDao());
        return bean;
    }

    @Bean
    public AccountDao accountDao() {
        AccountDaoInMemoryImpl bean = new AccountDaoInMemoryImpl();
        return bean;
    }
}

and the code that is responsible for bootstrapping Spring looks like:

负责引导 Spring 的代码如下所示:

ApplicationContext applicationContext
            = new AnnotationConfigApplicationContext(Ch2BeanConfiguration.class);

So up to here, all is clear for me. (Kind of..) I would also like to note that, here we actually have an Annotation which is called @Configuration...

到此为止,对我来说一切都很清楚。(有点......)我还想指出,这里我们实际上有一个名为@Configuration 的注释......



Annotation Based Configuration

基于注释的配置

The last configuration method available, explained in the book is the Annotation Based Configuration.

本书中解释的最后一种可用的配置方法是Annotation Based Configuration

There is an xml file just like we had in the XML-Based Configuration, however a much smaller one:

有一个 xml 文件,就像我们在基于 XML 的配置中一样,但要小得多:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" ...>
    <context:component-scan base-package="com.wiley.beginningspring.ch2"/>
</beans>

All the beans have Annotations such as:

所有的bean都有注释,例如:

@Component, @Service

etc..

等等..

And all the dependencies have the annotations:

并且所有依赖项都有注释:

@Autowired

so that beans can be injected.

这样可以注入bean。

The way Spring framework bootstrapped in this configuration method is as follows:

Spring框架在这个配置方法中的自举方式如下:

ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("/ch2-beans.xml");


Here are my questions:

以下是我的问题:

Why is the (so-called) Annotation Based Configurationactually using ClassPathXmlApplicationContextbut not AnnotationConfigApplicationContextabove? The latter seems much more appropriate to be used in a Configuration that has the words "Annotation Based" in it, isn 't it?

为什么(所谓的)基于注释的配置实际上使用的是ClassPathXmlApplicationContext而不是上面的AnnotationConfigApplicationContext?后者似乎更适合用于包含“基于注释”字样的配置中,不是吗?

The Java Based Configurationexplained in the book seems like what should be called Annotation Based Configuration.?

Java配置在书中解释好像应该叫什么基于注解配置的

And the way Annotation Based Configuration explained in the book actually seems to me something like: XML-Based Configuration with Autowired beans. It does not even have the @Configuration annotation, which the "Java Based Configuration" has..

书中解释的基于注解的配置的方式在我看来实际上是这样的:使用自动装配 bean 的基于 XML 的配置。它甚至没有“基于 Java 的配置”所具有的 @Configuration 注释。

How many ways are there to configure Spring framework?

Spring框架有几种配置方式?

采纳答案by Ken Bekov

To avoid confusion, we should understand, that configuration definitionand bean definitionare two different things. There are three ways to define configuration, available in Spring 4 by default:

为了避免混淆,我们应该明白,配置定义bean 定义是两个不同的东西。共有三种定义配置的方式,默认在 Spring 4 中可用:

  • xml-basedconfiguration, when you describe configuration in xml file;
  • java-basedconfiguration, when configuration is Java class, marked with specific annotations;
  • groovy-basedconfiguration, when configuration is file with Groovy code;
  • 基于xml的配置,当你在xml文件中描述配置时;
  • 基于java的配置,当配置为Java类时,标注具体的注解;
  • 基于groovy的配置,当配置是带有Groovy代码的文件时;

And there are two ways to add bean definition into application:

有两种方法可以将 bean 定义添加到应用程序中:

  • configuration insidebean definition, when you add beans manually by declaration right in configuration.

    In this case definition will be based on configuration type. For xml-config it will be <bean/>tag, for java-based config - method with @Beanannotation and beans {...}construction for Groovy.

  • annotation basedbean definition, when you mark bean classes with specific annotations (like @Component, @Service, @Controlleretc). This type of config uses classpath scanning.

    In this case you have to specify directive for scanning classpath. For xml-config it will be <context:component-scan base-package="..."/>, for java-config - @ComponentScanannotation, for Groovy ctx.'component-scan'(...)invocation.

  • bean 定义中配置,当您通过在配置中的声明手动添加 bean 时。

    在这种情况下,定义将基于配置类型。对于 xml-config,它将是<bean/>标记,对于基于 Java 的配置 - 带有@Bean注释和beans {...}构造的 Groovy 方法。

  • 基于注解的bean定义,当你标记bean类与特定的注解(如@Component@Service@Controller等)。这种类型的配置使用类路径扫描

    在这种情况下,您必须指定用于扫描类路径的指令。对于 xml-config <context:component-scan base-package="..."/>,对于 java-config -@ComponentScan注释,对于 Groovyctx.'component-scan'(...)调用。

As you see, you can use configurations and bean definitions in different combinations.

如您所见,您可以以不同的组合使用配置和 bean 定义。

Note, that if you use xml based config, you can choose approach to drive dependency injection: manually in xml, or by using annotations (@Autowire, @Requiredetc). In late case you have to define <context:annotation-config/>. But do not confuse bean definition and dependency injection control.

请注意,如果您使用基于XML配置,你可以选择的方式来驱动的依赖注入:手动XML,或通过使用注释(@Autowire@Required等)。在后期情况下,您必须定义<context:annotation-config/>. 但不要混淆 bean 定义和依赖注入控制。

Now based on this point of view lets try to answer your questions:

现在基于这个观点让我们试着回答你的问题:

Why is the (so-called) Annotation Based Configuration actually using ClassPathXmlApplicationContext but not AnnotationConfigApplicationContext above?

为什么(所谓的)基于注释的配置实际上使用的是 ClassPathXmlApplicationContext 而不是上面的 AnnotationConfigApplicationContext?

Book's author mixed up concepts. Actually, this is a xml-based configuration with annotation-based bean definition.

本书的作者混淆了概念。实际上,这是一个基于 xml 的配置,带有基于注释的 bean 定义。

The Java Based Configuration explained in the book seems like what should be called Annotation Based Configuration.?

书中解释的基于Java的配置看起来应该叫基于注解的配置。?

You're right - Java based configuration really actively uses annotations, and could be called Annotation based. But annotation is a part of Java. In addition this is a traditional term, specified in documentation.

您说得对 - 基于 Java 的配置确实积极使用注释,并且可以称为基于注释。但注解是 Java 的一部分。此外,这是一个传统术语,在文档中指定

How many ways are there to configure Spring framework?

Spring框架有几种配置方式?

So, by default, we have three ways to describe configuration, and two ways to define beans. That turns six ways to configure Spring framework(by default). But, of course, all of this ways can be used in combination with each other.

因此,默认情况下,我们有三种描述配置的方式,以及两种定义 bean 的方式。这变成了配置 Spring 框架的六种方式(默认情况下)。但是,当然,所有这些方式都可以相互结合使用。

回答by Name Name

I have not taken a look at the book, but your assumptions are actually correct.

我没有看过这本书,但你的假设实际上是正确的。

To start a Spring application and have all the beans instantiated by the framework using XML configured beans, you must use a ClassPathXmlApplicationContext.

要启动 Spring 应用程序并让框架使用 XML 配置的 bean 实例化所有 bean,您必须使用 ClassPathXmlApplicationContext。

There are 2 ways to configure beans in Spring:

Spring有两种配置bean的方法:

1) XML beans 2) Annotation approach

1) XML bean 2) 注释方法

Basically, XML beans were used in Spring 2.5-3 and Annotations approach is used more in Spring 4.

基本上,在 Spring 2.5-3 中使用了 XML bean,而在 Spring 4 中更多地使用了注释方法。

@Bean // is a way to create a bean. It is the equalivant of the beans tag in XML.

@Configuration // is a way to tell the Spring container that this class is a list of configuration beans

Spring has 2 spring containers:

Spring 有 2 个 spring 容器:

1) BeanFactory 2) ApplicationContext

1) BeanFactory 2) 应用上下文

A BeanFactory is the most simplest container and only accepts one config file. An ApplicationContext is the most advanced Spring container and is used in enterprise applications as it accepts an array of config files, has JNDI integration, EJB integration and supports internationalisation of messages.

BeanFactory 是最简单的容器,只接受一个配置文件。ApplicationContext 是最先进的 Spring 容器,用于企业应用程序,因为它接受一系列配置文件,具有 JNDI 集成、EJB 集成并支持消息的国际化。

I hope that helps.

我希望这有帮助。

回答by Bassem Reda Zohdy

Let me first show different in coding first:

让我先展示一下不同的编码:

  1. XML: you have to register your spring bean inside xml context file using <bean/>
  2. Java Configuration: you have to use @Configurationand @Beanto register your spring bean in context.

  3. Annotation: by using @Componentand its friends is actually common could be used with other 2 using:

    1. in xml using <context:component-scan/>
    2. in java config using @ComponentScan
  1. XML:您必须使用以下方法在 xml 上下文文件中注册您的 spring bean <bean/>
  2. Java 配置:您必须在上下文中使用@Configuration@Bean注册您的 spring bean。

  3. 注释: by using@Component和它的朋友实际上是 common 可以与其他 2 using 一起使用:

    1. 在 xml 中使用 <context:component-scan/>
    2. 在java配置中使用 @ComponentScan

Why used ClassPathXmlApplicationContextbecause he used xml to configure component scan but if he used @ComponentScanthen will sure use AnnotationConfigApplicationContext

为什么用,ClassPathXmlApplicationContext因为他用xml来配置组件扫描但是如果他用了@ComponentScan那么肯定会用AnnotationConfigApplicationContext

So for me I consider it as 2 ways to initialize spring context xml or java configuration and Annotation is option could be used by any one of them or not used at all.

所以对我来说,我认为它是初始化 spring 上下文 xml 或 java 配置的 2 种方法,并且 Annotation 选项可以被它们中的任何一个使用或根本不使用。

回答by Serge Ballesta

Not exactly. There are either only two ways to configure the Spring framework. The two basic configuration tools for the Spring framework are:

不完全是。配置 Spring 框架只有两种方式。Spring框架的两个基本配置工具是:

  • XML files (outside java files)
  • Java (5 +) based annotations (inside java files)
  • XML 文件(在 java 文件之外)
  • 基于 Java (5 +) 的注释(在 java 文件中)

Both of them can be used to:

它们都可以用于:

  • configure an application context (meaning adding beans) - the constructors of concrete application contexts accept xml files, packages to scan, or directly named classes to load
  • bootstrap an application context in the case of a web application - using web.xmlvs using a class implementing WebApplicationInitializer
  • 配置应用程序上下文(意思是添加 bean)——具体应用程序上下文的构造器接受 xml 文件、要扫描的包或直接命名的类来加载
  • 在 Web 应用程序的情况下引导应用程序上下文 - 使用web.xml与使用类实现WebApplicationInitializer

And last but not least:

最后但并非最不重要:

  • you can scan annotations from an xml configuration file by using <context:component-scan/>
  • you can load a xml file from an annotated configuration bean by using @import
  • 您可以使用以下命令从 xml 配置文件中扫描注释 <context:component-scan/>
  • 您可以通过使用从带注释的配置 bean 加载 xml 文件 @import

The second way called Java Based Configurationin your book, is a particular annotation @Configuration. A class annotated with it will normally be a bean in the application context, but it can also declare other beans with the @Beanannotation on one method. That's the reason why those classes are generally loaded directly instead of bean scanned.

在您的书中称为基于 Java 的配置的第二种方式是特定的注释@Configuration。用它注释的类通常是应用程序上下文中的一个 bean,但它也可以用@Bean一个方法上的注释声明其他 bean 。这就是为什么这些类通常直接加载而不是 bean 扫描的原因。

The third way called Annotation Based Configuration, is simply a mixing of the two modes, where you use xml configuration at higher level and just scan packages for additional beans.

第三种方式称为Annotation Based Configuration,只是两种模式的混合,您可以在更高级别使用 xml 配置,只需扫描包中的其他 bean。



TL/DR: there are only 2 ways to configure an application context in the spring framework:

TL/DR:spring框架中配置应用上下文只有两种方式:

  • xml configuration files
  • java annotations
  • xml配置文件
  • Java注解

and they can be mixed

它们可以混合使用

but every single bean can be declared in 3 ways:

但是每个 bean 都可以通过 3 种方式声明:

  • from a xml file
  • with a @Beanannotation inside a @Configurationannotated bean
  • directly with a @Component(or any of the specialized annotations @Controller, @Service, etc.)
  • 从一个 xml 文件
  • @Bean一个内部的注解@Configuration注释豆
  • 直接与@Component(或任何专门的注释@Controller@Service等等)


Now for your precise questions:

现在回答您的确切问题:

Why is the (so-called) Annotation Based Configuration actually using ClassPathXmlApplicationContext but not AnnotationConfigApplicationContext above?

为什么(所谓的)基于注释的配置实际上使用的是 ClassPathXmlApplicationContext 而不是上面的 AnnotationConfigApplicationContext?

Because the ApplicationContext is first initialized form a xml configuration file. Package scanning occurs later with the help of <context:scan ...>tags. You use an AnnotationConfigApplicationContextif you directly initialize it from configuration classes or through package scanning.

因为 ApplicationContext 首先从一个 xml 配置文件初始化。包扫描稍后在<context:scan ...>标签的帮助下进行。您可以使用的AnnotationConfigApplicationContext,如果你直接从配置类或通过包扫描初始化。

The Java Based Configuration explained in the book seems like what should be called Annotation Based Configuration.?

书中解释的基于Java的配置看起来应该叫基于注解的配置。?

They call it Java Basedbecause it needs no xml so configuration only uses Java

他们称之为Java Based因为它不需要 xml 所以配置只使用 Java

回答by Franti?ek Hartman

The easiest way to understand this is to look into the long history of the framework how this was developed.

理解这一点的最简单方法是查看框架如何开发的悠久历史。

  • XML based configuration - this was there from the the beginning - version 1 - see javadoc for ClassPathXmlApplicationContext. This was around march 2004, the time of J2EE 1.4, which had HUGE xml configuration and Spring was big simplification (XML as well, but simpler). This uses XML for everything, including specifying autowiring, or what dependencies go where directly (your ref="accoundDao" example).

  • Annotation based configuration - in Spring 2.5 - this came as a reaction to Java EE 5, new anotations like @Autowiredwere introduced, there was still some context configuration in XML files - usually you would define which packages were to be scanned and rest of it was done automatically based on annotations - hence the name.

  • Java based configuration came with Spring 3, was improved in later versions. This is when AnnotationConfigApplicationContextand Configuration annotation were introduced - you could potentially drop XML entirely -> java based config. Although this became practical only later with version 4+, because of large number of xml helper tags for aop, jdbc etc.

  • 基于 XML 的配置 - 从一开始就存在 - 版本 1 - 请参阅ClassPathXmlApplicationContext 的 javadoc。那是在 2004 年 3 月左右,J2EE 1.4 的时代,它有大量的 xml 配置,Spring 进行了很大的简化(XML 也是如此,但更简单)。这对所有内容都使用 XML,包括指定自动装配,或直接依赖哪些地方(您的 ref="accoundDao" 示例)。

  • 基于注解的配置——在 Spring 2.5 中——这是对 Java EE 5 的一种反应,引入了像@Autowired这样的新注解,XML 文件中仍然有一些上下文配置——通常你会定义要扫描的包以及其余的包是根据注释自动完成的 - 因此得名。

  • 基于 Java 的配置随 Spring 3 一起提供,在以后的版本中得到了改进。这是引入AnnotationConfigApplicationContext和 Configuration 注释的时候 - 您可能会完全删除 XML -> 基于 java 的配置。尽管这只是在 4+ 版本之后才变得实用,因为 aop、jdbc 等的大量 xml 辅助标记。

Beside these 3 (2 actually as 1 and 2 use the same ApplicationContext class), are other ways to create a context:

除了这 3 个(实际上 2 个,因为 1 和 2 使用相同的 ApplicationContext 类),还有其他创建上下文的方法:

回答by SkyWalker

At first, I want to give thanks Ken Bekovfor his more resourceful answer. I have tried to improvise his answer so that anyone can learn more on this area.

首先,我要感谢Ken Bekov更足智多谋的回答。我试图即兴创作他的答案,以便任何人都可以在这方面了解更多。

Configuration Definition:

配置定义:

Spring 4 contains 3 ways to define its configuration. They are

Spring 4 包含 3 种定义其配置的方法。他们是

enter image description here

在此处输入图片说明

Advantages of the annotation:

注解的优点:

  1. All the information is in a single file(no need to open two files to configure a given behavior)

  2. When the class changes, no need to modify the xml file

  3. Annoations often said to be more intuitive and robust when re-factoring application code. Also they benefit from a better IDE guidance like guise provides. But they mix application code with DI concerns. An application gets dependent on a framework. Clear separation is almost impossible. Annotations are also limited when describing different injection behaviour at the same place (constructor, field) dependent on other circumstances (e.g. robot legs problem).Moreover they don't allow to treat external classes (library code) like your own source. Therefore they are considered to run faster than XML.

  1. 所有information is in a single file(不需要打开两个文件来配置给定的行为)

  2. 当班级发生变化时, no need to modify the xml file

  3. 在重构应用程序代码时,注释通常被认为更加直观和健壮。他们还受益于更好的 IDE 指导,如 guise 提供的。但是它们将应用程序代码与 DI 问题混合在一起。应用程序依赖于框架。清晰的分离几乎是不可能的。Annotations are also limited when describing different injection behaviour at the same place (constructor, field) dependent on other circumstances (e.g. robot legs problem).此外,它们不允许将外部类(库代码)视为您自己的源代码。因此,它们被认为比 XML 运行得更快。

Advantages of xml file:

xml文件的优点:

  1. Clear separation between the POJO and its behavior

  2. When you do not know which POJO is responsible for the behavior, it is easier to find that POJO (searching in a subset of files rather than all the source code)

  3. XML has the only benifit of a declarative style that is defined clearly separated from the application code itself. That stays independent from DI concerns. The downsides are verbosity, poor re-factoringrobustnessand a general runtime failurebehaviour. There is just a general (XML) tool support with little benefit compared to IDE support for e.g. Java. Besides this XML comes with a performance overhead so it is usually slower than code solutions.

  1. POJO 及其行为之间的明确分离

  2. 当您不知道哪个 POJO 负责该行为时,更容易找到该 POJO(搜索文件的子集而不是所有源代码)

  3. XML 具有声明式风格的唯一优点,它与应用程序代码本身明确定义分开。这与 DI 问题保持独立。缺点是verbositypoor re-factoringrobustnessa general runtime failure行为。与 IDE 对 Java 等的支持相比,只有一个通用 (XML) 工具支持并没有什么好处。此外,此 XML 会带来性能开销,因此通常为slower than code solutions.

XML and Annotation Based Link:

XML 和基于注释的链接:

  1. http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#beans-annotation-config
  2. Annotations vs XML, advantages and disadvantages
  3. Java Dependency injection: XML or annotations
  4. Spring annotation-based DI vs xml configuration?
  5. Xml configuration versus Annotation based configuration
  1. http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#beans-annotation-config
  2. 注解 vs XML,优缺点
  3. Java 依赖注入:XML 或注解
  4. 基于 Spring 注释的 DI 与 xml 配置?
  5. Xml 配置与基于注解的配置

Groovy Based Link:

基于 Groovy 的链接:

  1. https://objectpartners.com/2016/01/12/using-groovy-based-spring-configuration/
  2. http://blog.andresteingress.com/2014/02/14/grails-java-based-spring-config/
  1. https://objectpartners.com/2016/01/12/using-groovy-based-spring-configuration/
  2. http://blog.andresteingress.com/2014/02/14/grails-java-based-spring-config/

Bean Definition:

豆定义:

There are 2 ways to bean Definition: enter image description here

bean定义有两种方式: 在此处输入图片说明

Scanning classpath:

扫描类路径:

For xml-configit will be <context:component-scan base-package="..."/>, for java-config- @ComponentScanannotation, for Groovy ctx.'component-scan'(...)invocation.

对于XML的配置将是<context:component-scan base-package="..."/>对Java的配置-@ComponentScan注释,对Groovyctx.'component-scan'(...)调用。

Dependency Injection:

依赖注入:

In xml based config, dependency injectioncan be done manually in xml, or by using annotations(@Autowire, @Required etc). On that case It is need to define <context:annotation-config/>

在基于 xml 的配置中,dependency injection可以在 中手动完成,也可以xml使用annotations(@Autowire、@Required 等)来完成。在这种情况下,需要定义<context:annotation-config/>

Question & Answer:

问答:

Q1: Why is the (so-called) Annotation Based Configuration actually using ClassPathXmlApplicationContext but not AnnotationConfigApplicationContext above?

Q1:为什么(所谓的)基于注解的配置实际上使用的是 ClassPathXmlApplicationContext 而不是上面的 AnnotationConfigApplicationContext?

Ans:It is a xml-based configuration with annotation-based bean definition.

Ans:它是一个基于 xml 的配置,带有基于注解的 bean 定义。

Application Context:

应用上下文:

  1. http://docs.spring.io/spring/docs/4.2.0.RELEASE/javadoc-api/org/springframework/context/ApplicationContext.html
  1. http://docs.spring.io/spring/docs/4.2.0.RELEASE/javadoc-api/org/springframework/context/ApplicationContext.html

AnnotationConfigApplicationContext:

AnnotationConfigApplicationContext:

1.AnnotationConfigApplicationContext and parent context

1. AnnotationConfigApplicationContext 和父上下文

ClassPathXmlApplicationContext:

ClassPathXmlApplicationContext:

  1. http://www.tutorialspoint.com/spring/spring_applicationcontext_container.htm
  2. http://www.mkyong.com/spring3/spring-3-hello-world-example/
  1. http://www.tutorialspoint.com/spring/spring_applicationcontext_container.htm
  2. http://www.mkyong.com/spring3/spring-3-hello-world-example/

Q2: The Java Based Configuration explained in the book seems like what should be called Annotation Based Configuration.?

Q2:书中解释的Java Based Configuration好像应该叫Annotation Based Configuration。?

Ans:You're right on that case. Java based configuration uses annotations, and called annotation based configuration. But annotation is a single part of Java, nothing else.

Ans:你是对的。基于 Java 的配置使用注解,称为基于注解的配置。但是注释是 Java 的一个部分,没有别的。

But elaborately we need to understand how this hierarchy comes from xml to annotation based and at last groovy based?

但是我们需要仔细了解这个层次结构是如何从 xml 到基于注释的,最后是基于 groovy 的?

An alternative to XML setups is provided by annotation-based configuration which rely on the bytecode metadata for wiring up components instead of angle-bracket declarations. Instead of using XML to describe a bean wiring, the developer moves the configuration into the component class itself by using annotations on the relevant class, method, or field declaration. As mentioned in the section called “Example: The RequiredAnnotationBeanPostProcessor”, using a BeanPostProcessor in conjunction with annotations is a common means of extending the Spring IoC container. For example, Spring 2.0introduced the possibility of enforcing required properties with the @Requiredannotation.

XML 设置的替代方案由基于注释的配置提供,该配置依赖于字节码元数据来连接组件而不是尖括号声明。开发人员不使用 XML 来描述 bean 连接,而是通过在相关类、方法或字段声明上使用注释将配置移动到组件类本身中。如“示例:RequiredAnnotationBeanPostProcessor”部分所述,将 BeanPostProcessor 与注解结合使用是扩展 Spring IoC 容器的常用方法。例如, Spring 2.0引入了使用@Required注释强制执行所需属性的可能性。

Spring 2.5made it possible to follow that same general approach to drive Spring's dependency injection. Essentially, the@Autowiredannotation provides the same capabilities as described in Section 6.4.5, “Autowiring collaborators”but with more fine-grained control and wider applicability.

Spring 2.5使得遵循相同的通用方法来驱动 Spring 的依赖注入成为可能。本质上,@Autowired注释提供了与第 6.4.5 节“自动装配协作者”中描述的相同的功能但具有更细粒度的控制和更广泛的适用性。

Spring 2.5also added support for JSR-250 annotations such as @PostConstruct, and @PreDestroy.

Spring 2.5还添加了对 JSR-250 注释的支持,例如@PostConstruct, 和@PreDestroy

Spring 3.0added support for JSR-330 (Dependency Injection for Java) annotations contained in the javax.inject package such as @Injectand @Named. Details about those annotations can be found in the relevant section.

Spring 3.0添加了对 javax.inject 包中包含的 JSR-330(Java 依赖注入)注解的支持,例如@Inject@Named。有关这些注释的详细信息可以在相关部分中找到

Q3: How many ways are there to configure Spring framework?

Q3:Spring框架有几种配置方式?

Ans:

答:

Theoretically,3 ways to describe configuration, and 2 ways to define beans. It turns 3*2 = 6 ways to configure Spring framework (by default). All of this ways can be used in combination with each other.

Theoretically,3 种描述配置的方式,以及 2 种定义 bean 的方式。它变成了 3*2 = 6 种配置 Spring 框架的方式(默认)。所有这些方式都可以相互结合使用。

But Actually,in a single word, we can configure spring framework by using XMLor annotations.

But Actually,总之,我们可以通过使用XML或来配置spring框架annotations

回答by Costi Ciudatu

The three ways to configure the spring framework are not at all meant to be mutually exclusive. Actually, my guess is that on average you will find at least two of them used together.

配置 spring 框架的三种方式并不意味着相互排斥。实际上,我的猜测是平均而言,您会发现其中至少两个一起使用。

The annotation based configuration is the least likely to be used stand-alone, simply because it relies on metadata that is inherently scattered throughout the source code.

基于注释的配置最不可能单独使用,因为它依赖于固有地分散在整个源代码中的元数据。

The AnnotationConfigApplicationContextcan be used to fire up a pureannotation-based context, but you will need to pass in all your classesannotated as @Componentor derivatives, rather than passing in a single (or a couple of) @Configuration-annotated class(es) -- which is usually not practical.

AnnotationConfigApplicationContext可以用来启动一个纯粹的基于注释的背景下,但你将需要通过所有的类注释为@Component或衍生工具,而不是通过在单(或几个)@Configuration-annotated类(ES) -这通常不实用。

Although this is pretty much the same as statically listing the beans in XML or Java configuration, the fact that you'll need to do this in code when you build the application context itself makes it less useful, as you can't benefit of the various clever ways of automatically firing up an application context (in web contexts and so on).

尽管这与在 XML 或 Java 配置中静态列出 bean 几乎相同,但在构建应用程序上下文本身时需要在代码中执行此操作的事实使其不太有用,因为您无法从自动启动应用程序上下文(在 Web 上下文等中)的各种巧妙方法。

That's why you will probably want to be able to have the full object graph metadata assembled in one go, and that can only be achieved with either XML or Java-based config, which rely on "centralized" metadata describing the whole object graph.

这就是为什么您可能希望能够一次性组装完整的对象图元数据,而这只能通过 XML 或基于 Java 的配置来实现,它们依赖于描述整个对象图的“集中”元数据。

For both XML and Java-based approaches, this "centralized metadata" (<beans>or @Configuration) can be defined statically (with explicit <bean>or @Beandefinitions) or dynamically (using <component-scan>or @ComponentScan). So it's reasonable to say that these two approaches are just different formats with pretty much the same capabilities which can both benefit of annotation-based config for gathering the "de-centralized" metadata.

对于 XML 和基于 Java 的方法,这种“集中元数据”(<beans>@Configuration)可以静态(使用显式<bean>@Bean定义)或动态(使用<component-scan>@ComponentScan)定义。因此,可以合理地说这两种方法只是具有几乎相同功能的不同格式,它们都可以从基于注释的配置中受益,以收集“去中心化”元数据。