當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring 教程03
生活随笔
收集整理的這篇文章主要介紹了
Spring 教程03
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
spring-3
1. Xml<!-- \src\applicationContext-xml.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"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd"><!-- 配置 bean --><bean id="arithmeticCalculator" class="com.atguigu.spring.aop.xml.ArithmeticCalculatorImpl"></bean><!-- 配置切面的 bean. --><bean id="loggingAspect"class="com.atguigu.spring.aop.xml.LoggingAspect"></bean><bean id="vlidationAspect"class="com.atguigu.spring.aop.xml.VlidationAspect"></bean><!-- 配置 AOP --><aop:config><!-- 配置切點表達式 --><aop:pointcut expression="execution(* com.atguigu.spring.aop.xml.ArithmeticCalculator.*(int, int))" id="pointcut"/><!-- 配置切面及通知 --><aop:aspect ref="loggingAspect" order="2"><aop:before method="beforeMethod" pointcut-ref="pointcut"/><aop:after method="afterMethod" pointcut-ref="pointcut"/><aop:after-throwing method="afterThrowing" pointcut-ref="pointcut" throwing="e"/><aop:after-returning method="afterReturning" pointcut-ref="pointcut" returning="result"/><!-- <aop:around method="aroundMethod" pointcut-ref="pointcut"/>--></aop:aspect> <aop:aspect ref="vlidationAspect" order="1"><aop:before method="validateArgs" pointcut-ref="pointcut"/></aop:aspect></aop:config></beans><!-- \src\applicationContext.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:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"><!-- 配置自動掃描的包 --><context:component-scan base-package="com.atguigu.spring.aop"></context:component-scan><!-- 配置自動為匹配 aspectJ 注解的 Java 類生成代理對象 --><aop:aspectj-autoproxy></aop:aspectj-autoproxy></beans> 2. Java// \src\com\atguigu\spring\aop\ArithmeticCalculator.java
package com.atguigu.spring.aop;public interface ArithmeticCalculator {int add(int i, int j);int sub(int i, int j);int mul(int i, int j);int div(int i, int j);}// \src\com\atguigu\spring\aop\ArithmeticCalculatorImpl.java
package com.atguigu.spring.aop;import org.springframework.stereotype.Component;@Component("arithmeticCalculator")
public class ArithmeticCalculatorImpl implements ArithmeticCalculator {@Overridepublic int add(int i, int j) {int result = i + j;return result;}@Overridepublic int sub(int i, int j) {int result = i - j;return result;}@Overridepublic int mul(int i, int j) {int result = i * j;return result;}@Overridepublic int div(int i, int j) {int result = i / j;return result;}}// \src\com\atguigu\spring\aop\LoggingAspect.java
package com.atguigu.spring.aop;import java.util.Arrays;import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
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;/*** 可以使用 @Order 注解指定切面的優先級, 值越小優先級越高*/
@Order(2)
@Aspect
@Component
public class LoggingAspect {/*** 定義一個方法, 用于聲明切入點表達式. 一般地, 該方法中再不需要添入其他的代碼. * 使用 @Pointcut 來聲明切入點表達式. * 后面的其他通知直接使用方法名來引用當前的切入點表達式. */@Pointcut("execution(public int com.atguigu.spring.aop.ArithmeticCalculator.*(..))")public void declareJointPointExpression(){}/*** 在 com.atguigu.spring.aop.ArithmeticCalculator 接口的每一個實現類的每一個方法開始之前執行一段代碼*/@Before("declareJointPointExpression()")public void beforeMethod(JoinPoint joinPoint){String methodName = joinPoint.getSignature().getName();Object [] args = joinPoint.getArgs();System.out.println("The method " + methodName + " begins with " + Arrays.asList(args));}/*** 在方法執行之后執行的代碼. 無論該方法是否出現異常*/@After("declareJointPointExpression()")public void afterMethod(JoinPoint joinPoint){String methodName = joinPoint.getSignature().getName();System.out.println("The method " + methodName + " ends");}/*** 在方法法正常結束受執行的代碼* 返回通知是可以訪問到方法的返回值的!*/@AfterReturning(value="declareJointPointExpression()",returning="result")public void afterReturning(JoinPoint joinPoint, Object result){String methodName = joinPoint.getSignature().getName();System.out.println("The method " + methodName + " ends with " + result);}/*** 在目標方法出現異常時會執行的代碼.* 可以訪問到異常對象; 且可以指定在出現特定異常時在執行通知代碼*/@AfterThrowing(value="declareJointPointExpression()",throwing="e")public void afterThrowing(JoinPoint joinPoint, Exception e){String methodName = joinPoint.getSignature().getName();System.out.println("The method " + methodName + " occurs excetion:" + e);}/*** 環繞通知需要攜帶 ProceedingJoinPoint 類型的參數. * 環繞通知類似于動態代理的全過程: ProceedingJoinPoint 類型的參數可以決定是否執行目標方法.* 且環繞通知必須有返回值, 返回值即為目標方法的返回值*//*@Around("execution(public int com.atguigu.spring.aop.ArithmeticCalculator.*(..))")public Object aroundMethod(ProceedingJoinPoint pjd){Object result = null;String methodName = pjd.getSignature().getName();try {//前置通知System.out.println("The method " + methodName + " begins with " + Arrays.asList(pjd.getArgs()));//執行目標方法result = pjd.proceed();//返回通知System.out.println("The method " + methodName + " ends with " + result);} catch (Throwable e) {//異常通知System.out.println("The method " + methodName + " occurs exception:" + e);throw new RuntimeException(e);}//后置通知System.out.println("The method " + methodName + " ends");return result;}*/
}// \src\com\atguigu\spring\aop\Main.java
package com.atguigu.spring.aop;import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class Main {public static void main(String[] args) {ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");ArithmeticCalculator arithmeticCalculator = (ArithmeticCalculator) ctx.getBean("arithmeticCalculator");System.out.println(arithmeticCalculator.getClass().getName());int result = arithmeticCalculator.add(1, 2);System.out.println("result:" + result);result = arithmeticCalculator.div(1000, 10);System.out.println("result:" + result);}}// \src\com\atguigu\spring\aop\VlidationAspect.java
package com.atguigu.spring.aop;import java.util.Arrays;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;@Order(1)
@Aspect
@Component
public class VlidationAspect {@Before("com.atguigu.spring.aop.LoggingAspect.declareJointPointExpression()")public void validateArgs(JoinPoint joinPoint){System.out.println("-->validate:" + Arrays.asList(joinPoint.getArgs()));}}// \src\com\atguigu\spring\aop\xml\ArithmeticCalculator.java
package com.atguigu.spring.aop.xml;public interface ArithmeticCalculator {int add(int i, int j);int sub(int i, int j);int mul(int i, int j);int div(int i, int j);}// \src\com\atguigu\spring\aop\xml\ArithmeticCalculatorImpl.java
package com.atguigu.spring.aop.xml;public class ArithmeticCalculatorImpl implements ArithmeticCalculator {@Overridepublic int add(int i, int j) {int result = i + j;return result;}@Overridepublic int sub(int i, int j) {int result = i - j;return result;}@Overridepublic int mul(int i, int j) {int result = i * j;return result;}@Overridepublic int div(int i, int j) {int result = i / j;return result;}}// \src\com\atguigu\spring\aop\xml\LoggingAspect.java
package com.atguigu.spring.aop.xml;import java.util.Arrays;import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;public class LoggingAspect {public void beforeMethod(JoinPoint joinPoint){String methodName = joinPoint.getSignature().getName();Object [] args = joinPoint.getArgs();System.out.println("The method " + methodName + " begins with " + Arrays.asList(args));}public void afterMethod(JoinPoint joinPoint){String methodName = joinPoint.getSignature().getName();System.out.println("The method " + methodName + " ends");}public void afterReturning(JoinPoint joinPoint, Object result){String methodName = joinPoint.getSignature().getName();System.out.println("The method " + methodName + " ends with " + result);}public void afterThrowing(JoinPoint joinPoint, Exception e){String methodName = joinPoint.getSignature().getName();System.out.println("The method " + methodName + " occurs excetion:" + e);}@Around("execution(public int com.atguigu.spring.aop.ArithmeticCalculator.*(..))")public Object aroundMethod(ProceedingJoinPoint pjd){Object result = null;String methodName = pjd.getSignature().getName();try {//前置通知System.out.println("The method " + methodName + " begins with " + Arrays.asList(pjd.getArgs()));//執行目標方法result = pjd.proceed();//返回通知System.out.println("The method " + methodName + " ends with " + result);} catch (Throwable e) {//異常通知System.out.println("The method " + methodName + " occurs exception:" + e);throw new RuntimeException(e);}//后置通知System.out.println("The method " + methodName + " ends");return result;}
}// \src\com\atguigu\spring\aop\xml\Main.java
package com.atguigu.spring.aop.xml;import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class Main {public static void main(String[] args) {ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext-xml.xml");ArithmeticCalculator arithmeticCalculator = (ArithmeticCalculator) ctx.getBean("arithmeticCalculator");System.out.println(arithmeticCalculator.getClass().getName());int result = arithmeticCalculator.add(1, 2);System.out.println("result:" + result);result = arithmeticCalculator.div(1000, 0);System.out.println("result:" + result);}}// \src\com\atguigu\spring\aop\xml\VlidationAspect.java
package com.atguigu.spring.aop.xml;import java.util.Arrays;import org.aspectj.lang.JoinPoint;public class VlidationAspect {public void validateArgs(JoinPoint joinPoint){System.out.println("-->validate:" + Arrays.asList(joinPoint.getArgs()));}}
?
轉載于:https://www.cnblogs.com/c0liu/p/7469189.html
總結
以上是生活随笔為你收集整理的Spring 教程03的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: U盘基本处理,U盘与移动固态硬盘
- 下一篇: 26岁小伙口渴难耐吃半个西瓜险丧命!为何