当前位置: 代码迷 >> Web前端 >> Spring AOP 入门范例
  详细解决方案

Spring AOP 入门范例

热度:343   发布时间:2012-08-24 10:00:21.0
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>??
1 楼 Roshan2 2011-09-30  
http://lijiejava.iteye.com/blog/1180331
  相关解决方案