如何为bean的原始属性设置值?
既然我们有@Component注解,@Autowired注解也用于绑定实例依赖,那么原始属性呢?
@Component
class Person{
@Autowired
Address address;
int age /// what about this one?
}
Run Code Online (Sandbox Code Playgroud)
对于基元,您可以使用@Value注释。通常的情况是PropertyPlaceholderConfigurer从属性文件中加载值,然后@Value("${property.key}")
你还可以将你的值定义为 beans,这是更老式的:
<bean id="foo" class="java.lang.Integer" factory-method="valueOf">
<constructor-arg value="20" />
</bean>
Run Code Online (Sandbox Code Playgroud)
进而
@Autowired
@Qualifier("foo")
private int foo;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7165 次 |
| 最近记录: |