Results 1 to 3 of 3

Thread: Object post-processing for prototype objects not working

  1. #1
    Join Date
    Oct 2012
    Posts
    1

    Default Object post-processing for prototype objects not working

    Hello,

    it seems that the object post-processing is not used for objects with scope PROTOTYPE. I'm using @transactional which works on an object with SINGLETON scope but not on an object with PROTOTYPE scope. It's unclear if it is the desired behaviour or a bug. Does anybody know the answer?

    Regards,
    Valeriy

  2. #2
    Join Date
    Aug 2006
    Posts
    382

    Default

    I looked deeply into the code, and realize why this is happening.

    Code:
            post_processors = [object for object in self.objects.values() if isinstance(object, ObjectPostProcessor)]
    
            for obj_name, obj in self.objects.iteritems():
                if not isinstance(obj, ObjectPostProcessor):
                    for post_processor in post_processors:
                        self.objects[obj_name] = post_processor.post_process_before_initialization(obj, obj_name)
    
    
            for object in self.objects.values():
                self._apply(object)
    
            for obj_name, obj in self.objects.iteritems():
                if not isinstance(obj, ObjectPostProcessor):
                    for post_processor in post_processors:
                        self.objects[obj_name] = post_processor.post_process_after_initialization(obj, obj_name)
    If you'll notice, the post processors are fetched from self.objects, which are the instantiated objects. From there, they iterated over, and applied to all self.objects. Surprise, surprise: self.objects only contains cached objects, i.e. SINGLETON-scoped objects. PROTOTYPE-scoped objects never get stored in self.objects, but are instead created on the spot every time.

    This isn't by design, except for this only working inside an ApplicationContext.
    Greg L. Turnquist (@gregturn), SpringSource/VMware
    Project Lead: Spring Python and author of Spring Python 1.1 and Python Testing Cookbook.
    Listen to Pond Jumpers, the international podcast for open source developers.
    These comments are my own personal opinions, and do not reflect those of my company.

  3. #3
    Join Date
    Aug 2006
    Posts
    382

    Default

    Looking closer at things, it would also appear that lazy objects will suffer not having post processors applied either. Essentially, this gets done when an ApplicationContext eagerly fetches SINGLETON objects.

    I tried to think in my mind how to handle this. If post processor application logic was moved to get_object(), so that it got applied to any type of object when it was requested, we would have to be sure to handle the situation so that it doesn't get applied twice to the same object.

    Post processors also benefit from the fact that they are themselves must be SINGLETONs and this application only happens after all other SINGLETONs have been created. Because they all already exist, we can scan the list of objects, and pick out the post processors. If we move this process to get_object(), then there is no guarantee they have been detected yet. For example, what if you are requesting the first object in the container and it's not a post processor. That would mean the post processor hadn't been created yet, and it wouldn't be easy to check if any exist (yet).
    Greg L. Turnquist (@gregturn), SpringSource/VMware
    Project Lead: Spring Python and author of Spring Python 1.1 and Python Testing Cookbook.
    Listen to Pond Jumpers, the international podcast for open source developers.
    These comments are my own personal opinions, and do not reflect those of my company.

Posting Permissions

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