我試圖在web應用中實現Spring AOP。不幸的是,我在Web上找到的所有示例代碼都是控制檯應用程序。我正在用盡線索我怎麼能在網絡應用程序中做到這一點?如何將Spring AOP加載到Web應用程序中?
在web.xml文件中,我加載applicationContext.xml的是這樣的:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
在applicationContext.xml文件,我有ProxyFactryBean是這樣定義的:
<bean id="theBo" class="my.package.TheBo">
...
</bean>
<bean id="theProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces">
<list>
<value>my.package.ITheBo</value>
</list>
</property>
<property name="target" ref="theBo"/>
<property name="interceptorNames">
<list>
<value>loggingBeforeAdvice</value>
</list>
</property>
</bean>
我現在的情況是我不知道哪裏是放置此代碼的最佳位置:
ApplicationContext context = new ClassPathXmlApplicationContext("WEB-INF/applicationContext.xml");
theBo = (ITheBo) context.getBean("theProxy");
如果這是一個控制檯應用程序,我寧願把它放在main()中,但我怎麼能在web應用程序中做到這一點?
你會將代理連接到一個使用它的類,就像任何其他bean一樣。 –
目前我只有一個bean,它是ITheBo。爲了讓我連接代理,我需要爲Proxy配置另一個bean。我被困在如何將代理投向博弈。 – huahsin68