Results 1 to 2 of 2

Thread: An aspect to introduce common @PostConstruct code

  1. #1
    Join Date
    Jun 2011
    Location
    Boston, USA
    Posts
    1

    Question An aspect to introduce common @PostConstruct code

    I have a bit of @PostConstruct code that I want in many, many spring objects.

    Is it possible to use an aspect to automatically introduce this code on any spring bean with a particular annotation (say @MyComponent) ?

    To recap:
    - If a spring bean has @MyComponent annotation
    - It should get the following method and code automatically injected:
    @PostConstruct
    public void myComponentStart()
    {
    // common code here
    }
    - Spring should then instantiate the bean itself as normal, but it should see this @PostConstruct and execute it as you would expect it to had the original code been in this class all along

    Is this possible?

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    You have to use either loadtime or compiletimeweaving. If you want to same code in a lot of other beans why not simply make an abstract super class and have all the other classes extend that... I would first try to solve it without aspects (basically trying to keep it simple) instead of with aspects, adds a lot of complexity to your application.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

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
  •