Spring AOP 入门实例

以Struts2+Spring为例,要求必须在登录之后才能实现某一链接。如果未登录则抛出异常,跳转到异常页面。

假设链接为:http://localhost:8080/aop/test.action

Struts2的配置文件struts.xml文件:

Java代码 复制代码  收藏代码
  1. <action name="test" class="testAction" method="test">   
  2.       <result name="success">/succ.jsp</result>   
  3. </action>  
<action name="test" class="testAction" method="test">
      <result name="success">/succ.jsp</result>
</action>



Spring中的TestAction配置:

Java代码 复制代码  收藏代码
  1. <bean id="testAction" class="action.TestAction"/>  
<bean id="testAction" class="action.TestAction"/>



TestAction类中的test方法只是实现简单的跳转,不需要考虑是否登录问题:

Java代码 复制代码  收藏代码
  1. public String test() throws Exception{    
  2.       return "success";      
  3. }  
public String test() throws Exception{ 
      return "success";   
}



此时点击链接能够跳转到succ.jsp。

加入登录检查

在applicationContext.xml文件中:

Java代码 复制代码  收藏代码
  1. <?xml version="1.0" encoding="UTF-8"?>   
  2.   
  3. <beans xmlns="http://www.springframework.org/schema/beans"  
  4.          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.          xmlns:aop="http://www.springframework.org/schema/aop"  
  6.          xmlns:tx="http://www.springframework.org/schema/tx"  
  7.          xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd   
  8.            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd   
  9.            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">   
  10.     
  11.   
  12.      <bean id="testAction" class="test.TestAction"/>   
  13.         
  14.      <!-- 定义通知 -->   
  15.      <bean id="loginCheck" class="aop.LoginCheck" scope="prototype"/>    
  16.         
  17.      <!--定义Advisor,即Spring中的切面(由通知和切入点组成)-->   
  18.      <bean id="loginAdvisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">   
  19.          <property name="advice" ref="loginCheck"></property>   
  20.          <property name="mappedNames">   
  21.             <list>     
  22.                 <!-- 需要被拦截的方法名,为了清除显示,   
  23.                通过在struts.xml文件中设置method选项,来避免execute方法名 -->   
  24.                <value>test</value>     
  25.             </list>   
  26.         </property>    
  27.      </bean>    
  28.             
  29.      <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">   
  30.         <property name="beanNames">   
  31.            <list>   
  32.                 <!-- 需要被拦截的bean的名称 -->     
  33.                 <value>testAction</value>     
  34.            </list>   
  35.         </property>   
  36.         <property name="interceptorNames">   
  37.             <list>   
  38.                <value>loginAdvisor</value>     
  39.             </list>   
  40.         </property>   
  41.      </bean>   
  42.   
  43. </beans>  
<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
	     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	     xmlns:aop="http://www.springframework.org/schema/aop"
	     xmlns:tx="http://www.springframework.org/schema/tx"
	     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
 

     <bean id="testAction" class="test.TestAction"/>
     
     <!-- 定义通知 -->
     <bean id="loginCheck" class="aop.LoginCheck" scope="prototype"/> 
     
     <!--定义Advisor,即Spring中的切面(由通知和切入点组成)-->
     <bean id="loginAdvisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
         <property name="advice" ref="loginCheck"></property>
         <property name="mappedNames">
            <list>  
                <!-- 需要被拦截的方法名,为了清除显示,
               通过在struts.xml文件中设置method选项,来避免execute方法名 -->
               <value>test</value>  
            </list>
        </property> 
     </bean> 
         
     <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
        <property name="beanNames">
           <list>
                <!-- 需要被拦截的bean的名称 -->  
                <value>testAction</value>  
           </list>
        </property>
        <property name="interceptorNames">
            <list>
               <value>loginAdvisor</value>  
            </list>
        </property>
     </bean>

</beans>



LoginCheck类:

Java代码 复制代码  收藏代码
  1. public class LoginCheck implements MethodInterceptor{   
  2.   
  3.     public Object invoke(MethodInvocation invocation) throws Throwable {   
  4.         HttpSession session = ServletActionContext.getRequest().getSession();   
  5.         Object obj = session.getAttribute("loginuser");   
  6.         if(obj==null){   
  7.             throw new NoLoginException("您还没有登录!!!");     
  8.         }   
  9.         return invocation.proceed();           
  10.     }   
  11.   
  12. }  
public class LoginCheck implements MethodInterceptor{

	public Object invoke(MethodInvocation invocation) throws Throwable {
		HttpSession session = ServletActionContext.getRequest().getSession();
		Object obj = session.getAttribute("loginuser");
		if(obj==null){
			throw new NoLoginException("您还没有登录!!!");  
		}
		return invocation.proceed();		
	}

}



为了在抛出NoLoginException异常时能够跳转到相关页面,可以在web.xml中设置:

Java代码 复制代码  收藏代码
  1. <error-page>   
  2.      <exception-type>exception.NoLoginException</exception-type>   
  3.      <location>/error.jsp</location>   
  4. </error-page>  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值