0

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?

Eric B.
  • 23,425
  • 50
  • 169
  • 316

1 Answers1

0

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.

Blake Caldwell
  • 311
  • 2
  • 8
  • The problem I encountered already by leaving the `@Component` annotation while having an `@Configuration` definition for the same bean is that Spring would seemingly have two definitions for the same bean in its context. And when I would try to inject the bean, I would not necessary get the correct bean instance (with the parameters set). It had taken me a while to understand why I had two different bean instances, one that was property initialized, and one without any initialization. Once I had removed the `@Component`, I would only get a single instance. – Eric B. Nov 22 '13 at 12:47
  • I verified what you're saying in a sample project, then updated my answer. Thanks, I learned something here. – Blake Caldwell Nov 22 '13 at 19:58
  • The problem remains the same though. There does not seem to be a "clean" way of doing this. Using an autowired config object is not clean at all, in my opinion. So over the life of your project, you end up creating some beans using '@Component' or others using '@Bean' - basically there is nothing consistent. I was hoping someone would have a discovered a cleaner way of doing this. – Eric B. Nov 23 '13 at 00:23
  • If the configuration values can be represented via Spring's expression language (SPEL), you could use '@Value' annotations above setters in your '@Component' class. Properties defined in .property files will be available this way. If setup properly, I believe you can also compile with -D flags to set these variables. http://docs.spring.io/spring/docs/3.0.x/reference/expressions.html – Blake Caldwell Nov 23 '13 at 02:04
  • That's pretty much the only option I came up with as well, but not too thrilled with it either. I presumed there would be a way to specify default params without SpEL, but that might be my best shot. – Eric B. Nov 24 '13 at 02:18