Results 1 to 9 of 9

Thread: Advise on afterPropertiesSet

  1. #1
    Join Date
    Jan 2008
    Posts
    17

    Default Advise on afterPropertiesSet

    I wonder if Spring AOP can advise on afterPropertiesSet method, which is from InitializingBean interface.

    I tried to put a db reading logic in this method so as that the data only be read once at very beginning. But error occurs (hibernate session is closed) when afterPropertiesSet is invoked. In my application, AOP advised on all method within service tier with transactions which with the same edge as session. The seeion is closed error means no transaction wrapped on afterPropertiesSet method. I move the logic into other normal method, it works properly. Anyone has any clue of it? Thanks

  2. #2
    Join Date
    Jan 2008
    Posts
    17

    Default

    Quote Originally Posted by Yaozong View Post
    I wonder if Spring AOP can advise on afterPropertiesSet method, which is from InitializingBean interface.

    I tried to put a db reading logic in this method so as that the data only be read once at very beginning. But error occurs (hibernate session is closed) when afterPropertiesSet is invoked. In my application, AOP advised on all method within service tier with transactions which with the same edge as session. The seeion is closed error means no transaction wrapped on afterPropertiesSet method. I move the logic into other normal method, it works properly. Anyone has any clue of it? Thanks
    I found relevant explain in the Spring offical reference book (3.5.1.3)

    "Finally, please be aware that the Spring container guarantees that a configured initialization callback is called immediately after a bean has been supplied with all of its dependencies. This means that the initialization callback will be called on the raw bean reference, which means that any AOP interceptors or suchlike that will ultimately be applied to the bean will not yet be in place."

    So AOP doesn't apply to afterPropertiesSet method --> it doesn't wrapped with transaction --> session scope is not binded to it --> try to use dao in the method causes "session is closed" error

  3. #3
    Join Date
    Jan 2009
    Posts
    21

    Default

    Hi Yaozong. I've just encountered the very same issue. Did you ever find a solution to the problem?

  4. #4
    Join Date
    Jun 2006
    Location
    SF Bay Area, California
    Posts
    524

    Default

    You could use AspectJ weaving and mark the afterPropertiesSet() method with the @Transactional annotation. See Spring Reference documentation for more details.

    -Ramnivas
    Ramnivas Laddad (Follow me on Twitter)
    AspectJ in Action: Enterprise AOP with Spring Applications (2nd edition). Now available!

  5. #5
    Join Date
    Jan 2009
    Posts
    21

    Default

    Quote Originally Posted by ramnivas View Post
    You could use AspectJ weaving and mark the afterPropertiesSet() method with the @Transactional annotation. See Spring Reference documentation for more details.

    -Ramnivas
    I've actually already got the function marked with the @Transaction annotation but it seems to be ignoring it. I'm unfamiliar AspectJ, how does it differ from Spring AOP?

  6. #6
    Join Date
    May 2007
    Location
    Saint Petersburg, Russian Federation
    Posts
    1,189

    Default

    Quote Originally Posted by caskater4 View Post
    I'm unfamiliar AspectJ, how does it differ from Spring AOP?
    You may find the following article interesting then - Weaving with AspectJ

  7. #7
    Join Date
    Jan 2009
    Posts
    21

    Default

    Quote Originally Posted by denis.zhdanov View Post
    You may find the following article interesting then - Weaving with AspectJ
    I've read your article but it doesn't really apply to me. The issue I am having is not with Spring AOPs limitations or issues it's because the afterPropertiesSet function is being called before the @Transactional proxy is either created or properly set up. What I need is a way to delay or call the function so that @Transactional properly wraps my function call and opens my session. Since this bean is meant to be standalone I would need some way of setting a function for Spring to call once the bean is fully set up and proxied. I don't see how AspectJ will help me with this.

  8. #8
    Join Date
    May 2007
    Location
    Saint Petersburg, Russian Federation
    Posts
    1,189

    Default

    Quote Originally Posted by caskater4 View Post
    I've read your article but it doesn't really apply to me. The issue I am having is not with Spring AOPs limitations or issues it's because the afterPropertiesSet function is being called before the @Transactional proxy is either created or properly set up. What I need is a way to delay or call the function so that @Transactional properly wraps my function call and opens my session. Since this bean is meant to be standalone I would need some way of setting a function for Spring to call once the bean is fully set up and proxied. I don't see how AspectJ will help me with this.
    It looks like you still don't see the difference between spring aop and aspectj aop though it's crucial in your use-case. Aspectj doesn't create proxy for the bean, it injects aspect logic directly to the target class byte code. I.e. every time advised method is called on a 'raw' instance aspect logic is executed.

  9. #9
    Join Date
    Jan 2009
    Posts
    21

    Default

    Quote Originally Posted by denis.zhdanov View Post
    It looks like you still don't see the difference between spring aop and aspectj aop though it's crucial in your use-case. Aspectj doesn't create proxy for the bean, it injects aspect logic directly to the target class byte code. I.e. every time advised method is called on a 'raw' instance aspect logic is executed.
    I perfectly understood the difference between the two in that respect (but I re-read your blog anyway to double check). What your blog does not detail is how you can apply Spring's AOP aspects in AspectJ. I had to do some heavy reading of the Spring manual to sort that part out. Thankfully I was able to figure it all out. I am now using AspectJ's compile-time weaving along with Spring's aspects library to inject @Transactional into my classes which gives me exactly what we needed.

    Thank you very much for pointing me in the right direction.

Posting Permissions

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