欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

1.首先創(chuàng)建web工程,之后導(dǎo)入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("后置攔截:下課之后寫(xiě)作業(yè)");
	}
}

2.2BeforeAdvice

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

	}
}

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("中間攔截:你是學(xué)生");
			}
			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的目標(biāo)方法
public void havingClass(String name) {
	System.out.println("aop的目標(biāo)方法");
	System.out.println(name+"正在上課");
}
public void dohomework(String name) {
	System.out.println(name+"正在寫(xiě)作業(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">
  
  <!-- 自動(dòng)掃描web包,將帶有注解的類(lèi) 納入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>
  <!-- 定義目標(biāo) -->
<bean id="target" class="com.niit.logic.Student"></bean>
<!-- 切入點(diǎn) 哪些方法會(huì)被aop影響 可選 -->
<bean id="pointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut">
<!-- 模式 -->
<property name="pattern" value=".*dohomework.*" >
</property>

</bean>
<!-- 通知器advisor 連接通知和切入點(diǎn) 可選-->
<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">
<!-- 注入目標(biāo) -->
<property name="target" ref="target"></property>
<!-- 設(shè)置攔截器 -->
<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
    ">
    
    <!-- 包掃描:掃描注解所在的包c(diǎn)ontroller類(lèi)所在的包 -->
    <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" />
    <!-- 開(kāi)啟注解驅(qū)動(dòng)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(異常類(lèi)與 View 的對(duì)應(yīng)關(guān)系) -->
  <bean
    class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
    <!-- 定義默認(rèn)的異常處理頁(yè)面,當(dāng)該異常類(lèi)型注冊(cè)時(shí)使用 -->
    <property name="defaultErrorView" value="error"></property>
    <!-- 定義異常處理頁(yè)面用來(lái)獲取異常信息的變量名,默認(rèn)名為exception -->
    <property name="exceptionAttribute" value="ex"></property>
    <!-- 定義需要特殊處理的異常,用類(lèi)名或完全路徑名作為key,異常頁(yè)名作為值 -->
    <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ù)擴(kuò)展對(duì)不同異常類(lèi)型的處理 -->
      </props>
    </property>
  </bean>
   <!--托管MyExceptionHandler-->
  <!--<bean class="com.niit.exception.MyExceptionHandler"/> -->
</beans>

4效果圖

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

相關(guān)文章

  • java圖論弗洛伊德和迪杰斯特拉算法解決最短路徑問(wèn)題

    java圖論弗洛伊德和迪杰斯特拉算法解決最短路徑問(wèn)題

    這篇文章主要為大家介紹了java圖論弗洛伊德算法和迪杰斯特拉算法解決最短路徑的問(wèn)題示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助
    2021-11-11
  • Spring事務(wù)的失效場(chǎng)景你知道多少

    Spring事務(wù)的失效場(chǎng)景你知道多少

    這篇文章主要為大家詳細(xì)介紹了Spring事務(wù)的失效場(chǎng)景,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助
    2022-03-03
  • 詳談Java中的Object、T(泛型)、?區(qū)別

    詳談Java中的Object、T(泛型)、?區(qū)別

    下面小編就為大家?guī)?lái)一篇詳談Java中的Object、T(泛型)、?區(qū)別。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-06-06
  • IntelliJ IDEA 常用設(shè)置(配置)吐血整理(首次安裝必需)

    IntelliJ IDEA 常用設(shè)置(配置)吐血整理(首次安裝必需)

    這篇文章主要介紹了IntelliJ IDEA 常用設(shè)置(配置)吐血整理(首次安裝必需),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-06-06
  • SpringBoot異步實(shí)現(xiàn) 的8種方式

    SpringBoot異步實(shí)現(xiàn) 的8種方式

    在同步操作中,執(zhí)行到?發(fā)送短信?的時(shí)候,我們必須等待這個(gè)方法徹底執(zhí)行完才能執(zhí)行?贈(zèng)送積分?這個(gè)操作,如果?贈(zèng)送積分?這個(gè)動(dòng)作執(zhí)行時(shí)間較長(zhǎng),發(fā)送短信需要等待,這就是典型的同步場(chǎng)景,這篇文章主要介紹了SpringBoot異步實(shí)現(xiàn) 的8種方式,需要的朋友可以參考下
    2023-11-11
  • SpringBoot下使用定時(shí)任務(wù)的方式全揭秘(6種)

    SpringBoot下使用定時(shí)任務(wù)的方式全揭秘(6種)

    這篇文章主要介紹了SpringBoot下使用定時(shí)任務(wù)的方式全揭秘(6種),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2019-02-02
  • JDBC 程序的常見(jiàn)錯(cuò)誤及調(diào)試方法

    JDBC 程序的常見(jiàn)錯(cuò)誤及調(diào)試方法

    本文是《Java Web開(kāi)發(fā)教程——入門(mén)與提高篇(JSP+Servlet)》一書(shū)《第9章 JDBC技術(shù)》的補(bǔ)充內(nèi)容。
    2009-06-06
  • 關(guān)于線(xiàn)程池異步線(xiàn)程中再次獲取線(xiàn)程池資源的問(wèn)題

    關(guān)于線(xiàn)程池異步線(xiàn)程中再次獲取線(xiàn)程池資源的問(wèn)題

    這篇文章主要介紹了關(guān)于線(xiàn)程池異步線(xiàn)程中再次獲取線(xiàn)程池資源的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-08-08
  • InputStreamReader 和FileReader的區(qū)別及InputStream和Reader的區(qū)別

    InputStreamReader 和FileReader的區(qū)別及InputStream和Reader的區(qū)別

    這篇文章主要介紹了InputStreamReader 和FileReader的區(qū)別及InputStream和Reader的區(qū)別的相關(guān)資料,需要的朋友可以參考下
    2015-12-12
  • 一篇文章帶你入門(mén)Java基本概念

    一篇文章帶你入門(mén)Java基本概念

    本文主要介紹了Java編程的基本概念基本概念,可以幫助我們更加深刻的所要講解的Java命令,具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧,希望能給你帶來(lái)幫助
    2021-08-08

最新評(píng)論