最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringMVC用XML方式實現(xiàn)AOP的方法示例

 更新時間:2020年04月16日 11:58:19   作者:hmmmq!  
這篇文章主要介紹了SpringMVC用XML方式實現(xiàn)AOP的方法示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

1.首先創(chuàng)建web工程,之后導入Spring jar包,目錄如下


2.文件代碼

2.1AfterAdvice

package com.niit.aop;

import java.lang.reflect.Method;

import org.springframework.aop.AfterReturningAdvice;
/*
 * 后置通知
 * havingClass方法執(zhí)行之后才執(zhí)行。
 * 輸出日記
 * */
public class AfterAdvice implements AfterReturningAdvice {

	@Override
	public void afterReturning(Object arg0, Method arg1, Object[] arg2, Object arg3) throws Throwable {
		// TODO Auto-generated method stub
		System.out.println("后置攔截:下課之后寫作業(yè)");
	}
}

2.2BeforeAdvice

package com.niit.aop;
import java.lang.reflect.Method;
import org.springframework.aop.MethodBeforeAdvice;
public class BeforeAdvice implements MethodBeforeAdvice {
/*
 * 前置通知
 * 在havingClass切入點方法執(zhí)行之前通知
 * 用于驗證用戶的合法性。/判斷一些數(shù)據(jù)是否存在。適用于檢索。注冊判斷用戶名是否存在。
 * */
	@Override
	public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable {
		// TODO Auto-generated method stub
		System.out.println("前面攔截:上課之前要點名!在調用havingClass方法之前調用");

	}
}

2.3StudentIntercepter

package com.niit.aop;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

public class StudentIntercepter implements MethodInterceptor {

	@Override
	public Object invoke(MethodInvocation method) throws Throwable {
		// TODO Auto-generated method stub
		if(method.getArguments().length>0) {
			String name=(String)method.getArguments()[0];
			if("hmq".equals(name)){
				System.out.println("中間攔截:你是hmq");
			}
			else {
				System.out.println("中間攔截:你是學生");
			}
			method.proceed();
		}
		return null;
	}
}

2.4StudentIF

package com.niit.logic;
public interface StudentIF {
	public void havingClass(String name);
	public void dohomework(String name);
	
}

2.5Student

package com.niit.logic;

public class Student implements StudentIF {

	//作為aop的目標方法
public void havingClass(String name) {
	System.out.println("aop的目標方法");
	System.out.println(name+"正在上課");
}
public void dohomework(String name) {
	System.out.println(name+"正在寫作業(yè)");
}
}

2.6StudentLogic

package com.niit.logic;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class StudentLogic {
	public static void main(String[] args) {
		ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
		StudentIF s=(StudentIF)context.getBean("student");
		s.havingClass("hmq");
		System.out.println("---------------");
		s.dohomework("hmq");
		System.out.println("---------------");
		s.havingClass("abc");
		System.out.println("---------------");
		s.dohomework("abc");
		System.out.println("---------------");
		
	}

}

2.7applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:context="http://www.springframework.org/schema/context"
  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" xmlns:p="http://www.springframework.org/schema/p"
  xmlns:util="http://www.springframework.org/schema/util" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
  xmlns:cache="http://www.springframework.org/schema/cache"
  xsi:schemaLocation="
  http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context/spring-context.xsd
  http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans.xsd
  http://www.springframework.org/schema/tx
  http://www.springframework.org/schema/tx/spring-tx.xsd
  http://www.springframework.org/schema/jdbc
  http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd
  http://www.springframework.org/schema/cache
  http://www.springframework.org/schema/cache/spring-cache-3.1.xsd
  http://www.springframework.org/schema/aop
  http://www.springframework.org/schema/aop/spring-aop.xsd
  http://www.springframework.org/schema/util
  http://www.springframework.org/schema/util/spring-util.xsd">
  
  <!-- 自動掃描web包,將帶有注解的類 納入spring容器管理 -->
  <!-- <context:component-scan base-package="com.niit.beans">
  </context:component-scan> -->
  <!-- 定義通知 -->
  <bean id="BeforeAdvice" class="com.niit.aop.BeforeAdvice"></bean>
   <bean id="AfterAdvice" class="com.niit.aop.AfterAdvice"></bean>
 
  <!-- 定義攔截器 -->
  <bean id="StudentIntercepter" class="com.niit.aop.StudentIntercepter"> </bean>
  <!-- 定義目標 -->
<bean id="target" class="com.niit.logic.Student"></bean>
<!-- 切入點 哪些方法會被aop影響 可選 -->
<bean id="pointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut">
<!-- 模式 -->
<property name="pattern" value=".*dohomework.*" >
</property>

</bean>
<!-- 通知器advisor 連接通知和切入點 可選-->
<bean id="advisor" class="org.springframework.aop.support.DefaultPointcutAdvisor">
<property name="advice" ref="BeforeAdvice"/>
<property name="pointcut" ref="pointcut"/>
</bean> 
<!-- 定義代理 -->
<bean id="student" class="org.springframework.aop.framework.ProxyFactoryBean">
<!-- 注入目標 -->
<property name="target" ref="target"></property>
<!-- 設置攔截器 -->
<property name="interceptorNames">
<list>
<value>BeforeAdvice</value>
<value>AfterAdvice</value>
<value>StudentIntercepter</value>
</list>
</property>
<!-- 定義代理接口 -->
<property name="proxyInterfaces" value="com.niit.logic.StudentIF"></property>

</bean>
</beans>

2.8SpringMVC.xml

<?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:context="http://www.springframework.org/schema/context"
  xmlns:mvc="http://www.springframework.org/schema/mvc"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/mvc
    http://www.springframework.org/schema/mvc/spring-mvc.xsd
    ">
    
    <!-- 包掃描:掃描注解所在的包controller類所在的包 -->
    <context:component-scan base-package="com.niit.controller"></context:component-scan>
     <context:component-scan base-package="com.niit.service" />
     <context:component-scan base-package="com.niit.dao" />
    <!-- 開啟注解驅動AnnotationHandlerMapping -->
    <mvc:annotation-driven/>
  
  <!-- 配置視圖解析器 -->
  <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/view/"/>
    <property name="suffix" value=".jsp"/>
  </bean>
  <!--SimpleMappingExceptionResolver(異常類與 View 的對應關系) -->
  <bean
    class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
    <!-- 定義默認的異常處理頁面,當該異常類型注冊時使用 -->
    <property name="defaultErrorView" value="error"></property>
    <!-- 定義異常處理頁面用來獲取異常信息的變量名,默認名為exception -->
    <property name="exceptionAttribute" value="ex"></property>
    <!-- 定義需要特殊處理的異常,用類名或完全路徑名作為key,異常頁名作為值 -->
    <property name="exceptionMappings">
      <props>
        <prop key="exception.MyException">my-error</prop>
        <prop key="java.sql.SQLException">sql-error</prop>
        <prop key="exception.KeyWordNotFoundException">my-error</prop>
        <!-- 在這里還可以繼續(xù)擴展對不同異常類型的處理 -->
      </props>
    </property>
  </bean>
   <!--托管MyExceptionHandler-->
  <!--<bean class="com.niit.exception.MyExceptionHandler"/> -->
</beans>

4效果圖

到此這篇關于SpringMVC用XML方式實現(xiàn)AOP的方法示例的文章就介紹到這了,更多相關SpringMVC XML實現(xiàn)AOP內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • 談談Java利用原始HttpURLConnection發(fā)送POST數(shù)據(jù)

    談談Java利用原始HttpURLConnection發(fā)送POST數(shù)據(jù)

    這篇文章主要給大家介紹java利用原始httpUrlConnection發(fā)送post數(shù)據(jù),設計到httpUrlConnection類的相關知識,感興趣的朋友跟著小編一起學習吧
    2015-10-10
  • 簡單了解Spring Bean常用注解的裝配

    簡單了解Spring Bean常用注解的裝配

    這篇文章主要介紹了簡單了解Spring Bean常用注解的裝配,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-11-11
  • java中哈希表及其應用詳解

    java中哈希表及其應用詳解

    Java中哈希表(Hashtable)是如何實現(xiàn)的呢?Hashtable中有一個內部類Entry,用來保存單元數(shù)據(jù),我們用來構建哈希表的每一個數(shù)據(jù)是Entry的一個實例。假設我們保存下面一組數(shù)據(jù),第一列作為key, 第二列作為value。
    2015-06-06
  • SpringBoot?Security的自定義異常處理

    SpringBoot?Security的自定義異常處理

    這篇文章主要介紹了SpringBoot?Security的自定義異常處理方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • IntelliJ IDEA將導入的項目轉成maven項目

    IntelliJ IDEA將導入的項目轉成maven項目

    這篇文章主要介紹了IntelliJ IDEA將導入的項目轉成maven項目,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-09-09
  • java獲取版本號及字節(jié)碼編譯版本方法示例

    java獲取版本號及字節(jié)碼編譯版本方法示例

    這篇文章主要給大家介紹了關于java獲得版本號及字節(jié)碼編譯版本的相關資料,文中通過示例代碼介紹的非常詳細,對大家學習或使用java具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧。
    2017-10-10
  • Java 手寫LRU緩存淘汰算法

    Java 手寫LRU緩存淘汰算法

    本文主要講了如何通過哈希鏈表這種數(shù)據(jù)結構來實現(xiàn)LRU算法,提供了三種實現(xiàn)思路,第一種從雙向鏈表開始,借助于HashMap來實現(xiàn)滿足要求的LRUCache
    2021-05-05
  • 詳解java裝飾模式(Decorator Pattern)

    詳解java裝飾模式(Decorator Pattern)

    這篇文章主要為大家詳細介紹了java裝飾模式Decorator Pattern,這種類型的設計模式屬于結構型模式,它是作為現(xiàn)有的類的一個包裝,對裝飾器模式感興趣的小伙伴們可以參考一下
    2016-04-04
  • SpringMvc+POI處理excel表數(shù)據(jù)導入

    SpringMvc+POI處理excel表數(shù)據(jù)導入

    這篇文章主要為大家詳細介紹了SpringMvc+POI處理excel表數(shù)據(jù)導入,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-06-06
  • 詳細講解springboot如何實現(xiàn)異步任務

    詳細講解springboot如何實現(xiàn)異步任務

    異步:異步與同步相對,當一個異步過程調用發(fā)出后,調用者在沒有得到結果之前,就可以繼續(xù)執(zhí)行后續(xù)操作。也就是說無論異步方法執(zhí)行代碼需要多長時間,跟主線程沒有任何影響,主線程可以繼續(xù)向下執(zhí)行
    2022-04-04

最新評論

江华| 滦平县| 衡阳市| 依兰县| 新野县| 奉新县| 吉林省| 开封市| 大田县| 搜索| 彝良县| 翼城县| 自治县| 阳新县| 邯郸县| 青岛市| 北川| 会同县| 福海县| 太和县| 阳春市| 奈曼旗| 石城县| 旬邑县| 邳州市| 焦作市| 营山县| 曲松县| 稷山县| 浦东新区| 惠州市| 临沧市| 青铜峡市| 仁怀市| 承德市| 南宫市| 航空| 祁连县| 阳城县| 固阳县| 叶城县|