Results 1 to 3 of 3

Thread: Injecting data using @Value into @Configurable classes

  1. #1
    Join Date
    Dec 2008
    Posts
    26

    Default Injecting data using @Value into @Configurable classes

    Hi,

    I'm trying to inject String using @Value into @Configurable class. Class itself is a JSF Converter, and therefore is not Spring managed. That's why I'm using @Configurable. Trick is, can I do that injection without defining prototype bean in my XML? Without prototype I get:

    Code:
    org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'pl.wroc.jug.jb.web.converter.DateConverter' is defined
    With prototype everything works as expected. My problem is: why I have to define this prototype when injecting using @Value, while when injecting using @Inject or @Autowire can do without prototype? Is it a feature or a bug? My prototype doesn't do anything interesting:

    Code:
    <bean class="pl.wroc.jug.jb.web.converter.DateConverter" scope="prototype"/>
    Best regards

    Jacek Bilski

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    @Value is replaced using a BeanFactoryPostProcessor which is only run BEFORE bean creation. Spring doesn't control the creation and hence cannot do anything with the @Value.

    @Autowired by default does dependency injection by type, if you use @Configurable it checks the application context and let that do autowiring. Now if you have a prototype bean of the same type of your @Configurable defined it is going to use that as the blue print to wire/configure your new bean.

    So it is neither a undocumented feature nor a bug it is the way an ApplicationContext works.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Feb 2013
    Posts
    2

    Default

    Quote Originally Posted by Marten Deinum View Post
    @Value is replaced using a BeanFactoryPostProcessor which is only run BEFORE bean creation. Spring doesn't control the creation and hence cannot do anything with the @Value.

    @Autowired by default does dependency injection by type, if you use @Configurable it checks the application context and let that do autowiring. Now if you have a prototype bean of the same type of your @Configurable defined it is going to use that as the blue print to wire/configure your new bean.

    So it is neither a undocumented feature nor a bug it is the way an ApplicationContext works.
    Is this now possible in Spring 3? Is there any trickery I can use to get @Value to work with @Configurable?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •