java – 了解spring @Configuration类
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – 了解spring @Configuration类,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3554字,纯文字阅读大概需要6分钟。
内容图文
![java – 了解spring @Configuration类](/upload/InfoBanner/zyjiaocheng/715/d17b5b6a3e82402ba202e5516a8717ab.jpg)
在问题Understanding Spring @Autowired usage之后,我想为弹簧布线的另一个选项@Configuration类创建一个完整的知识库.
假设我有一个如下所示的spring XML文件:
<?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?它对代码本身有什么影响吗?
解决方法:
将XML迁移到@Configuration
可以通过几个步骤将xml迁移到@Configuration:
>创建一个@Configuration注释类:
@Configuration
public class MyApplicationContext {
}
>对于每个< bean> tag创建一个用@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:
@ImportResource("another-application-context.xml")
@Configuration
public class MyApplicationContext {
...
}
如果bean在另一个@Configuration类中定义,我们可以使用@Import注释:
@Import(OtherConfiguration.class)
@Configuration
public class MyApplicationContext {
...
}
>在导入其他XML或@Configuration类之后,我们可以通过向@Configuration类声明私有成员来使用它们在我们的上下文中声明的bean,如下所示:
@Autowired
@Qualifier(value = "beanFromSomewhereElse")
private final StrangeBean beanFromSomewhereElse;
或者直接在方法中使用它作为参数,该方法使用@Qualifier定义依赖于此beanFromSomewhereElse的bean,如下所示:
@Bean(name = "anotherBean")
public AnotherClass getAnotherClass(@Qualifier (value = "beanFromSomewhereElse") final StrangeBean beanFromSomewhereElse) {
return new AnotherClassImpl(getSomeClass(), beanFromSomewhereElse);
}
>导入属性与从另一个xml或@Configuration类导入bean非常相似.我们将使用@Value而不是使用@Qualifier,如下所示:
@Autowired
@Value("${some.interesting.property}")
private final String someInterestingProperty;
这也可以与SpEL表达式一起使用.
>为了让spring将这些类视为bean容器,我们需要在主xml中将此标记放在上下文中:
<context:annotation-config/>
您现在可以导入@Configuration类与创建简单bean完全相同:
<bean class="some.package.MyApplicationContext"/>
有一些方法可以完全避免使用Spring XML,但它们不在本答案的范围内.您可以在我的blog post中找到其中一个选项,我的答案基于这些选项.
使用这种方法的优点和缺点
基本上我发现这种声明bean的方法比使用XML更舒服,因为我看到了一些优点:
>错别字 – @Configuration类被编译,错别字只是不允许编译
>快速失败(编译时) – 如果你忘记注入一个bean,你将在编译时失败,而不是像XML一样在运行时
>更容易在IDE中导航 – 在bean的构造函数之间理解依赖关系树.
>可以轻松调试配置启动
我看到它们的缺点并不多,但有一些我能想到的:
>滥用 – 代码比XML更容易被滥用
>使用XML,您可以基于在编译期间不可用但在运行时提供的类来定义依赖项.使用@Configuration类,您必须在编译时提供类.通常这不是问题,但有些情况可能会发生.
结论:在您的应用程序环境中组合XML,@ Configuration和annotations是完美的. Spring并不关心声明bean的方法.
内容总结
以上是互联网集市为您收集整理的java – 了解spring @Configuration类全部内容,希望文章能够帮你解决java – 了解spring @Configuration类所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。