• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

Can @Value inject bean?

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Using the STS Eclipse plugin for a Spring Template project for a Hibernate utility (using Spring 3.1), there is some code generated that I believe is injecting a bean vs. a value:

@Configuration
public class HibernateConfiguration {

@Value("#{dataSource}")
private DataSource dataSource;

And the XML file contains:

<jdbc:embedded-database id="dataSource" type="H2"/>

I've seen @Value used to inject properties, but is this is injecting the bean itself? And if so, why not use @Autowired or @Inject?

Thanks in advance.

Les

 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes it can,

Basically @Value isn't really about setting a value. So to speak. Meaning I would never do this

@Value("hello")
private String sayHello;

I would do

private String sayHello="hello"

So then what exactly is @Value for. Well it is for Spring Expression Language. And in the Spring Expression language it can evaluate to anything, any type.

So yes, the evaluation of @Value("#dataSource") is the bean with the id called dataSource, and that it what it was evaluated to the ref of the bean or the DataSource itself and the instance variable is set to that value.

Mark
 
Les Hartzman
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the response. The places where I've seen @Value used for strings has typically been when there is a properties file involved:

@Value(${jdbc.url})
private String jdbcUrl;

Could @Inject or @Autowired have been used in place as well?

Les
 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Les Hartzman wrote:Thanks for the response. The places where I've seen @Value used for strings has typically been when there is a properties file involved:

@Value(${jdbc.url})
private String jdbcUrl;

Could @Inject or @Autowired have been used in place as well?

Les



In this particular case, no. In the dataSource case way above, yeah. I probably would have chosed @Autowired or @Inject instead. You could partially argue that with @Value is like @Autowired with @Qualifier without it being 2 annotations, now it is just one.

Mark
 
reply
    Bookmark Topic Watch Topic
  • New Topic