How to set value for primitive properties of a bean?
Since we have @Component annotation and also @Autowired annotation also is for binding instance dependencies, so what about primitive properties?
@Component
class Person{
@Autowired
Address address;
int age /// what about this one?
}
For primitives you can use the
@Valueannotation. The usual scenario is to have aPropertyPlaceholderConfigurerwhich has loaded the values from a properties file, and then have@Value("${property.key}")You can also define your values as beans, which is more old-school:
and then