2012-06-08 49 views
0

我试图在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应用程序中做到这一点?

+0

你会将代理连接到一个使用它的类,就像任何其他bean一样。 –

+0

目前我只有一个bean,它是ITheBo。为了让我连接代理,我需要为Proxy配置另一个bean。我被困在如何将代理投向博弈。 – huahsin68

回答

0

感谢@Dave Newton给我提供了线索。为了让我从网上注入theProxy,对于我的情况,这是JSF,我必须在faces-config.xml中输入以下代码。

<application> 
    <variable-resolver> 
     org.springframework.web.jsf.DelegatingVariableResolver 
    </variable-resolver> 
</application> 

<managed-bean> 
    <managed-bean-name>theAction</managed-bean-name> 
    <managed-bean-class>org.huahsin68.theAction</managed-bean-class> 
     <managed-bean-scope>session</managed-bean-scope> 
     <managed-property> 
     <property-name>theBo</property-name> 
      <value>#{theProxy}</value> 
     </managed-property> 
</managed-bean> 

及认沽通过提供@汤姆以及进入web.xml听众。

3

你不需要下面的代码加载背景:

ApplicationContext context = new ClassPathXmlApplicationContext("WEBINF/applicationContext.xml"); 
theBo = (ITheBo) context.getBean("theProxy"); 

您必须添加ContextLoaderListenerweb.xml文件:

<listener> 
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
</listener> 

现在,当你的web应用程序启动将加载<context-param> contextConfigLocation中声明的上下文。在你的情况'/WEB-INF/applicationContext.xml'。

如果你需要你的上下文在一个特定的类,你可以实现ApplicationContextAware接口来检索它。

其余的,你的web应用程序现在是一个基本的弹簧应用程序,你可以像平时一样连接你的类。

相关问题