国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

spring學(xué)習(xí)隨筆5

2019-11-08 02:50:27
字體:
供稿:網(wǎng)友

本文介紹aop的基于配置文件的方式

aopconfig.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:aop="http://www.springframework.org/schema/aop"	xmlns:p="http://www.springframework.org/schema/p"	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">		<!-- 配置bean -->	<bean id="calculator" class="com.liu.aopconfig.Calculator"></bean>	<bean id="calculatoraspect" class="com.liu.aopconfig.CalculatorAspect"></bean>	<bean id="validateAspect" class="com.liu.aopconfig.ValidateAspect"></bean>		<!-- 配置切面 -->	<aop:config>		<aop:aspect id="aspect2" ref="calculatorAspect" order="2">			<aop:pointcut expression="execution(public int com.liu.aopconfig.ICalculator.add(.. ))" id="pointcut1"/>			<aop:pointcut expression="execution(public int com.liu.aopconfig.ICalculator.div(.. ))" id="pointcut2"/>			<aop:pointcut expression="execution(public int com.liu.aopconfig.ICalculator.minus(.. ))" id="pointcut3"/>			<aop:before method="beforeMethod" pointcut-ref="pointcut1"/>			<aop:after method="afterMethod" pointcut-ref="pointcut1"/>			<aop:after-returning method="afterReturn" pointcut-ref="pointcut1" returning="result"/>			<aop:after-throwing method="afterThrow" pointcut-ref="pointcut2" throwing="e"/>			<aop:around method="aroundMethod" pointcut-ref="pointcut3"/>		</aop:aspect>		<aop:aspect id="aspect1" ref="validateAspect" order="1">			<aop:before method="beforeMethod" pointcut-ref="pointcut1"/>		</aop:aspect>			</aop:config></beans>

ICalculator.java

package com.liu.aopconfig;public interface ICalculator {	public int add(int i,int j);	public int minus(int i,int j);	public int div(int i,int j);}

Calculator.java

package com.liu.aopconfig;import org.springframework.stereotype.Component;public class Calculator implements ICalculator {	@Override	public int add(int i, int j) {		return i + j;	}	@Override	public int minus(int i, int j) {		return i - j;	}	@Override	public int div(int i, int j) {		return i/j;	}}

CalculatorAspect.java

package com.liu.aopconfig;import java.util.Arrays;import java.util.List;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.After;import org.aspectj.lang.annotation.AfterReturning;import org.aspectj.lang.annotation.AfterThrowing;import org.aspectj.lang.annotation.Around;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;import org.aspectj.lang.annotation.Pointcut;import org.springframework.core.annotation.Order;import org.springframework.stereotype.Component;public class CalculatorAspect {	public void declareAddMethod(){}		public void beforeMethod(JoinPoint joinPoint){		String methodName = joinPoint.getSignature().getName();		List<Object> args = Arrays.asList(joinPoint.getArgs());		System.out.println("The method "+ methodName+ " begin with"+args);	}		public void afterMethod(JoinPoint joinPoint){		String methodName = joinPoint.getSignature().getName();		System.out.println("The method "+ methodName+ " end ");	}		public void afterReturn(JoinPoint joinPoint,Object result){		String methodName = joinPoint.getSignature().getName();		System.out.println("The method "+ methodName+ " return with " + result);			}		public void afterThrow(JoinPoint joinPoint,Exception e){		String methodName = joinPoint.getSignature().getName();		System.out.println("The method "+ methodName+ " occurs with " + e);			}		public Object aroundMethod(ProceedingJoinPoint pjoinPoint){		Object result = null;		String methodName = pjoinPoint.getSignature().getName();		List<Object> args = Arrays.asList(pjoinPoint.getArgs());		System.out.println("The method "+ methodName+ " begin with"+args);		try {			result = pjoinPoint.proceed();		} catch (Throwable e) {			// TODO Auto-generated catch block			e.printStackTrace();		}		System.out.println("The method "+ methodName+ " return with "+result);		return result;	}}

ValidateAspect.java

package com.liu.aopconfig;import java.util.Arrays;import java.util.List;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;import org.springframework.core.annotation.Order;import org.springframework.stereotype.Component;public class ValidateAspect {	public void beforeMethod(JoinPoint joinPoint){		System.out.println("The method validate");	}}

Test.java

package com.liu.aopconfig;import org.springframework.context.applicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Test {	public static void main(String[] args) {		ApplicationContext cxt = new ClassPathXmlApplicationContext("aopconfig.xml");				ICalculator calculator = (ICalculator)cxt.getBean("calculator");		calculator.add(1, 3);				calculator.minus(1, 3);				calculator.div(1, 0);			}}

 

 


上一篇:STL 棧stack的使用

下一篇:vagrant的安裝

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 班玛县| 厦门市| 东宁县| 西安市| 湖北省| 通辽市| 黄龙县| 江都市| 什邡市| 二连浩特市| 安达市| 芷江| 成安县| 阜城县| 土默特右旗| 漳州市| 芜湖县| 汾西县| 瓮安县| 平邑县| 内黄县| 顺平县| 黔江区| 香港 | 顺昌县| 邵阳市| 加查县| 古田县| 芜湖县| 广南县| 滦平县| 凌海市| 分宜县| 景谷| 临汾市| 东台市| 北碚区| 洮南市| 宜宾市| 宁德市| 垣曲县|