如何将SpringAOP加载到web应用程序中?
我试图在web应用程序中实现SpringAOP。不幸的是,我在网上找到的所有示例代码都是控制台应用程序。我不知道如何在web应用程序中实现它 在web.xml文件中,我加载applicationContext.xml如下:如何将SpringAOP加载到web应用程序中?,spring,aop,Spring,Aop,我试图在web应用程序中实现SpringAOP。不幸的是,我在网上找到的所有示例代码都是控制台应用程序。我不知道如何在web应用程序中实现它 在web.xml文件中,我加载applicationContext.xml如下: <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml<
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<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>
如果这是一个控制台应用程序,我宁愿将其放在main()中,但如何在web应用程序中执行此操作?加载上下文不需要以下代码:
ApplicationContext context = new ClassPathXmlApplicationContext("WEBINF/applicationContext.xml");
theBo = (ITheBo) context.getBean("theProxy");
您必须将ContextLoaderListener
添加到web.xml
文件中:
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
org.springframework.web.context.ContextLoaderListener
现在,当web应用程序启动时,将加载
contextConfigLocation中声明的上下文。在您的例子中,“/WEB-INF/applicationContext.xml”
如果您需要特定类中的上下文,可以实现ApplicationContextAware
接口来检索它
对于其余部分,您的webapp现在是一个基本的spring应用程序,您可以像平常一样连接您的类。您不需要以下代码来加载上下文:
ApplicationContext context = new ClassPathXmlApplicationContext("WEBINF/applicationContext.xml");
theBo = (ITheBo) context.getBean("theProxy");
您必须将ContextLoaderListener
添加到web.xml
文件中:
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
org.springframework.web.context.ContextLoaderListener
现在,当web应用程序启动时,将加载
contextConfigLocation中声明的上下文。在您的例子中,“/WEB-INF/applicationContext.xml”
如果您需要特定类中的上下文,可以实现ApplicationContextAware
接口来检索它
剩下的,你的webapp现在是一个基本的spring应用程序,你可以像平常一样连接你的类。多亏了@Dave Newton给了我线索。为了让我从web注入Proxy,我的例子是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>
org.springframework.web.jsf.DelegatingVariableResolver
行动
org.huahsin68.theAction
一场
泰博
#{theProxy}
由于@Dave Newton给了我线索,@tom提供的监听器也被放入了
web.xml
。为了让我从web注入Proxy,我的例子是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>
org.springframework.web.jsf.DelegatingVariableResolver
行动
org.huahsin68.theAction
一场
泰博
#{theProxy}
将@tom提供的侦听器放入
web.xml
中,您可以将代理连接到一个类中,该类与任何其他bean一样使用它。目前我只有一个bean,即ITheBo。为了让我连接Proxy,我需要为Proxy准备另一个bean。我被困在如何将代理转换为BO的问题上。你可以将代理连接到一个类中,该类与其他任何bean一样使用它。目前我只有一个bean,那就是ITheBo。为了让我连接Proxy,我需要为Proxy准备另一个bean。我被困住了,我怎么能把火箭投给theBo。
<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>