spring-02

AOP

静态代理

角色分析:

        抽象角色:一般会使用接口或抽象类类解决

        真实角色:被代理角色

        代理角色:代理真实角色,代理真实角色后,我们一般会做一些附属操作

        客户:访问代理对象的人

代码步骤:

1、接口

//租房
public interface Rent {
    public void rent();
}

2、真实角色

//房东
public class Host implements Rent {
    public void rent() {
        System.out.println("房东要出租房子!");
    }
}

3、代理角色

//中介
public class Procy implements Rent {
    Host host= new Host();
    public void rent() {
//        System.out.println("代理帮忙出租房子");
        kanfang();
        host.rent();
        hetong();
        fare();
    }
    public Procy(){}

    public Procy(Host host){
        this.host = host;
    }
    //看房
    public void kanfang(){
        System.out.println("中介帮忙看房");
    }
    //收中介费
    public void fare(){
        System.out.println("收中介费");
    }
    //签合同
    public void hetong(){
        System.out.println("签合同");
    }
}

4、客户端访问代理角色

//租房子的人
public class Client {
    public static void main(String[] args) {
        Host host = new Host();
        Procy procy = new Procy(host);
        procy.rent();
    }

}

代理模式的好处:

        可是使真实角色的操作更加纯粹,不用去关注一些公共的业务

        公共业务也是交给代理角色,实现了业务分工

        公共业务发生扩展的时候,方便集中管理

        一个动态代理类代理的是一个接口,一般就是对应的一类业务

        一个动态代理类可以代理多个类,只要是实现了同一个接口接口

缺点:一个真实角色就会产生一个的代理角色,代码量会翻一倍

package com.gykj.demo2;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
//通用代理
public class ProxyInvocationHandler1 implements InvocationHandler {
    //被代理的接口
    private Object target;
    public void  setTarget(Object target){
        this.target = target;
    }

    //生成代理类
    public Object getProxy(){
        return Proxy.newProxyInstance(this.getClass().getClassLoader(),target.getClass().getInterfaces(),this);
    }

    //处理代理实例,并返回结果
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        //动态代理的本质,就是使用反射机制实现
        Object result = method.invoke(target,args);
        return result;
    }
}

AOP

AOP:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术,AOP是OOP的延续,是软件开发中的一个热点,也是spring框架中的一个重要内容,是函数式编程的一种衍生即使,利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率

spring实现AOP

首先导入包

        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.9.8.M1</version>
        </dependency>

 方式一:使用Spring的AIP接口

package com.gykj.Log;

import org.springframework.aop.BeforeAdvice;
import org.springframework.aop.MethodBeforeAdvice;

import java.lang.reflect.Method;

public class Log implements MethodBeforeAdvice {
    //method:要执行的目标对象的方法
    //args:参数
    //target:目标对象
    public void before(Method method, Object[] args, Object target) throws Throwable {
        System.out.println(target.getClass().getName()+"执行了"+method.getName()+"方法");

    }
}

方式二:自定义来实现AOP(主要是切面定义)

package com.gykj.Diy;

public class DiyPointCut {
    public void before(){
        System.out.println("==============方法执行前===============");
    }
    public void after(){
        System.out.println("==============方法执行后===============");
    }
}

方式三 注解

package com.gykj.Diy;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

@Aspect
public class AnnotationPointCut {

    @Before("execution(* com.gykj.service.UserServiceImpl.*(..))")
    public void before(){
        System.out.println("==============执行方法前=============");
    }

    @After("execution(* com.gykj.service.UserServiceImpl.*(..))")
    public void after(){
        System.out.println("==============执行方法后=============");
    }

    //在环绕增强中,我们可以给定一个参数,代表我们要获取处理切入的点
    @Around("execution(* com.gykj.service.UserServiceImpl.*(..))")
    public void around(ProceedingJoinPoint jp) throws Throwable{
        System.out.println("环绕前");
        //执行方法
        Object proceed = jp.proceed();
        System.out.println("环绕后");

    }
}

并要在配置中开启注解支持 

配置

<?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
    https://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">

    <!--    id  bean唯一标识符
            class:全限定名-->
<!--    注册bean-->
    <bean id = "userservice" class="com.gykj.service.UserServiceImpl"/>
    <bean id="AfterLog" class="com.gykj.Log.AfterLog"/>
    <bean id = "Log" class="com.gykj.Log.Log"/>

<!--    方式一:使用原生spring API接口-->
<!--    配置aop,需导入aop约束-->
    <aop:config>
<!--        切入点:expression 表达式 execution(需执行的位置 ! * * * *-->
        <aop:pointcut id="pointcut" expression="execution(* com.gykj.service.UserServiceImpl.*(..))"/>
<!--        执行环绕增加-->
        <aop:advisor advice-ref="Log" pointcut-ref="pointcut"/>
        <aop:advisor advice-ref="AfterLog" pointcut-ref="pointcut"/>
    </aop:config>

<!--    方式二 自定义类-->
    <bean id="diyPoint" class="com.gykj.Diy.DiyPointCut"/>

    <aop:config>
<!--        自定义切面,ref为要引用的类-->
        <aop:aspect ref="diyPoint">
<!--            切面点-->
            <aop:pointcut id="point" expression="execution(* com.gykj.service.UserServiceImpl.*(..))"/>
<!--            通知-->
            <aop:before method="before" pointcut-ref="point"/>
            <aop:after method="after" pointcut-ref="point"/>
        </aop:aspect>
    </aop:config>

<!--    方式三-->
    <bean id="annotion" class="com.gykj.Diy.AnnotationPointCut"/>
<!--    开启注解支持-->
    <aop:aspectj-autoproxy/>


</beans>

 Cause: com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException: 1 字节的 UTF-8 序列的字节 1 无效。

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

spring整合mybatis

1、有一个类

2、对这个类进行操作的接口

3、有xml来与数据库进行增删改查

4、spring配置DataSource、SqlSessionFactory、SqlSession

5、有一个实现类实现SqlSession

6、进行测试

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值