Announcement Announcement Module
Collapse
No announcement yet.
IoC avaiable at constructor bean? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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
    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.

    Comment


    • #3
      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

      Comment

      Working...
      X