Avi*_*Avi 106 java configuration spring autowired
在了解了Spring @Autowired用法的问题之后,我想为另一个弹簧布线选项(@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>
Run Code Online (Sandbox Code Playgroud)
我该怎么用@Configuration呢?它对代码本身有什么影响吗?
Avi*_*Avi 148
@Configuration可以@Configuration通过几个步骤将xml迁移到a :
创建带@Configuration注释的类:
@Configuration
public class MyApplicationContext {
}
Run Code Online (Sandbox Code Playgroud)对于每个<bean>标记,创建一个注释为的方法@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
}
}
Run Code Online (Sandbox Code Playgroud)为了导入,beanFromSomewhereElse我们需要导入它的定义.它可以在XML中定义,我们将使用@ImportResource:
@ImportResource("another-application-context.xml")
@Configuration
public class MyApplicationContext {
...
}
Run Code Online (Sandbox Code Playgroud)
如果bean在另一个@Configuration类中定义,我们可以使用@Import注释:
@Import(OtherConfiguration.class)
@Configuration
public class MyApplicationContext {
...
}
Run Code Online (Sandbox Code Playgroud)在我们导入其他XML或@Configuration类之后,我们可以通过向@Configuration类声明私有成员来使用它们在我们的上下文中声明的bean ,如下所示:
@Autowired
@Qualifier(value = "beanFromSomewhereElse")
private final StrangeBean beanFromSomewhereElse;
Run Code Online (Sandbox Code Playgroud)
或者直接在方法中使用它作为参数,该方法beanFromSomewhereElse使用@Qualifier如下定义依赖于此的bean :
@Bean(name = "anotherBean")
public AnotherClass getAnotherClass(@Qualifier (value = "beanFromSomewhereElse") final StrangeBean beanFromSomewhereElse) {
return new AnotherClassImpl(getSomeClass(), beanFromSomewhereElse);
}
Run Code Online (Sandbox Code Playgroud)导入属性与从另一个xml或@Configuration类导入bean非常相似.而不是使用@Qualifier我们将使用@Value属性如下:
@Autowired
@Value("${some.interesting.property}")
private final String someInterestingProperty;
Run Code Online (Sandbox Code Playgroud)
这也可以与SpEL表达式一起使用.
为了允许spring将这些类视为bean容器,我们需要在主xml中将此标记放在上下文中:
<context:annotation-config/>
Run Code Online (Sandbox Code Playgroud)
您现在可以导入@Configuration与创建简单bean完全相同的类:
<bean class="some.package.MyApplicationContext"/>
Run Code Online (Sandbox Code Playgroud)
有一些方法可以完全避免使用Spring XML,但它们不在本答案的范围内.您可以在我的博客文章中找到其中一个选项,我的答案基于这些选项.
基本上我发现这种声明bean的方法比使用XML更舒服,因为我看到了一些优点:
@Configuration编译类,错别字只是不允许编译我看到它们的缺点并不多,但有一些我能想到的:
@Configuration类,您必须在编译时提供类.通常这不是问题,但有些情况可能会发生.结论:在应用程序上下文中组合XML @Configuration和注释是完美的.Spring并不关心声明bean的方法.
| 归档时间: |
|
| 查看次数: |
57622 次 |
| 最近记录: |