Announcement Announcement Module
Collapse
No announcement yet.
Advise on afterPropertiesSet Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

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

    Comment


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

      Comment


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

        -Ramnivas

        Comment


        • #5
          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?

          Comment


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

            Comment


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

              Comment


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

                Comment


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

                  Comment

                  Working...
                  X