导入jar包

Spring的学习运用_System

编写SomeService和SomeServiceImpl

Spring的学习运用_xml_02

SomeService.java

package cn.lexed.service;

public interface SomeService {
public abstract void doSome(String company, String founder);
	
	String doOtherr(String company, String founder);
	
	String doFirst(String company, String founder);
	
	void doSecond();
}

SomeServiceImpl.java

package cn.lexed.service.impl;

import org.springframework.stereotype.Service;

import cn.lexed.service.SomeService;
@Service("c")
public class SomeServiceImpl implements SomeService{

	@Override
	public void doSome(String company, String founder) {
		System.out.println("执行工作洽谈业务");
	}

	@Override
	public String doOtherr(String company, String founder) {
		System.out.println("执行核心业务代码");
		return "核心代码";
	}

	@Override
	public String doFirst(String company, String founder) {
        System.out.println("执行了核心业务代码doFirst...");
		return "doFirst";
	}

	@Override
	public void doSecond() {
		System.out.println("执行了业务代码"+(10/0));
	}

}

编写MyAspectJ

@Pointcut:定义和管理切入点,如果你项目里有多个切入点表达式重复的,可以这样使用

       value:切入点表达式

   特点:定义在方法上,此方法的方法名就是切入点表达式的别名

指定通知方法中的参数:JoinPoint

   作用:可以在通知方法中获取方法执行时的信息,例如方法名称方法的实参等

       如果说你需要用到方法里的信息,就加入JoinPoint

       这个JoinPoint参数的值由框架赋予的,必须是第一个位置的参数

@AfterReturning:后置通知

             value:属性值就是切入点表达式

             returning:自定义的变量,表示目标方法的返回值

                       自定义的变量和通知方法的形参名一致

   特点:1.在目标方法之后执行

             2.能够获取到目标方法的返回值,根据这个返回值做不同的处理

环绕通知方法的定义格式:

                  1.public

                  2.必须有一个返回值,推荐使用Object

                  3.方法名自定义

                  4.方法有参数,固定的参数 ProceedingJoinPoint

异常通知方法的定义格式:

                  1.public

                  2.没有返回值

                  3.方法名自定义

                  4.方法中有一个Exception

   @AfterThrowing:异常通知

        属性:value:

                           throwing:表示目标方法抛出的异常对象,名称和方法参数名一致

Spring的学习运用_System_03

MyAspectJ.java

package cn.lexed.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
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;

@Aspect
public class MyAspectJ {
	/* @Pointcut:定义和管理切入点,如果你项目里有多个切入点表达式重复的,可以这样使用
	 *     value:切入点表达式
	 * 特点:定义在方法上,此方法的方法名就是切入点表达式的别名
	 * */
	@Pointcut(value="execution(* cn.lexed.service..*(..))")
	public void mypt(){
		//无需代码
	}
	
	/* 指定通知方法中的参数:JoinPoint
	 * 作用:可以在通知方法中获取方法执行时的信息,例如方法名称方法的实参等
	 *     如果说你需要用到方法里的信息,就加入JoinPoint
	 *     这个JoinPoint参数的值由框架赋予的,必须是第一个位置的参数
	 * 
	 * */
	@Before(value="mypt()")
	public void MyBefore(JoinPoint jp){
		System.out.println(jp.getSignature());
		System.out.println(jp.getSignature().getName());
		//获取发放的实参
		Object[] args=jp.getArgs();
		for(Object arg: args){
			System.out.println("创始人="+arg);
			if(arg.equals("华为技术有限公司")){
				System.out.println();
			}
		}
		System.out.println("在洽谈业务之前进行上班打卡");
	}
	
	/*@AfterReturning:后置通知
	 *           value:属性值就是切入点表达式
	 *           returning:自定义的变量,表示目标方法的返回值
	 *                     自定义的变量和通知方法的形参名一致
	 * 特点:1.在目标方法之后执行
	 *     2.能够获取到目标方法的返回值,根据这个返回值做不同的处理
	 * 
	 */ 
	@AfterReturning(value="mypt()",returning="res")
	public void myAfterRe(JoinPoint jp,Object res){
		System.out.println(res);
		if(res.equals("核心代码")){
			//写一些功能
		}else{
			//写一些功能
		}
		System.out.println("在洽谈业务之后进行事务相关的处理");
	}
	
	/* 环绕通知方法的定义格式:
	 *                1.public
	 *                2.必须有一个返回值,推荐使用Object
	 *                3.方法名自定义
	 *                4.方法有参数,固定的参数 ProceedingJoinPoint
	 * 
	 * */
	@Around(value="mypt()")
	public Object myAround(ProceedingJoinPoint pjp) throws Throwable{
		Object o=null;
		System.out.println("在洽谈业务之前进行上班打卡");
		
		pjp.proceed();//doFirst
		
		System.out.println("在洽谈业务之后进行事务相关的处理");
		return o;
	}
	
	/* 异常通知方法的定义格式:
	 *                1.public
	 *                2.没有返回值
	 *                3.方法名自定义
	 *                4.方法中有一个Exception
	 * @AfterThrowing:异常通知
	 *      属性:value:
	 *                throwing:表示目标方法抛出的异常对象,名称和方法参数名一致
	 * */
	@AfterThrowing(value="mypt()",throwing="ex")
	public void myThrow(Exception ex){
		System.out.println("异常通知:方法发送异常时,执行:"+ex.getMessage());
		//发送邮件,短信,通知开发人员
	}
}

编写配置文件(点击Namespaces添加aop、context)

Spring的学习运用_System_04

app.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:context="http://www.springframework.org/schema/context"
	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-3.2.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">

<!-- 使用组件扫描器扫描 -->
<context:component-scan base-package="cn.lexed.service"></context:component-scan>
<!-- 声明切面类对象 -->
<bean class="cn.lexed.aop.MyAspectJ"></bean>
<!-- 配置AspectJ的代理机制 -->
<aop:aspectj-autoproxy/>

</beans>

编写测试类

Spring的学习运用_spring_05

Test.java

package cn.lexed.test;

import static org.junit.Assert.*;

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

import cn.lexed.service.SomeService;

public class Test {

	@org.junit.Test
	public void test() {
		//1.创建Spring容器的对象
		ApplicationContext ac=new ClassPathXmlApplicationContext("app.xml");
		SomeService pro=(SomeService)ac.getBean("c");
		pro.doSome("华为技术有限公司", "任正非");
	}
	@org.junit.Test
	public void test1() {
		//1.创建Spring容器的对象
		ApplicationContext ac=new ClassPathXmlApplicationContext("app.xml");
		SomeService pro=(SomeService)ac.getBean("c");
		pro.doOtherr("华为技术有限公司", "任正非");
	}
	@org.junit.Test
	public void test2() {
		//1.创建Spring容器的对象
		ApplicationContext ac=new ClassPathXmlApplicationContext("app.xml");
		SomeService pro=(SomeService)ac.getBean("c");
		pro.doFirst("华为技术有限公司", "任正非");
	}
	@org.junit.Test
	public void test3() {
		//1.创建Spring容器的对象
		ApplicationContext ac=new ClassPathXmlApplicationContext("app.xml");
		SomeService pro=(SomeService)ac.getBean("c");
		pro.doSecond();
	}
}
阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6
标签: Spring