理解 spring @Configuration 类 [英] Understanding spring @Configuration class

查看:24
本文介绍了理解 spring @Configuration 类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

按照问题了解 Spring @Autowired 用法 我想为其他弹簧接线选项创建一个完整的知识库,@Configuration 类.

Following the question Understanding Spring @Autowired usage I wanted to create a complete knowledge base for the other option of spring wiring, the @Configuration class.

假设我有一个如下所示的 spring XML 文件:

Let's assume I have a spring XML file that looks like this:

<?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-3.0.xsd">

  <import resource="another-application-context.xml"/>

  <bean id="someBean" class="stack.overflow.spring.configuration.SomeClassImpl">
    <constructor-arg value="${some.interesting.property}" />
  </bean>

  <bean id="anotherBean" class="stack.overflow.spring.configuration.AnotherClassImpl">
    <constructor-arg ref="someBean"/>
    <constructor-arg ref="beanFromSomewhereElse"/>
  </bean>
</beans>

我该如何使用 @Configuration 代替?对代码本身有什么影响吗?

How can I use @Configuration instead? Does it have any affect on the code itself?

推荐答案

Migrating XML to @Configuration

可以通过几个步骤将 xml 迁移到 @Configuration:

  1. 创建一个@Configuration注解类:

@Configuration
public class MyApplicationContext {

}

  • 为每个标签创建一个用@Bean注解的方法:

  • For each <bean> tag create a method annotated with @Bean:

    @Configuration
    public class MyApplicationContext {
    
      @Bean(name = "someBean")
      public SomeClass getSomeClass() {
        return new SomeClassImpl(someInterestingProperty); // We still need to inject someInterestingProperty
      }
    
      @Bean(name = "anotherBean")
      public AnotherClass getAnotherClass() {
        return new AnotherClassImpl(getSomeClass(), beanFromSomewhereElse); // We still need to inject beanFromSomewhereElse
      }
    }
    

  • 为了导入beanFromSomewhereElse,我们需要导入它的定义.它可以在 XML 中定义,我们将使用 @ImportResource:

  • In order to import beanFromSomewhereElse we need to import it's definition. It can be defined in an XML and the we'll use @ImportResource:

    @ImportResource("another-application-context.xml")
    @Configuration
    public class MyApplicationContext {
      ...  
    }
    

    如果 bean 是在另一个 @Configuration 类中定义的,我们可以使用 @Import 注释:

    If the bean is defined in another @Configuration class we can use the @Import annotation:

    @Import(OtherConfiguration.class)
    @Configuration
    public class MyApplicationContext {
      ...
    }
    

  • 在我们导入其他 XML 或 @Configuration 类之后,我们可以通过向 @Configuration 类声明一个私有成员来使用它们在我们的上下文中声明的 bean如下:

  • After we imported other XMLs or @Configuration classes, we can use the beans they declare in our context by declaring a private member to the @Configuration class as follows:

    @Autowired
    @Qualifier(value = "beanFromSomewhereElse")
    private final StrangeBean beanFromSomewhereElse;
    

    或者在定义依赖于这个beanFromSomewhereElse的bean的方法中直接使用它作为参数,使用@Qualifier如下:

    Or use it directly as parameter in the method which defines the bean that depends on this beanFromSomewhereElse using @Qualifier as follows:

    @Bean(name = "anotherBean")
    public AnotherClass getAnotherClass(@Qualifier (value = "beanFromSomewhereElse") final StrangeBean beanFromSomewhereElse) {
      return new AnotherClassImpl(getSomeClass(), beanFromSomewhereElse);
    }
    

  • 导入属性与从另一个 xml 或 @Configuration 类导入 bean 非常相似.我们将使用带有属性的 @Value 代替 @Qualifier,如下所示:

  • Importing properties is very similar to importing bean from another xml or @Configuration class. Instead of using @Qualifier we'll use @Value with properties as follows:

    @Autowired
    @Value("${some.interesting.property}")
    private final String someInterestingProperty;
    

    这也可以与 SpEL 表达式一起使用.

    This can be used with SpEL expressions as well.

    为了让 spring 将此类类视为 bean 容器,我们需要通过将此标记放在上下文中来将其标记在我们的主 xml 中:

    In order to allow spring to treat such classes as beans containers we need to mark this in our main xml by putting this tag in the context:

    <context:annotation-config/>
    

    您现在可以像创建一个简单的 bean 一样导入 @Configuration 类:

    You can now import @Configuration classes exactly the same as you would create a simple bean:

    <bean class="some.package.MyApplicationContext"/>
    

    有一些方法可以完全避免使用 spring XML,但它们不在本答案的范围内.您可以在我的 博客文章,我的回答基于此.

    There are ways to avoid spring XMLs altogether but they are not in the scope of this answer. You can find out one of these options in my blog post on which I'm basing my answer.

    <小时>

    使用这种方法的优缺点

    基本上,我发现这种声明 bean 的方法比使用 XML 更方便,因为我看到了一些优点:


    The advantages and disadvantages of using this method

    Basically I find this method of declaring beans much more comfortable than using XMLs due to a few advantages I see:

    1. 拼写错误 - @Configuration 类被编译,拼写错误将不允许编译
    2. 快速失败(编译时) - 如果您忘记注入 bean,您将在编译时失败,而不是像 XML 那样在运行时失败
    3. 更容易在 IDE 中导航 - 在 bean 的构造函数之间了解依赖树.
    4. 可以轻松调试配置启动
    1. Typos - @Configuration classes are compiled and typos just won't allow compilations
    2. Fail fast (compile time) - If you forget to inject a bean you'll fail on compile time and not on run-time as with XMLs
    3. Easier to navigate in IDE - between constructors of beans to understand the dependency tree.
    4. Possible to easily debug configuration startup

    缺点在我看来并不多,但我能想到的有几个:

    The disadvantages are not many as I see them but there are a few which I could think of:

    1. 滥用 - 代码比 XML 更容易被滥用
    2. 使用 XML,您可以根据在编译时不可用但在运行时提供的类来定义依赖项.对于 @Configuration 类,您必须在编译时使这些类可用.通常这不是问题,但在某些情况下可能会有问题.
    1. Abuse - Code is easier to abuse than XMLs
    2. With XMLs you can define dependencies based on classes that are not available during compile time but are provided during run-time. With @Configuration classes you must have the classes available at compile time. Usually that's not an issue, but there are cases it may be.

    底线:将 XML、@Configurationannotations 结合在您的应用程序上下文.Spring 不关心声明 bean 的方法.

    Bottom line: It is perfectly fine to combine XMLs, @Configuration and annotations in your application context. Spring doesn't care about the method a bean was declared with.

    这篇关于理解 spring @Configuration 类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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