回顾| 学习笔记

简介: 简介: 快速学习回顾

开发系列课程Java Web 开发系列课程:Spring 框架入门回顾学习笔记,与课程紧密联系,让用户快速学习知识。

课程地址:https://developer.aliyun.com/learning/course/538/detail/7348


回顾

 

内容简介:

一、领域模型

二、静态代理

三、动态代理

四、使用 spring 实现 aop

五、异常处理

 

一、领域模型

领域业务--领域模型; 支付业务--接入第三方接口---安全检查-进行身份验证一资金验证-支付

 

二、静态代理

UserServicelmpljav

package cn.sxt.service;

public class UserServiceImpl implements UserService {

@Override

public void add() {

//公共的业务----日志,安全,权限,缓存,事务等等

//A.log();-分离的思想--纸质阅读器;osgi--java 模块开发 -spring--osgi System.out.println("增加用户");}

@Override

public void update(){

public void add(

log("add");

userService.add();}

@Override

public void delete()

{

//日志,安全,缓存,事务,异常处理等 log("delete");

userService.delete();

@Override

public void search() {

log("searEh");

userService.search();

A public void update(){

log("update");

userService.update();

 

三、动态代理

public class ProxyInovationHandler implements

InvocationHandler{

private Object target;//目标对象--真实对象

public void setTarget(object target){

this.target = target;

}

/**

public object getproxyo(){

return

Proxy.newProxyinstance(this.hetClass(),getclassloader(),

target,getclass() getintertaces(),this);

/**

*Proxy是代理类

*method 代理类的调用处理程序的方法对象

**/

@overide

public object invoke(object proxy, method method,object [ ]

args)

throws Throwable(

log(method.getName());

Object result= method.invoke(target, args);

return result;

}

public void log(string methodtame){

throws Throwable

{ log(method.getName());

Object result=methodinvoke(target,args) ;

return result;

}

public void log(string methodName){

System.out.println("执行"+methodName+"方法

");

}

}

Chent.java 客户

public class client {

public static void main(string[ ] args){

Host host =new Host();

Proxy proxy=new Proxy(host);

proxy.rent();

}

}

 

 

四、使用 spring 实现aop

第一种实现方式,通过 springAPI 来实现。  Log-java --前置通知

代码如下:

public class Log implements MethodBeforeAdvice{

/**

*@param method 被调用方法对象

*@param args被调用的方法的参数

*@param target 被调用的方法的目标对象

@override

public void before(Method method, Object[] args,Object target)

throws Throwable {

System.out.println(target.getclass()getName()+""+method.getName()+"方法被执行");

}

}

AfterLog.java

public class AfterLog implements AfterReturningAdvice {

/*

*目标方法执行后执行的通知  returnValue --返回值

*method 被调用的方法对象

*args被调用的方法对象的参数

*target被调用的方法对象的目标对象

**/

@override

public void afterReturning(object returnValue,Method method,

Object[ ] args,object target)throws Throwable { System.out.println(target.getclass()getName()+"的

"+method.getName()+"被成功执行,返回值是:"+returnValue);

目标类

public class UserServiceImpl implements Userservice {

@override

public void add() {

Systen.out.println("增加用户");

}

@override

public void update(int a){

System.out.println("修改用户");

@Overr ide

public void delete(){

System.out.println("制除用户");

@override

public void search() {

Systemout.println(“查询用户”);

}

}

Spring的配置文件

<?xml version="1.0"encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns;xsi="http://wwy.w3.org/2001/XMLSchema-instance" xmlns.aop="http://www.springframework.org/schema/aop"

xsiischemalocation="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.xsd"><bean id="userService"

class="cn.sxt.service,impl.UserServiceImpl"/>

<bean id="log" class="cn.sxt.log.Log"/>

<bean id="afterlog" class="cn.sxt.log.Afterlog"/><aop:confi&>

<aop:pointsut expression="execution(*

cn.sxt.service.impl.*.*(..))" id="pointcut"/>

<aop:advisor advice-ref="log" pointcut-ref="pointcut"/><aop.advisor advice-ref="afterlog"

Rointcut-ref="pointcut"/>

</aop.confia>

</beans>

第二种方式实现aop:自定义类来实现

代码如下:

Log java

public class Log{

public void before(){

System.out.println("-----方法执行前---");

public void after(){

System.out.println("----方法执行后---“);

业务类不改变

配置文件

<bean id="userService

class="cn.sxt.service.impl.UserServiceImpl"/>

<bean id="log" class="cn.sxt.log.Loq"/><aop:config>

<aop:aspect ref="log">

<aop;pointcut expression="execution(

cn.sxt.service.impl.*.*(..))”id="pointcut"/>

<aop:before method="before

pointcut-ref="pointcut"/>

<aop:after method="after" pointcut-ref="pointcut"/></aop:aspect></aop:config>

第三种实现方法:通过注解来实现

Logjava

public class Log {

@Before("execution(* cn.sxt.service,impl.."(..))")

public void before(){

System.out.println("---方法执行前---");

}

@After(*execution(*cn.sxt.service.impl...(..))")

public void after()

System.out.println("方法执行后");

@Around("execution(* cn.sxt.service,impl.**(..))")

public obiect aroud(ProceedingjoinPoint ip) throws Throwablef

System.out.println("环绕前");

System.out.println(*签名:"+jp.getSignature());

//执行目标方法

Object result = jp.proceed();

System.out.println("环绕后");

return result;

}

}

配置文件

<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.xs

d

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop.xsd"

<bean id="userService"

class="cn.sxt.service.impl.UserServiceImpl"/>

<bean id="log" class="cn.sxt.log.Log"/>

<aop:aspectj-autoproxy/>

</beans>


五、异常通知

如果连接点抛出异常,异常通知 ( throws advice ) 将在连接点返回后被调用。 Spring 提供类型检查的异常通知 ( typed throws advice ),这意味着 org.springfranework.aop.ThrowsAdvice 接口不包含何方法:它只是一个标记接口用来标识所给对象实现了一个或者多个针对特定类型的异常通知方法。这些方法应当满足下面的格式:afterThrowing([Method,args,target],subclassOfThrowable )

只有最后一个参数是必须的。根据异常通知方法对方法及参数的需求,方法的签名可以有一个或者四个参数。下面是一些异常通知的例子。当一个 RemoteException (包括它的子类)被抛出时,下面的通知会被调用:

public class RemoteThrowsAdvice implements ThrowsAdvice{

public void afterThrowing(RemoteException ex) throws Throwable {

// Do something with remote exception

}

}

当一个  ServletException  被抽出,下面的通知将被调用。和上面的通知不同,它声明了4个参数,因此它可以访问被调用的方法,方法的参数。及目标对象:

public class ServletThrowsAdviceWithArguments implements ThrowsAdvice {

public void afterThrowing(Method m, Object[] args, Object target, ServletException ex)(

/ /Do something with all arguments

}

}

相关文章
|
机器学习/深度学习 分布式计算 DataWorks
EasyRec 使用介绍|学习笔记
快速学习 EasyRec 使用介绍。
906 0
|
SQL 开发框架 JSON
honeycomb使用|学习笔记
快速学习honeycomb使用
511 0
honeycomb使用|学习笔记
|
存储 缓存 固态存储
Fatcache学习笔记
Fatcache学习笔记
Fatcache学习笔记
|
存储 SQL Java
JAP4|学习笔记
快速学习JAP4
161 0
JAP4|学习笔记
|
XML SQL 机器学习/深度学习
M-PEG7 | 学习笔记
快速学习 M-PEG7,介绍了 M-PEG7系统机制, 以及在实际应用过程中如何使用。
88 0
M-PEG7 | 学习笔记
|
算法 Scala 开发者
拉链的介绍和使用 | 学习笔记
快速学习拉链的介绍和使用
83 0
|
物联网 Linux 开发者
Mycp|学习笔记
快速学习 Mycp
82 0
Mycp|学习笔记
|
算法 开发者 索引
FuzzyQuery|学习笔记
快速学习 FuzzyQuery。
186 0
|
XML 设计模式 JavaScript
回顾 | 学习笔记
快速学习回顾,介绍了回顾系统机制, 以及在实际应用过程中如何使用。
117 0
|
存储 开发者 Python
练习4|学习笔记
快速学习练习4
79 0