1

I am using spring. I know how to use properties data inside a class but I need to know how to give component name from the properties file.

@Component("componentName") // here I need to give my property instead of "componentName".
public class TestClass {

}

I do search abt it but not able to find sos! Please help me...

Mohanraj
  • 396
  • 3
  • 8
  • 21

3 Answers3

4

I'm afraid that is not possible. All names that is given to @Controller, @Service and so on requires to be final/constant.

vegaasen
  • 1,022
  • 7
  • 16
1

If you can use additional XML config, use alias directive as explained in this other question:

<beans>
    <alias name="${service.class}" alias="Service"/>
    <context:property-placeholder location="example/app.properties"/>
    <context:component-scan base-package="example"/>
<beans>

If you need to use java config only, there's an open issue about it here. It is still unresolved, so for strictly java config the same is not currently possible.

If that's a problem, you can also register a bean programmatically as detailed here. So you'd autowire a property and your target bean, implement BeanDefinitionRegistryPostProcessor and register your bean in the registry you get as a parameter using your property.

eis
  • 51,991
  • 13
  • 150
  • 199
0

I'm not sure if I understand correctly, from Spring 3 or up, you can use @PropertySource annotation to externalize your configuration to a properties file. And then display the values with @Value

If you are talking about distinguishing configuration beans, @Qualifier is the thing you are looking for

Thamiar
  • 600
  • 7
  • 22