Results 1 to 7 of 7

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

  1. #1
    Join Date
    Jan 2011
    Posts
    18

    Question 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. #2
    Join Date
    Sep 2009
    Location
    Vilnius, Lithuania
    Posts
    118

    Default

    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.

  3. #3
    Join Date
    Sep 2009
    Location
    Vilnius, Lithuania
    Posts
    118

    Default

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

  4. #4
    Join Date
    Jan 2011
    Posts
    18

    Default

    Quote 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

  5. #5
    Join Date
    Jan 2011
    Posts
    18

    Default

    Quote 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

  6. #6
    Join Date
    Sep 2009
    Location
    Vilnius, Lithuania
    Posts
    118

    Default

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

  7. #7
    Join Date
    Jan 2011
    Posts
    18

    Default

    Quote 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

Tags for this Thread

Posting Permissions

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