About injecting @Qualified Strings (or other native values)
Hi,
Let's say I have a service Pojo to which I want to inject a String. Let's say the string is kind of a flag used overall the system.
I'd like to have something like this in my service:Code:
class MyServiceImpl implements MyService {
@Autowired @Qualifier(quot;databaseCatalogquot;) public void setDatabaseCatalog(String databaseCatalog) { this.databaseCatalog = databaseCatalog; } ...
}
I have a Guice background, and I'm quite new to Spring annotations so I wonder whether this makes sense in Spring. How should my xml file look like to specify the candidate value of the string?
Thanks in advance for your comments,
-walbar
You can use @Value annotation for that - 7.4.2 Annotation-based configuration
Originally Posted by walbarHow should my xml file look like to specify the candidate value of the string?
As Dennis mentioned, you can use @Value in Spring 3.0; however, it's still possible to achieve autowiring of Strings in Spring 2.5.x using @Autowired and @Qualifier as you've shown above. The XML for your example would then simply be the following:Code:
lt;bean id=quot;databaseCatalogquot; class=quot;java.lang.Stringquot;gt;
lt;constructor-arg value=quot;XXXquot; /gt;
lt;/beangt;
Note that the bean's ID matches the qualifier string.
Replace quot;XXXquot; with an appropriate value.
Regards,
Sam
Cool. Thanks for your answers! (I'm using 2.5) |