- 一、SpringBoot
- @Document
- @EnableCaching
- @EnableWebSecurity
- @SringBootApplication
- @EnableDiscoveryclient
- @EnableAdminServer
- @EnableEurekaClient
- @EnableEurekaServer
- @EnableHystrix
- @HystrixCommand
- @FeignClient
- @RefreshScope
- @FeignClient
- @EnableFeignClients
- @EnableCircuitBreaker
- @LoadBalanced
- @WebAppConfiguration
- @RibbonClient
- @JsonBackReference
- @RepositoryRestResourcepublic
- @SpringBootApplication
- @EnableRedisHttpSession
- 二、Spring与SpringMVC
- @Controller
- @Service
- @Qualifier
- @Repository
- @Autowired
- @Resource
- @Component
- @RequestMapping (value=’’,method={RequestMethod。GET或者POSt})
- @RequestParam (value=’’ required=false)
- @ModelAttribute
- @Transactional (readOnly=true)
- @TransactionalEventListener
- @Value(“${}”)
- @ControllerAdvice
- @ExceptionHandler
- @ComponentScan
- @Comfiguration
- @Import
- @ImportResource
- @PathVariable
- @InitBinder
- @RestController
- @EnableAutoConfiguration
- @CrossOrigin
- @ApiOperation
一、SpringBoot
@Document
@Document(indexName=””,shards=””,replicas=””),标注在实体类上,声明由ES管理数据
@EnableCaching
@EnableWebSecurity
注解开启spring security的功能,集成websercrityconfigureadapter。
@SringBootApplication
相当于@configuration,@EnableAutoConfiguation @ComponentScan三个注解合用。
@EnableDiscoveryclient
@EnableAdminServer
@EnableEurekaClient
配置本应用将使用服务注册和服务发现,注意:注册和发现用这个注解。
@EnableEurekaServer
@EnableHystrix
@HystrixCommand
注解方法失败后,系统将西东切换到fallbackMethod方法执行。指定回调方法
@FeignClient
@RefreshScope
如果代码中需要动态刷新配置,在需要的类上加上该注解就行。但某些复杂的注入场景下,这个注解使用不当,配置可能仍然不动态刷新;
@FeignClient
springboot调用外部接口:声明接口之后,在代码中通过@Resource注入之后即可使用。@FeignClient标签的常用属性如下:name:指定FeignClient的名称,如果项目使用了Ribbon,name属性会作为微服务的名称,用于服务发现
url: url一般用于调试,可以手动指定@FeignClient调用的地址decode404:当发生http 404错误时,如果该字段位true,会调用decoder进行解码,否则抛出FeignException
configuration: Feign配置类,可以自定义Feign的Encoder、Decoder、LogLevel、Contractfallback: 定义容错的处理类,当调用远程接口失败或超时时,会调用对应接口的容错逻辑,fallback指定的类必须实现@FeignClient标记的接口
fallbackFactory: 工厂类,用于生成fallback类示例,通过这个属性我们可以实现每个接口通用的容错逻辑,减少重复的代码path: 定义当前FeignClient的统一前缀
@EnableFeignClients
@EnableCircuitBreaker
@LoadBalanced
@WebAppConfiguration
开启Web 应用的配置,用于模拟ServletContext
@RibbonClient
这个注解用来为负载均衡客户端做一些自定义的配置,可以进一步配置或自定义从哪里获取服务端列表、负载均衡策略、Ping也就是服务鉴活策略等等
@JsonBackReference
@RepositoryRestResourcepublic
配合spring-boot-starter-data-rest使用
@SpringBootApplication
申明让spring boot自动给程序进行必要的配置,等价于以默认属性使用@Configuration,@EnableAutoConfiguration和@ComponentScan;
@EnableRedisHttpSession
默认关闭,开启Spring Session Data Redis 技术中提供的自定义会话实现。
WEB工程默认使用Tomcat提供的会话实现。
二、Spring与SpringMVC
@Controller
@Service
@Qualifier
@Repository
@Autowired
@Resource
bean的注入,同Autowired 有相同的功能。
说明:
共同点:@Resource和@Autowired都可以作为注入属性的修饰,在接口仅有单一实现类时,两个注解的修饰效果相同,可以互相替换,不影响使用。
不同点:
@Resource是Java自己的注解,@Resource有两个属性是比较重要的,分别是name和type;Spring将@Resource注解的name属性解析为bean的名字,而type属性则解析为bean的类型。所以如果使用name属性,则使用byName的自动注入策略,而使用type属性时则使用byType自动注入策略。如果既不指定name也不指定type属性,这时将通过反射机制使用byName自动注入策略。
@Autowired是spring的注解,是spring2.5版本引入的,Autowired只根据type进行注入,不会去匹配name。如果涉及到type无法辨别注入对象时,那需要依赖@Qualifier或@Primary注解一起来修饰。
@Component
@RequestMapping (value=’’,method={RequestMethod。GET或者POSt})
@RequestParam (value=’’ required=false)
@ModelAttribute
一般用于controller层,被注解的方法会在所以mapping执行之前执行,并且可以绑定参数到Model model里面。
@Transactional (readOnly=true)
@TransactionalEventListener
用于配置事务的回调方法,可以在事务提交前、提交后、完成后以及回滚后几个阶段接受回调事件。
@Value(“${}”)
@ControllerAdvice
@ExceptionHandler
如果在controller方法遇到异常,就会调用含有此注解的方法。
@ComponentScan
表示将该类自动发现并注册bean 可以自动收集所有的spring组件
@Comfiguration
@Import
@ImportResource
@PathVariable
@InitBinder
一般用于controller 可以将所以form 讲所有传递进来的string 进行html编码,防止xss攻击,比如可以将字符串类型的日期转换成date类型
@RestController
返回json字符串的数据,直接可以编写RESTFul的接口;
@EnableAutoConfiguration
spring boot自动配置,尝试根据你添加的jar依赖自动配置你的spring应用。
@CrossOrigin
@ApiOperation
首先@ApiOperation注解不是Spring自带的,它是是swagger里的注解@ApiOperation是用来构建Api文档的@ApiOperation(value = “接口说明”, httpMethod = “接口请求方式”, response = “接口返回参数类型”, notes = “接口发布说明”;