Spring Cloud常用注解

@SpringBootApplication(exclude = {org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration.class})
概述: @SpringBootApplication = (默认属性)@Configuration + @EnableAutoConfiguration + @ComponentScan ,@ComponentScan basePackages,includeFilters,excludeFilters属性来动态确定自动扫描范围,类型已经不扫描的类型,默认情况下:它扫描所有类型,并且扫描范围是 @ComponentScan 注解所在配置类包及子包的类。
参考:https://www.cnblogs.com/MaxElephant/p/8108140.htmlhttps://www.jianshu.com/p/39ee4f98575c

@EnableEurekaClient
概述: 如果选用的注册中心是eureka,那么就推荐@EnableEurekaClient,如果是其他的注册中心,那么推荐使用@EnableDiscoveryClient。
参考:https://www.jianshu.com/p/f6db3117864f

@FeignClient(name = "bigdataservice")
概述:FeignClient简化了请求的编写,且通过动态负载进行选择要使用哪个服务进行消费,而这一切都由Spring动态配置实现,我们不用关心这些,只管使用方法即可。再说,就是简化了编写,RestTemplate还需要写上服务器IP这些信息等等,而FeignClient则不用。
参考:https://blog.csdn.net/github_39577257/article/details/81842234https://blog.csdn.net/ZYC88888/article/details/81291684

@RestController
概述:相当于@Controller+@ResponseBody两个注解的结合,返回json数据不需要在方法前面加@ResponseBody注解了,但使用@RestController这个注解,就不能返回jsp,html页面,视图解析器无法解析jsp,html页面。
参考:https://www.cnblogs.com/shuaifing/p/8119664.htmlhttps://blog.csdn.net/qq_41063141/article/details/81805082

@RequestMapping("/fei")
概述:@RequestMapping请求路径映射,如果标注在某个controller的类级别上,则表明访问此类路径下的方法都要加上其配置的路径;最常用是标注在方法上,表明哪个具体的方法来接受处理某次请求。
参考:https://blog.csdn.net/yiye2017zhangmu/article/details/80500932https://blog.csdn.net/jeofey/article/details/76019953https://www.oschina.net/translate/using-the-spring-requestmapping-annotation

@GetMapping("/cf")
概述:@GetMapping是一个组合注解,是@RequestMapping(method = RequestMethod.GET)的缩写,该注解将 HTTP Get 映射到特定的处理方法上。
参考:https://www.cnblogs.com/niexinlei/p/9704075.htmlhttps://blog.csdn.net/weixin_38553453/article/details/73692863

@Getter
注解在类上, 为类提供读写属性
参考:https://blog.csdn.net/qq_37433657/article/details/83275051
@Setter
注解在类上, 为类提供读写属性
参考:https://blog.csdn.net/qq_37433657/article/details/83275051
@ToString
注解在类上,为类提供toString()方法
参考:https://blog.csdn.net/qq_37433657/article/details/83275051
@ExcelTitle("资产编号")
@Documented //文档
@Retention(RetentionPolicy.RUNTIME) //在运行时可以获取
@Target({ ElementType.TYPE, ElementType.FIELD}) //作用到类,方法,接口上等
@Inherited //子类会继承
@Data
注解在类上,为类提供读写属性, 此外还提供了equals()、hashCode()、toString()方法
参考:https://blog.csdn.net/qq_37433657/article/details/83275051
@Resource
默认按名称装配,当找不到与名称匹配的bean才会按类型装配
参考:https://blog.csdn.net/qq_16055765/article/details/78833260
 
@Component
泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注
参考:https://blog.csdn.net/qq_16055765/article/details/78833260
 
@Validated
用在类型、方法和方法参数上。但不能用于成员属性(field)
参考:https://blog.csdn.net/weixin_38118016/article/details/80977207https://blog.csdn.net/herojuice/article/details/86020101

@Valid
用于验证注解是否符合要求,可以用在方法、构造函数、方法参数和成员属性(field)上
参考:https://blog.csdn.net/weixin_38118016/article/details/80977207https://blog.csdn.net/herojuice/article/details/86020101
 
@EnableFeignClients
启用feign客户端

@EnableRedisHttpSession

@Bean
带有@Configuration的注解类表示这个类可以使用Spring IoC容器作为bean定义的来源。@Bean注解告诉Spring,一个带有@Bean的注解方法将返回一个对象,该对象应该被注册为在Spring应用程序上下文中的bean。
参考:https://www.cnblogs.com/microcat/p/7074720.html
@Primary
自动装配时当出现多个Bean候选者时,被注解为@Primary的Bean将作为首选者,否则将抛出异常
参考:https://blog.csdn.net/qq_16055765/article/details/78833260
 
@ConditionalOnMissingBean(ObjectMapper.class)
 
@Service
@Service用于标注业务层组件,@Controller用于标注控制层组件(如struts中的action),@Repository用于标注数据访问组件,即DAO组件,而@Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。
@Autowired
可以对类成员变量、方法及构造函数进行标注,完成自动装配的工作。通过@Autowired的使用来消除set,get方法。

@Override
在重写父类的onCreate时,在方法前面加上@Override系统可以帮你检查方法的正确性。

@Repository
@Service用于标注业务层组件,@Controller用于标注控制层组件(如struts中的action),@Repository用于标注数据访问组件,即DAO组件,而@Component泛指组件,当组件不好归类的时候,我们可以使用这个注解进行标注。

@Query(value = "AVG(cost) cost,SUM(totla_original_value) original,AVG(aera) aeara,STATISTICS_MONTH month from stat_finance where STATISTICS_YEAR=?1 and department=?2 group by STATISTICS_MONTH", nativeQuery = true)
为了简化Dao的代码,使用了JPA来写Dao,Dao的接口方法上使用@Query注释,不需要再写一个实现类(Impl)。
 

带有 @Configuration 的注解类表示这个类可以使用 Spring IoC 容器作为 bean 定义的来源。@Bean 注解告诉 Spring,一个带有 @Bean 的注解方法将返回一个对象,该对象应该被注册为在 Spring 应用程序上下文中的 bean。

猜你喜欢

转载自www.cnblogs.com/ratels/p/11121385.html