一、回顾

1.1 依赖注入的方式。

  1. set方法来注入 <property name=”属性名” />
  2. 构造方法来注入<construtor-arg index=”” />

1.2 依赖注入的数据类型。

  1. 基本类型和字符串 value
  2. 对象类型  ref       <bean></bean>
  3. 集合List set
  4. map类型 <entry key= value=>
  5. array类型

1.3 引入属性文件.<context: property-placeholder     location=”classpath:*.properties”>

1.4 自动注入。 autowire=”byName | byType | default | no ”

1.5 bean的作用域  scope=”singleton | prototype”

1.6 注解. @Repository (持久化) @Service (业务层) @Controller (控制层) @Component (组件) ,这四个功能都是一样的。

@Autowired (自动注入 先按照类型注入,再按照名称注入)  @Resource (自动注入 按照名称先注入,再按照类型注入。如果没有起名,那么它的名称就是属性的名称。)

二、AOP面向切面编程

Aop的前提:1.代理 2.AOP

1.动态代理模式

代理设计模式的原理: 使用一个代理将对象包装起来, 然后用该代理对象取代原始对象. 任何对原始对象的调用都要通过代理. 代理对象决定是否以及何时将方法调用转到原始对象上.

示例:

创建接口ArithmeticCalculate

public interface ArithmeticCalculate {

    /**
* 加法
* @param a
* @param b
* @return
*/
public double add(double a,double b);
/**
* 减法
* @param a
* @param b
* @return
*/
public double sub(double a,double b);
/**
* 乘法
* @param a
* @param b
* @return
*/
public double mul(double a,double b);
/**
* 除法
* @param a
* @param b
* @return
*/
public double div(double a,double b);
}

创建接口的实现类

package com.zhiyou100.ykq.aop.proxy;
//1.使用代理(了解) 2.使用spring的aop。
public class ArithmeticCalculateImp implements ArithmeticCalculate {
//在每个方法中加入日志信息。
@Override
public double add(double a, double b) {
double result=a+b;
System.out.println("result:"+result);
return result;
} @Override
public double sub(double a, double b) {
double result=a-b;
System.out.println("result:"+result);
return result;
} @Override
public double mul(double a, double b) {
double result=a*b;
System.out.println("result:"+result);
return result;
} @Override
public double div(double a, double b) {
double result=a/b;
System.out.println("result:"+result);
return result;
} }

创建代理类并实现接口来代理需要被代理的对象

//调用处理程序:InvocationHandler
public class ArithmeticCalculateLogProxy implements InvocationHandler{
//被代理的对象 明星
private Object target; public ArithmeticCalculateLogProxy(Object target) {
this.target = target;
} /**
* mothod: 正在被调用的方法
* args: 正在执行的方法所需要的参数
*/
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
String methodName=method.getName();
System.out.println("the method "+methodName+" begin with "+Arrays.asList(args));
//method:方法
//target:目标对象
Object result=method.invoke(target, args);//回调
System.out.println("the method add end result:"+result);
return result;
} //得到代理对象。 经纪人
public Object getProxy() {
//loader: 得到代理对象的类加载器。
//interfaces: 代理对象 要代理的方法由哪些。
//h: 当执行这些方法时,会调用该类中invoke方法
return Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), this);
} }

测试

public class Test {
public static void main(String[] args) {
ArithmeticCalculate target=new ArithmeticCalculateImp();//创建的王宝强 ArithmeticCalculateLogProxy proxy=new ArithmeticCalculateLogProxy(target); ArithmeticCalculate p=(ArithmeticCalculate) proxy.getProxy(); //宋喆 p.sub(15, 25);//1.首先执行invoke方法 }
}

2.AOP模式

2.1使用注解来完成

加入依赖的jar包文件

创建一个接口ArithmeticCalculate

public interface ArithmeticCalculate {

    // 加法
public double add(double a,double b);
// 减法
public double sub(double a,double b);
// 乘法
public double mul(double a,double b);
// 除法
public double div(double a,double b);
}

创建实现以上接口的类

@Component
public class ArithmeticCalculateImp implements ArithmeticCalculate {
//在每个方法中加入日志信息。
@Override
public double add(double a, double b) {
double result=a+b;
System.out.println("result:"+result);
return result;
} @Override
public double sub(double a, double b) {
double result=a-b;
System.out.println("result:"+result);
return result;
} @Override
public double mul(double a, double b) {
double result=a*b;
System.out.println("result:"+result);
return result;
} @Override
public double div(double a, double b) {
if(b==2) {
throw new RuntimeException("出错了");
}
double result=a/b;
System.out.println("result:"+result);
return result;
} }

创建一个切面类

//切面类
@Aspect
@Component
public class LogAspect {
//*:通配的是访问修饰符
//..:通配的是方法的参数,一般三个及以上
//连接点(joinpoint)
//Spring中连接点指的就是被拦截到的方法,实际上连接点还可以是字段或者构造器
//前置通知
@Before(value="execution(* com.zhiyou100.xz.aspectj.*.*(..))")// 第一个*代表类,第二个*代表类中的方法
public void aa(JoinPoint joinPoint) { //在ArithmeticCalculateImp中add方法前执行
Object[] args=joinPoint.getArgs();
String name=joinPoint.getSignature().getName();
System.out.println("zhiyou-->the method "+name+" begin with"+Arrays.asList(args));
} //@AfterReturning
//后置通知
@After(value="execution(* com.zhiyou100.xz.aspectj.*.*(..))")
public void bb(JoinPoint joinPoint) {
String name=joinPoint.getSignature().getName();
//joinPoint.getTarget();
System.out.println("zhiyou-->the method "+name+"end result:");
} //返回通知
@AfterReturning(value="execution(* com.zhiyou100.xz.aspectj.*.*(..))",returning="result")
public void cc(Object result) {
System.out.println("======"+result);
} //异常通知
@AfterThrowing(value="execution(* com.zhiyou100.xz.aspectj.*.*(..))",throwing="e")
public void dd(Exception e) {
System.out.println("异常了:"+e.getMessage());
}
}

在spring的配置文件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/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd "> <!-- 包扫描 -->
<context:component-scan base-package="com.zhiyou100.xz.aspectj"></context:component-scan> <!-- 开启切面注解 -->
<aop:aspectj-autoproxy />
</beans>

测试

public class Test {
public static void main(String[] args) {
ApplicationContext app=new ClassPathXmlApplicationContext("app.xml");
ArithmeticCalculate a=(ArithmeticCalculate) app.getBean("arithmeticCalculateImp");
a.div(10, 5);
}
}

2.2使用xml的方式来完成

把第一种方式中的接口、接口的实现类以及切面类中的注解全部删除,然后新建spring的配置文件app2.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/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd ">
<!-- 定义被通知的程序类 -->
<bean id="ari" class="com.zhiyou100.xz.aspectj.xml.ArithmeticCalculateImp"></bean> <!-- 定义切面类的bean -->
<bean id="logAspect" class="com.zhiyou100.xz.aspectj.xml.LogAspect"></bean> <!-- 配置切面 -->
<aop:config>
<!-- 定义表达式切点 -->
<aop:pointcut expression="execution(* com.zhiyou100.xz.aspectj.xml.*.*(..))" id="pointcut"/>
<!-- 定义切面 -->
<aop:aspect ref="logAspect">
<!-- 定义前置通知 -->
<aop:before method="aa" pointcut-ref="pointcut"/>
<aop:after method="bb" pointcut-ref="pointcut"/>
<aop:after-returning method="cc" pointcut-ref="pointcut" returning="result"/>
<aop:after-throwing method="dd" pointcut-ref="pointcut" throwing="e"/>
</aop:aspect>
</aop:config> </beans>

测试

public class Test2 {
public static void main(String[] args) {
ApplicationContext app=new ClassPathXmlApplicationContext("app2.xml"); ArithmeticCalculate a=(ArithmeticCalculate) app.getBean("ari");
a.div(10, 5); }
}

spring学习日志三的更多相关文章

  1. spring 学习(三):aop 学习

    spring 学习(三):aop 学习 aop 概念 1 aop:面向切面(方面)编程,扩展功能不修改源代码实现 2 AOP采取横向抽取机制,取代了传统纵向继承体系重复性代码 3 aop底层使用动态代 ...

  2. MyEclipse Spring 学习总结三 SpringMVC

    MyEclipse Spring 学习总结三 SpringMVC 一.SpringMVC原理 1.Springmvc 框架介绍 1)Spring 框架停工了构建Web应用程序的全功能MVC模块.Spr ...

  3. Spring学习(三)——Spring中的依赖注入的方式

    [前面的话] Spring对我太重要了,做个关于web相关的项目都要使用Spring,每次去看Spring相关的知识,总是感觉一知半解,没有很好的系统去学习一下,现在抽点时间学习一下Spring.不知 ...

  4. Spring学习总结三——SpringIOC容器三

    一:spring容器自动装配注入 为了减少xml中配置内容,可以使用自动装配注入,代替setter注入,只需要在 bean对象配置中添加属性autoWire即可,那么在类中就会自动扫描setXXX() ...

  5. Spring学习(三)--Spring的IOC

    1.BeanFactory和FactoryBean BeanFactory是一个接口类,定义了IOC容器最基本的形式,提供了IOC容器所应该遵守的基本服务契约. FactoryBean是一个能产生或者 ...

  6. Spring学习系列(三) 通过Java代码装配Bean

    上面梳理了通过注解来隐式的完成了组件的扫描和自动装配,下面来学习下如何通过显式的配置的装配bean 二.通过Java类装配bean 在前面定义了HelloWorldConfig类,并使用@Compon ...

  7. Spring 学习(三)AOP

    (1)AOP概述 - AOP:面向切面编程,扩展功能不修改源代码实现 - AOP采取横向抽取机制,取代了传统的纵向继承体系重复性代码 (2)AOP底层原理 原始方法------->纵向继承体系 ...

  8. <黑马新秀>Spring学习日志

    # 用于梳理Spring知识点 Spring是分层的Java EE应用全栈轻量级开源框架,以IoC(Inverse Of Control反转控制)和AOP(Aspect Oriented Progra ...

  9. 【Java EE 学习 51】【Spring学习第三天】【cglib动态代理】【AOP和动态代理】【切入点表达式】

    一.cglib动态代理 1.简介 (1)CGlib是一个强大的,高性能,高质量的Code生成类库.它可以在运行期扩展Java类与实现Java接口. (2) 用CGlib生成代理类是目标类的子类. (3 ...

随机推荐

  1. C语言:最小公倍数

    //求最小公倍数 #include <stdio.h> main() { int m,n,i,k,max,min; scanf("%d,%d",&m,& ...

  2. C语言:按相反顺序输出字符

    #include <stdio.h> void pailie(int n) { char next; if (n<=1) { next=getchar(); putchar(next ...

  3. C语言:if条件写法

    if 语句的判断条件中不是必须要包含关系运算符,它可以是赋值表达式,甚至也可以是一个变量,常量 例如: //情况① if(b){ //TODO: } //情况② if(b=5){ //情况① //TO ...

  4. vuejs第一集之:vuejs了解

    1,了解到前后端分离2,连接到vuejs3,搜集书籍: Vuejs前端开发基础与项目实战 (https://detail.tmall.com/item.htm?spm=a230r.1.14.107.6 ...

  5. iOS 15 Beta升级卡死在更新进程,无法启动怎么办?

    2021苹果全球开发者大会结束后,大批果粉迫不及待的尝试升级iOS 15测试版本,想第一时间体验新功能. 但是许多用户反馈升级一直卡死在"准备更新"."验证更新" ...

  6. Java 反射(一)反射简介、原理和应用场景

    目录 一.动态语言和动态语言的比较 动态语言 静态语言 二.反射 简介 反射的常见使用 1. 代码编辑器 2. Spring等框架的IoC容器 3. 和注解的配合使用 原理 反射优缺点 调试查看 Cl ...

  7. Mybatis学习笔记导航

    Mybatis小白快速入门 简介 本人是一个Java学习者,最近才开始在博客园上分享自己的学习经验,同时帮助那些想要学习的uu们,相关学习视频在小破站的狂神说,狂神真的是我学习到现在觉得最GAN的老师 ...

  8. ssrf漏洞随笔

    一.ssrf漏洞定义 SSRF漏洞:SSRF是一 种由攻击者构造请求,由服务端发起请求的安全漏洞.一般情况下,ssrf攻击的目标是外网无法访问的内部系统 SSRF漏洞( 服务器端请求伪造 )也被称为跨 ...

  9. CTF_论剑场_Web25

    点击xiazai后面发现404,没办法打开,抓包也没发现啥,用御剑扫描了下发现还有新的页面 点击会跳转到flag.php这个文件,这里应该才是真正的提交页面 另外前面提示了一个ziidan.txt在s ...

  10. 第3篇-CallStub新栈帧的创建

    在前一篇文章 第2篇-JVM虚拟机这样来调用Java主类的main()方法  中我们介绍了在call_helper()函数中通过函数指针的方式调用了一个函数,如下: StubRoutines::cal ...