Results 1 to 3 of 3

Thread: IoC avaiable at constructor bean?

  1. #1
    Join Date
    Apr 2011
    Posts
    1

    Default IoC avaiable at constructor bean?

    Please, i'm trying to develop a jsf/spring aplication using IoC. I've followed some tutorials and according to those i put the applicationContext.xml under the WEB-INF and, in my jsf Bean, with the use of the annotation @Resource, i've mapped my Dao. It works fine when i execute the Bean's getters to retreive information from database, but the map only occurs after my Bean have been constructed (i can't acess the dao at the Bean constructor).
    In some cases i want to execute Dao's methods at Bean's construction time. How can I do this?
    Thanks

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

    Default

    You cannot...

    Properties are injected after construction time (there has to be an instance before we can call methods on it). So either use constructor based injection or find another means/change your architecture to call the dao.
    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

    Default

    You could implement the InitializingBean interface which has one method (afterPropertiesSet()) which is called after all properties are set on the bean. The problem with this is that it ties your class to the Spring API.

    Another alternative is to create a method which can be declared as the init-method in the xml declaration of your bean (if you are using xml to declare your bean)

    Code:
    <bean id="exampleInitBean" class="examples.ExampleBean" init-method="init"/>
    If your using annotations then use the @PostConstruct annotation for your init method.

    HTH

Posting Permissions

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