Do I have to drop @Component when wanting to set bean parameters via JavaConfig? Is there another way?

StackOverflow https://stackoverflow.com/questions/20137252

Pergunta

I've got a Spring bean that is annotated with @Component. But I want to set/specify bean parameters. From what I can tell, my only option if I want to stick with annotations is to use an @Configuration class to specify the parameters when the bean is instantiated. However, in that case, I have to drop the @Component since it is up to the @Configuration class to declare the bean.

Essentially, using @Configuration and @Component would appear to be mutually exclusive. Similarly, declaring the bean in XML and annotating with @Component would be mutually exclusive as well.

I find it a little messy to have declarations in two possible places: the class itself (if annotated with @Component and not requiring any special initialization) and within a JavaConfig class. Am I the only one that sees it like this? Or is there a way to specify parameters while still using the @Comonent annotation?

Foi útil?

Solução

Looks like @Component will win over @Bean when you're wiring by type, but it also looks like this isn't documented anywhere, so you might not want to rely on it.

https://jira.springsource.org/browse/SPR-10795?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel

If you really want to go full annotation, and have no @Configuration or XML configuration, then you could autowire a config object that has all of the settings you need. In my opinion, the class is much cleaner if it takes simple configuration parameters with no idea of where they came from, so I'd use a @Configuration class.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top