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

文章目录

自定义注解

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();
    }
}

动态代理存在的问题

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

相关推荐
缘来是庄17 小时前
设计模式之代理模式
java·设计模式·代理模式
勤奋的知更鸟13 天前
Java 编程之代理模式
java·开发语言·设计模式·代理模式
HoroMin16 天前
在Spring Boot中自定义JSON返回日期格式的指南
java·开发语言·spring boot·注解
Resurgence0317 天前
代理模式Proxy Pattern
笔记·代理模式
佛祖让我来巡山18 天前
【深入理解Spring AOP】核心原理与代理机制详解
代理模式·aop·springaop
哆啦A梦的口袋呀20 天前
基于Python学习《Head First设计模式》第十一章 代理模式
学习·设计模式·代理模式
啾啾Fun23 天前
Java反射操作百倍性能优化
java·性能优化·反射·缓存思想
寒山李白25 天前
Spring Boot 常用注解面试题深度解析
java·spring boot·面试·注解
爱喝喜茶爱吃烤冷面的小黑黑1 个月前
小黑一层层削苹果皮式大模型应用探索:langchain中智能体思考和执行工具的demo
python·langchain·代理模式
纳于大麓1 个月前
结构性-代理模式
代理模式