Okay, I see the problem. It is one of the gotcha's when you use Python's class-level variables. I thought this might be the issue. When you refer to global_app_context inside the set_app_context method, you need to prefix it with the class name, or it will treat it like a local variable.
Here is my sample where everything is running correctly.
main.py
Code:
from springpython.context import ApplicationContext
from springpython.config import XMLConfig
from context import ApplicationContextProvider
import logging
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
ch.setFormatter(logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s"))
logger = logging.getLogger("springpython")
logger.setLevel(logging.DEBUG)
logger.addHandler(ch)
my_logger = logging.getLogger("app_context_aware")
my_logger.setLevel(logging.DEBUG)
my_logger.addHandler(ch)
container = ApplicationContext(XMLConfig("app-context.xml"))
service = container.get_object("ApplicationContextProvider")
my_logger.debug("Service = %s" % service)
my_logger.debug("Application context = %s" % ApplicationContextProvider.global_app_context)
my_own_instance = ApplicationContextProvider()
my_logger.debug("Creating my own instance of ApplicationContextProvider => %s" % my_own_instance)
my_logger.debug("It should have the same app context: %s" % my_own_instance.global_app_context)
context.py
Code:
from springpython.context import ApplicationContextAware
import logging
logger = logging.getLogger("app_context_aware.ApplicationContextProvider")
class ApplicationContextProvider(ApplicationContextAware):
global_app_context = None
def set_app_context(self, app_context):
logger.debug("Updating global_app_context to %s" % app_context)
ApplicationContextProvider.global_app_context = app_context
And my app-context.xml file:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<objects xmlns="http://www.springframework.org/springpython/schema/objects/1.1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/springpython/schema/objects/1.1
http://springpython.webfactional.com/schema/context/spring-python-context-1.1.xsd">
<object id="ApplicationContextProvider" class="context.ApplicationContextProvider"/>
</objects>
Here is the output:
Code:
(sp)gturnquist-mbp:app_context_aware gturnquist$ python main.py
2011-10-27 12:23:00,449 - springpython.container.ObjectContainer - DEBUG - === Scanning configuration <springpython.config._xml_config.XMLConfig object at 0x10064f310> for object definitions ===
2011-10-27 12:23:00,449 - springpython.config.XMLConfig - DEBUG - ==============================================================
2011-10-27 12:23:00,450 - springpython.config.XMLConfig - DEBUG - * Parsing app-context.xml
2011-10-27 12:23:00,452 - springpython.config.XMLConfig - DEBUG - object: props = []
2011-10-27 12:23:00,453 - springpython.config.XMLConfig - DEBUG - object: There are 0 props
2011-10-27 12:23:00,453 - springpython.config.XMLConfig - DEBUG - ==============================================================
2011-10-27 12:23:00,453 - springpython.config.XMLConfig - DEBUG - Parsed id=ApplicationContextProvider props=[] scope=scope.SINGLETON factory=ReflectiveObjectFactory(context.ApplicationContextProvider)
2011-10-27 12:23:00,453 - springpython.container.ObjectContainer - DEBUG - ApplicationContextProvider object definition does not exist. Adding to list of definitions.
2011-10-27 12:23:00,453 - springpython.container.ObjectContainer - DEBUG - === Done reading object definitions. ===
2011-10-27 12:23:00,453 - springpython.context.ApplicationContext - DEBUG - Eagerly fetching ApplicationContextProvider
2011-10-27 12:23:00,453 - springpython.context.ApplicationContext - DEBUG - Did NOT find object 'ApplicationContextProvider' in the singleton storage.
2011-10-27 12:23:00,453 - springpython.context.ApplicationContext - DEBUG - Creating an instance of id=ApplicationContextProvider props=[] scope=scope.SINGLETON factory=ReflectiveObjectFactory(context.ApplicationContextProvider)
2011-10-27 12:23:00,453 - springpython.factory.ReflectiveObjectFactory - DEBUG - Creating an instance of context.ApplicationContextProvider
2011-10-27 12:23:00,453 - springpython.context.ApplicationContext - DEBUG - Stored object 'ApplicationContextProvider' in container's singleton storage
2011-10-27 12:23:00,454 - app_context_aware.ApplicationContextProvider - DEBUG - Updating global_app_context to <springpython.context.ApplicationContext object at 0x10064f390>
2011-10-27 12:23:00,454 - app_context_aware - DEBUG - Service = <context.ApplicationContextProvider object at 0x10064f490>
2011-10-27 12:23:00,454 - app_context_aware - DEBUG - Application context = <springpython.context.ApplicationContext object at 0x10064f390>
2011-10-27 12:23:00,454 - app_context_aware - DEBUG - Creating my own instance of ApplicationContextProvider => <context.ApplicationContextProvider object at 0x10064ff10>
2011-10-27 12:23:00,454 - app_context_aware - DEBUG - It should have the same app context: <springpython.context.ApplicationContext object at 0x10064f390>
2011-10-27 12:23:00,454 - springpython.context.ApplicationContext - DEBUG - Invoking the destroy_method on registered objects
2011-10-27 12:23:00,454 - springpython.context.ApplicationContext - DEBUG - Successfully invoked the destroy_method on registered objects