自定义解的使用,反射,代理模式

文章目录

自定义注解

springboot 框架中定义了大量的注解,为什么加上注解之后就能实现配置了。比如@Autowired, 将 IOC 中的对象拿出来使用。

创建自定义的注解

java 复制代码
@Target(ElementType.METHOD) //作用的目标
@Retention(RetentionPolicy.RUNTIME) //作用的时间
public @interface  LogAnnotation {
    public String  methodName() default "";
}

在方法上面使用注解

java 复制代码
@LogAnnotation(methodName = "print")
public  void  print(){
     System.out.println("Hello World");
}

使用反射机制来处理注解。反射可以扫描所有的类。在这个里面可以做一些详细得到操作,比如获取方法的一些参数等

java 复制代码
public class AnnotationProcessor {

    public void process() throws NoSuchMethodException {
        Method method = test.class.getMethod("print");
        if (method.isAnnotationPresent(LogAnnotation.class)) {
            LogAnnotation annotation = method.getAnnotation(LogAnnotation.class);
            String methodName = annotation.methodName();
            System.out.println(methodName);
        }
    }
    public static void main(String[] args) {
        AnnotationProcessor annotationProcessor = new AnnotationProcessor();
        try {
            annotationProcessor.process();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }
    }
}

反射

反射是 Java 框架的灵魂,通过反射可以获取任意一个类所有属性和方法,还可以调用这些属性和方法。

spring、mybatis 这些底层都是用了大量的反射机制。

想要通过反射获取到动态的信息 需要获取到 class 对象

plain 复制代码
package com.cl.jvppeteerdemo.util;

import lombok.SneakyThrows;

import java.lang.reflect.Method;

public class ReflectDemo {

   public String  name;
    public static void sayHello(){
        System.out.println("Hello World");
    }
    @SneakyThrows
    public static void main(String[] args) {
        Class<ReflectDemo> reflectDemoClass = ReflectDemo.class;
        reflectDemoClass.getDeclaredMethod("sayHello").invoke(null);
    }

}

代理模式、

静态代理

普通的静态代理对象

java 复制代码
public interface SmsSerivce {
    public void send();
}
java 复制代码
public class SmsSerivceImpl implements SmsSerivce{
    @Override
    public void send() {
        System.out.println("aaa");
    }
}

Proxy 代理类

java 复制代码
public class SMSProxy implements SmsSerivce{

    private SmsSerivce smsSerivce;
    public SMSProxy(SmsSerivce smsSerivce) {
        this.smsSerivce = smsSerivce;
    }
    @Override
    public void send() {
        System.out.println("代理之前设置");
        smsSerivce.send();
        System.out.println("Proxy: SMS sent");
    }
    public static void main(String[] args) {
        SmsSerivce smsSerivce1=new SmsSerivceImpl();
        SMSProxy proxy=new SMSProxy(smsSerivce1);
        proxy.send();
    }
}
动态代理
java 复制代码
package com.cl.jvppeteerdemo.静态代理;

import java.lang.reflect.Proxy;

public class JDKProxyFactory {

    public  static  Object test(Object target){
        return Proxy.newProxyInstance(
            // target通带代理类
            target.getClass().getClassLoader(),
            //被代理类实现的接口,可以有很多
            target.getClass().getInterfaces(),
            //实现invovationHandler的对象
            new DebugInvocationHandler(target)
        );
    }

    public static void main(String[] args) {
        SmsSerivce smsSerivce  = (SmsSerivce) JDKProxyFactory.test(new SmsSerivceImpl());
        smsSerivce.send();
    }
}

动态代理存在的问题

只能代理实现接口的类, 如果这个类没有实现接口那么就不能实现代理。

相关推荐
Auc243 天前
OJ判题系统第4期之判题机模块架构——设计思路、实现步骤、代码实现(工厂模式、代理模式的实践)
java·spring cloud·log4j·mybatis·代理模式·工厂模式
有梦想的攻城狮4 天前
spring中的@Qualifier注解详解
java·后端·spring·注解·qualifier
Yang三少喜欢撸铁4 天前
【阿里云免费领取域名以及ssl证书,通过Nginx反向代理web服务】
nginx·阿里云·代理模式·ssl
Blurpath5 天前
什么是静态住宅IP?为什么静态住宅IP能提高注册通过率?
网络·代理模式·ip代理·住宅ip
python算法(魔法师版)5 天前
JavaScript性能优化实战,从理论到落地的全面指南
开发语言·性能优化·前端框架·代理模式
常某某的好奇心8 天前
代理模式(Proxy Pattern)
代理模式
彬彬醤8 天前
全局网络:重构数字时代的连接范式
运维·服务器·网络·数据库·重构·代理模式
菜鸟破茧计划11 天前
穿越数据森林与网络迷宫:树与图上动态规划实战指南
网络·动态规划·代理模式
yy鹈鹕灌顶12 天前
动态规划算法精解(Java实现):从入门到精通
代理模式
帝锦_li13 天前
Java进阶--设计模式
观察者模式·单例模式·代理模式·抽象工厂模式·适配器模式·原型模式