将spring XML文件转换为spring @Configuration类 [英] Converting spring XML file to spring @Configuration class

查看:151
本文介绍了将spring XML文件转换为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?

推荐答案

(免责声明 - 这个答案基于我的博客文章

(Disclaimer - this answer is based on my blog post)

可以将xml迁移到 @Configuration 分几步:

It is possible to migrate the xml to a @Configuration in a few steps:


  1. 创建一个 @Configuration 带注释的类:

@Configuration
public class MyApplicationContext {

}


  • 对于每个< bean> ; 标签创建一个用 @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 类之后,我们可以通过向声明私有成员来使用它们在我们的上下文中声明的bean @Configuration 类如下:

  • 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;
    

    或者直接在方法中使用它作为参数,该方法定义依赖于此<$ c $的bean c> beanFromSomewhereElse 使用 @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或<导入bean非常相似code> @Configuration class。而不是使用 @Qualifier ,我们将使用 @Value ,其属性如下:

  • 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/>
    

    您现在可以准确导入 @Configuration 类与创建简单bean相同:

    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要舒服得多,因为我看到了一些优点: / p>


    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 defined 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非常好,<$应用程序上下文中的c $ c> @Configuration 和注释。 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 XML文件转换为spring @Configuration类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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