有多少种方法可以配置Spring框架?技术上它们之间有什么区别? (不是利弊......) [英] How many ways are there to configure the Spring framework? What are the differences between them technically? (Not pros or cons..)

查看:96
本文介绍了有多少种方法可以配置Spring框架?技术上它们之间有什么区别? (不是利弊......)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习这本书(我强烈推荐),我很担心作者如何解释Spring框架的配置方式。

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

你可以看到书中使用的一些代码示例here 。 (任何人都可以使用它们。)如果您想查看,我所引用的代码将是第2章中的代码。

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.

无论如何,本书指出 3种配置Spring容器的方法

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

基于XML的配置

这将需要一个类似于这样的xml文件:

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

<?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>

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

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

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

此刻我没有任何混淆。

I do not have any confusions at this moment.

基于Java的配置

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

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

@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;
    }
}

并且负责引导Spring的代码看起来像:

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

ApplicationContext applicationContext
            = new AnnotationConfigApplicationContext(Ch2BeanConfiguration.class);

所以到这里,一切都很清楚。 (有点......)我还要注意,这里我们实际上有一个名为@Configuration的注释......

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...

基于注释的配置

本书中介绍的最后一种配置方法是基于注释的配置

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

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

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

<?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>

所有bean都有注释,例如:

All the beans have Annotations such as:

@Component, @Service

等..

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

And all the dependencies have the annotations:

@Autowired

以便可以注入豆类。

方式在此配置方法中引导的Spring框架如下所示:

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

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






以下是我的问题:

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

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

本书中解释的基于Java的配置似乎应该称为基于注释的配置。?

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

本书中解释的基于注释的配置实际上似乎是这样的:基于XML的配置与自动装配的bean。它甚至没有@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..

有多少种方法可以配置Spring框架?

How many ways are there to configure Spring framework?

推荐答案

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

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


  • 基于xml的配置,您在xml文件中描述配置;

  • 基于java的配置,当配置为Java类时,标有特定注释;

  • 基于groovy的配置,当配置是带有Groovy代码的文件时;

  • xml-based configuration, when you describe configuration in xml file;
  • java-based configuration, when configuration is Java class, marked with specific annotations;
  • groovy-based configuration, when configuration is file with Groovy code;

并且有两种方式将bean定义添加到应用程序中:

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


  • 配置 bean定义,当你手动添加bean时申报权在配置中。

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

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

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

基于注释 bean定义,当您使用特定注释标记bean类时(例如 @Component @Service @Controller 等)。此类配置使用类路径扫描

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

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

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 - @ComponentScan annotation, for Groovy ctx.'component-scan'(...) invocation.

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

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

注意,如果你使用基于xml的配置,你可以选择驱动依赖注入的方法:手动在xml中,或使用注释( @Autowire @Required 等)。在最后一种情况下,您必须定义< context:annotation-config /> 。但是不要混淆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, @Required etc). In late case you have to define <context:annotation-config/>. But do not confuse bean definition and dependency injection control.

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

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


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

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

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

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


本书中解释的基于Java的配置看起来像
应该被称为基于注释的配置。?

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

你是对的 - 基于Java的配置确实主动使用注释,可能是称为基于注释。但注释是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.


有多少种方法可以配置Spring框架?

How many ways are there to configure Spring framework?

因此,默认情况下,我们有三种方式来描述配置,以及两种定义bean的方法。这有六种方式来配置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.

这篇关于有多少种方法可以配置Spring框架?技术上它们之间有什么区别? (不是利弊......)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆