Announcement Announcement Module
Collapse
No announcement yet.
How do I wire a custom annotation at the same time as @Autowired? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How do I wire a custom annotation at the same time as @Autowired?

    Hi,

    I've got a custom logger annotation that is based off this post: http://jgeeks.blogspot.com/2008/10/a...to-spring.html

    It works fine unless I try to use the logger in a method annotated with @Autowired. For example,

    Code:
    @Repository
    public class MyDao
    {
        @AutowiredLogger
        private Logger _logger;
    
        private JdbcTemplate _jt;
    
        @Autowired
        public void setDatasource(DataSource ds)
        {
            _logger.debug("Entering setDs")
            _jt = new JdbcTemplate(ds);
            _logger.debut("Exiting setDs);
        }
    }
    Is there a way to have my @AutowiredLogger wired before or with @Autowired annotations? Section 4.6.1.1 of the Spring Framework Reference advises not to use the InitializingBean interface - is my only option to configure my setDatasource method as an init method in my configuration XML?

    Thanks,

    Paul

  • #2
    So what you're asking is a way to define the running order of BeanPostProcessors as your beans are being wired. According to docs, your BeanPostProcessor can implement the Ordered interface by returning some integer in getOrder() method. This integer is then compared to the order of other BeanPostProcessors. The source code of AutowiredAnnotationBeanPostProcessor shows that its default order is Ordered.LOWEST_PRECEDENCE - 2 so you should set your @AutowiredLogger post-processor to something like Ordered.LOWEST_PRECEDENCE - 3 to force it to run first.

    Comment


    • #3
      On a side note, is there any reason why you want setter methods logged like that?

      Comment


      • #4
        Originally posted by Osvaldas Grigas View Post
        On a side note, is there any reason why you want setter methods logged like that?
        No, that came about through tinkering and curiosity about what happens when. In general I don't log things like setter.

        Thank you for your solution as well!

        Paul

        Comment


        • #5
          Originally posted by Osvaldas Grigas View Post
          So what you're asking is a way to define the running order of BeanPostProcessors as your beans are being wired. According to docs, your BeanPostProcessor can implement the Ordered interface by returning some integer in getOrder() method. This integer is then compared to the order of other BeanPostProcessors. The source code of AutowiredAnnotationBeanPostProcessor shows that its default order is Ordered.LOWEST_PRECEDENCE - 2 so you should set your @AutowiredLogger post-processor to something like Ordered.LOWEST_PRECEDENCE - 3 to force it to run first.
          I need a little clarification - I should write a custom BeanPostProcessor then to handle this?

          Paul

          Comment


          • #6
            You should already have a custom BeanPostProcessor that processes @AutowiredLogger annotations. That's what the blog post that you refer to is based on.

            Comment


            • #7
              Originally posted by Osvaldas Grigas View Post
              You should already have a custom BeanPostProcessor that processes @AutowiredLogger annotations. That's what the blog post that you refer to is based on.
              Oh, right, duh...

              Thanks

              Paul

              Comment

              Working...
              X