@@ -36,10 +36,35 @@ service = SampleService()
3636print service.method("something")
3737
3838"You sent me 'something'"
39- ]]> </programlisting >
40-
41- <para >Now, let's write an interceptor that will catch any results, and wrap them
42- with <![CDATA[ <Wrapped>]]> tags.</para >
39+ ]]> </programlisting >
40+
41+ <para >To configure the same thing using the IoC container, put the following text into a file named
42+ <filename >app-context.xml</filename >.</para >
43+
44+ <programlisting ><![CDATA[
45+ <?xml version="1.0" encoding="UTF-8"?>
46+ <objects xmlns="http://www.springframework.org/springpython/schema/objects"
47+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
48+ xsi:schemaLocation="http://www.springframework.org/springpython/schema/objects
49+ http://springpython.webfactional.com/schema/context/spring-python-context-1.0.xsd">
50+
51+ <object id="service" class="SampleService"/>
52+
53+ </objects>
54+ ]]> </programlisting >
55+
56+ <para >To instantiate the IoC container, use the following code.</para >
57+
58+ <programlisting ><![CDATA[
59+ from springpython.context import ApplicationContext
60+ from springpython.config import XMLConfig
61+
62+ container = ApplicationContext(XMLConfig("app-context.xml"))
63+ service = container.get_object("service")
64+ ]]> </programlisting >
65+
66+ <para >You can use either mechanism to define an instance of your service. Now, let's write an interceptor
67+ that will catch any results, and wrap them with <![CDATA[ <Wrapped>]]> tags.</para >
4368
4469<programlisting ><![CDATA[
4570from springpython.aop import *
@@ -60,16 +85,53 @@ class WrappingInterceptor(MethodInterceptor):
6085 must be created and given to the client. One way to create this is by creating a
6186 <classname >ProxyFactory</classname >. The factory is used to identify the target service
6287 that is being intercepted. It is used to create the dynamic proxy object to give back to
63- the client.</para >
88+ the client.</para >
89+
90+ <para >You can use the Spring Python APIs to directly create this proxied service.</para >
6491
6592<programlisting ><![CDATA[
6693from springpython.aop import *
6794factory = ProxyFactory()
6895factory.target = SampleService()
6996factory.interceptors.append(WrappingInterceptor())
7097service = factory.getProxy()
71- ]]> </programlisting >
72-
98+ ]]> </programlisting >
99+
100+ <para >Or, you can insert this definition into your <filename >app-context.xml</filename > file.</para >
101+
102+ <programlisting ><![CDATA[
103+ <?xml version="1.0" encoding="UTF-8"?>
104+ <objects xmlns="http://www.springframework.org/springpython/schema/objects"
105+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
106+ xsi:schemaLocation="http://www.springframework.org/springpython/schema/objects
107+ http://springpython.webfactional.com/schema/context/spring-python-context-1.0.xsd">
108+
109+ <object id="targetService" class="SampleService"/>
110+
111+ <object id="serviceFactory" class="springpython.aop.ProxyFactory">
112+ <property name="target" ref="targetService"/>
113+ <property name="interceptors">
114+ <object class="WrappingInterceptor"/>
115+ </property>
116+ </object>
117+
118+ </objects>
119+ ]]> </programlisting >
120+
121+ <para >If you notice, the original Spring Python "service" object has been renamed as "targetService", and there is, instead,
122+ another object called "serviceFactory" which is a Spring AOP ProxyFactory. It points to the target service and also has an
123+ interceptor plugged in. In this case, the interceptor is defined as an inner object, not having a name of its own,
124+ indicating it is not meant to be referenced outside the IoC container. When you get a hold of this, you can request a proxy.</para >
125+
126+ <programlisting ><![CDATA[
127+ from springpython.context import ApplicationContext
128+ from springpython.config import XMLConfig
129+
130+ container = ApplicationContext(XMLConfig("app-context.xml"))
131+ serviceFactory = container.get_object("serviceFactory")
132+ service = serviceFactory.getProxy()
133+ ]]> </programlisting >
134+
73135 <para >Now, the client can call <emphasis >service</emphasis >, and all function calls will
74136 be routed to <classname >SampleService</classname > with one simple detour through
75137 <classname >WrappingInterceptor</classname >.</para >
@@ -85,8 +147,8 @@ print service.method("something")
85147 dynamic nature, Spring Python AOP gives you the power to wrap your own source code
86148 as well as other 3rd party modules.</para >
87149
88- </section >
89-
150+ </section >
151+
90152 <section id =" aop-proxy-factory-objects" >
91153 <title >Proxy Factory Objects</title >
92154
@@ -102,8 +164,29 @@ service.interceptors = [WrappingInterceptor()]
102164print service.method(" proxy factory object")
103165
104166"You sent me a 'proxy factory object'"
105- ]]> </programlisting >
167+ ]]> </programlisting >
168+
169+ <para >To configure the same thing into your <filename >app-context.xml</filename > file, it looks like this:</para >
106170
171+ <programlisting ><![CDATA[
172+ <?xml version="1.0" encoding="UTF-8"?>
173+ <objects xmlns="http://www.springframework.org/springpython/schema/objects"
174+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
175+ xsi:schemaLocation="http://www.springframework.org/springpython/schema/objects
176+ http://springpython.webfactional.com/schema/context/spring-python-context-1.0.xsd">
177+
178+ <object id="targetService" class="SampleService"/>
179+
180+ <object id="service" class="springpython.aop.ProxyFactoryObject">
181+ <property name="target" ref="targetService"/>
182+ <property name="interceptors">
183+ <object class="WrappingInterceptor"/>
184+ </property>
185+ </object>
186+
187+ </objects>
188+ ]]> </programlisting >
189+
107190 <para >In this case, the <classname >ProxyFactoryObject</classname > acts as both a proxy
108191 and a factory. As a proxy, it behaves just like the target service would, and it also provides
109192 the ability to wrap the service with aspects. It saved us a step of coding, but more importantly,
0 commit comments