I'm having some issues with injection in the application I'm working on (using Spring Version 3.1.2). To start with, I'm seeing a lot of code like this:
@Value("#{searchRequestBean}")
private SearchRequest searchRequest;
@Value("#{searchResponseBean}")
private SearchResponse searchResponse;
@Autowired
private SavedSearchService service;
Each of these three appears to have the effect of autowiring the specified bean/service into the class. What I don't understand is, what's the difference between @Value
and @Autowired
in these cases? Every example I find online seems to use @Value
to inject values from a properties file. In this case, SearchResponse
and SearchRequest
are abstract classes.
I'm hoping that a better understanding of this will help me solve some issues I'm having with my Session bean.
@Value
can be used for injecting default values. A good example is to inject the default of a String
to be the value of a property file. In your example, @Value
is used to set the default value of a class to be a Spring managed bean.
@Autowired
can't be used for the first example: It's not property file aware. @Autowired
is only for DI of a bean. It is more specific than @Value
, but you can use @Value
to do the same thing.
Here is a good tutorial for @Value
: http://www.mkyong.com/spring3/spring-value-default-value/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With