Announcement Announcement Module
Collapse
No announcement yet.
Object post-processing for prototype objects not working Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

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

    Comment


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

      Comment

      Working...
      X