您的位置:首页 > 编程语言 > Java开发

spring springboot springcloud常用注解

2020-02-02 06:52 1231 查看

【spring】
@Qualifier
在Controller中需要注入service那么我的这个server有两个实现类如何区分开这两个impl呢?
Qualifier的意思是合格者,通过这个标示,表明了哪个实现类才是我们所需要的,添加@Qualifier注解,需要注意的是@Qualifier的参数名称为我们之前定义@Service注解的名称之一。
使用@resource注入时比较简单了注解自带了“name”的val就是@Service注解的名称之一。
https://blog.csdn.net/qq_36567005/article/details/80611139

@Component    最普通的组件,可以被注入到spring容器进行管理,如果想使用自定义的组件注解
@Repository    作用于持久层
@Service    作用于业务逻辑层
@Controller    作用于表现层(spring-mvc的注解)
https://blog.csdn.net/fansili/article/details/78740877

@Bean

注解在返回实例的方法上,如果未通过@Bean指定bean的名称,则默认与标注的方法名相同;

 

Spring中的@Bean是否一定要与@Configuration一起用?

使用@Configuration注解,此时调用方法返回的是被Spring管理的单例Bean。
如果换做是@Component 注解,那么调用了 方法返回的对象是执行这个方法返回的对象实例,而不是被spring管理的对象。
这就是差别所在。

https://blog.csdn.net/weixin_42749765/article/details/87098790

 

@Configuration

用于定义配置类,可替换xml配置文件,被注解的类内部包含有一个或多个被@Bean注解的方法,这些方法将会被AnnotationConfigApplicationContext或AnnotationConfigWebApplicationContext类进行扫描,并用于构建bean定义,初始化Spring容器。

@Configuation等价于<Beans></Beans>
@Bean等价于<Bean></Bean>
@ComponentScan等价于<context:component-scan base-package=”com.dxz.demo”/>
https://blog.csdn.net/BinshaoNo_1/article/details/85005935

@Configuration 注解本质上还是 @Component
虽然Component注解也会当做配置类,但是并不会为其生成CGLIB代理Class,所以在生成Driver对象时和生成Car对象时调用car()方法执行了两次new操作,所以是不同的对象。当时Configuration注解时,生成当前对象的子类Class,并对方法拦截,第二次调用car()方法时直接从BeanFactory之中获取对象,所以得到的是同一个对象。
原文链接:https://blog.csdn.net/long476964/article/details/80626930

一句话概括就是 @Configuration 中所有带 @Bean 注解的方法都会被动态代理,因此调用该方法返回的都是同一个实例。
https://blog.csdn.net/isea533/article/details/78072133

 

@Resource
作用:用来装配bean,可以写在字段上或者setter方法上。默认按照名称来装配注入,找不到名称时按照类型来装配注入。
https://blog.csdn.net/yuyeqianhen/article/details/90177597

@Component 作用于类,@Bean作用于方法。
@Component和@Bean都是用来注册Bean并装配到Spring容器中,但是Bean比Component的自定义性更强。可以实现一些Component实现不了的自定义加载类。
https://blog.csdn.net/weixin_42493179/article/details/86584341

@Documented
注解标记的元素,Javadoc工具会将此注解标记元素的注解信息包含在javadoc中。

@ConfigurationProperties 批量注入配置文件中的属性
@Value 一个一个注入
https://blog.csdn.net/clmmei_123/article/details/81871836

@EnableAutoConfiguration
自动载入应用程序所需的所有Bean——这依赖于Spring Boot在类路径中的查找。
从classpath中搜索所有META-INF/spring.factories配置文件然后,将其中org.springframework.boot.autoconfigure.EnableAutoConfiguration key对应的配置项加载到spring容器
只有spring.boot.enableautoconfiguration为true(默认为true)的时候,才启用自动配置
https://blog.csdn.net/l18848956739/article/details/100692163

Spring中也有一种类似与Java SPI的加载机制。它在META-INF/spring.factories文件中配置接口的实现类名称,然后在程序中读取这些配置文件并实例化。这种自定义的SPI机制是Spring Boot Starter实现的基础。
https://blog.csdn.net/lldouble/article/details/80690446

@EnableTransactionManagement
Spring Boot的事务管理注解@EnableTransactionManagement的使用

 

RefreshScope(org.springframework.cloud.context.scope.refresh)是spring cloud提供的一种特殊的scope实现,用来实现配置、实例热加载。
https://www.jianshu.com/p/188013dd3d02
需要热加载的bean需要加上@RefreshScope注解,当配置发生变更的时候可以在不重启应用的前提下完成bean中相关属性的刷新。
https://blog.csdn.net/weixin_40318210/article/details/87954179

 

@Conditional

@ConditionalOnBean // 当给定的在bean存在时,则实例化当前Bean

@ConditionalOnMissingBean // 当给定的在bean不存在时,则实例化当前Bean

@ConditionalOnClass // 当给定的类名在类路径上存在,则实例化当前Bean

@ConditionalOnMissingClass // 当给定的类名在类路径上不存在,则实例化当前Bean

https://www.geek-share.com/detail/2771581280.html

https://www.geek-share.com/detail/2771440460.html

[code]@Configuration
@EnableConfigurationProperties(CustomerProperties.class)
@ConditionalOnClass(CustomerService.class)
@ConditionalOnProperty(prefix = "customer", value = "enabled", matchIfMissing = true)
public class CustomerAutoConfiguration {

@Autowired
private CustomerProperties customerProperties;

@Bean
@ConditionalOnMissingBean(CustomerService.class)
public CustomerService customerService() {
CustomerService customerService = new CustomerService();
customerService.setName(customerProperties.getName());
return customerService;
}
}

https://my.oschina.net/liululee/blog/2222989

http://www.mamicode.com/info-detail-2312437.html

简单一句话就是,如果指定missing的class比如A,那么生成@Bean这个bean的前提条件就是spring工厂没有这个A,如果没有指定missing的bean,那么A就是它自己,也就是一个工厂不能实例化两次这种bean

 

@ConditionalOnProperty

作用在类上
https://www.cnblogs.com/duanxz/p/7493276.html

@ConditionalOnProperty
可以通过配置文件中的属性值来判定configuration是否被注入
在spring boot中有时候需要控制配置类是否生效,可以使用@ConditionalOnProperty注解来控制@Configuration是否生效.

 

@EnableDiscoveryClient和@EnableEurekaClient
共同点就是:都是能够让注册中心能够发现,扫描到改服务。
不同点:@EnableEurekaClient只适用于Eureka作为注册中心,@EnableDiscoveryClient 可以是其他注册中心。
https://blog.csdn.net/zheng199172/article/details/82466139


 

  • 点赞 7
  • 收藏
  • 分享
  • 文章举报
21aspnet 博客专家 发布了1595 篇原创文章 · 获赞 1163 · 访问量 1224万+ 他的留言板 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: