This forum is now a read-only archive. All commenting, posting, registration services have been turned off. Those needing community support and/or wanting to ask questions should refer to the Tag/Forum map, and to http://spring.io/questions for a curated list of stackoverflow tags that Pivotal engineers, and the community, monitor.

Naming a bean using @Bean?

Jan 30th, 2009, 07:25 AM

It looks like it should be fairly easy to add the ability to name a bean using the @Bean annotation. Is there a reason this does not seem to have been done and would not be a good idea? (if there is already a way to name beans then my apologies and I would appreciate a docs link)

I certainly understand using the BeanNamingStrategy as a way to generally override default naming but it seems like a weakness to not be able to name beans when defining them.

You're quite right to expect this, and it's a gap in functionality right now. I'd appreciate it if you'd add a JIRA issue to this effect.

In the meantime, you can use the @Bean(aliases="customName") as a workaround. It will mean that the bean is registered under both names (from the method and from the alias) which may or may not work for you.