005原生注解

一、

1
2
3
4
5
6
7
8
9
10
11
12
13
14
@SpringBootApplication
@EnableDiscoveryClient
public class OrderApplication {
 
    public static void main(String [] args){
 
        SpringApplication.run(OrderApplication.class,args);
    }
    @Bean
    @LoadBalanced
    public RestTemplate getRestTemplate(){
        return new RestTemplate();
    }
}

分析@LoadBalanced原生注解代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package org.springframework.cloud.client.loadbalancer;
 
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.springframework.beans.factory.annotation.Qualifier;
 
@Target({ElementType.FIELD, ElementType.PARAMETER, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@Qualifier
public @interface LoadBalanced {
}

解释如下:

1
2
3
4
5
6
7
8
9
10
11
12
@LoadBalanced是一个Spring的注解,用于标注需要进行负载均衡的RestTemplate bean。
 
这个注解的关键点有:
 
@Target指定了这个注解可以用于字段、方法参数和方法上,表示可以用在这些地方进行负载均衡。
@Retention指定了这个注解的生命周期是运行时,也就是说编译之后还存在,可以通过反射读取。
@Documented表示这个注解会保留在javadoc中。
@Inherited表示这个注解可以被子类继承。
@Qualifier表示这个是一个限定器注解,可以用来细分同一类型的bean。
没有@Component等类似注解,表示这个注解本身不会生成bean。
没有参数,表示是一个标志注解,存在即表示需要负载均衡。
总结一下,@LoadBalanced是一个原生注解,用于标注RestTemplate需要进行负载均衡,通过反射和LimitingBeanPostProcessor可以在运行时对这些Bean进行处理,实现负载均衡的功能。

 

posted @   arun_python  阅读(2)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示