springboot学习笔记

来源:互联网 发布:网络单机一键端论坛 编辑:程序博客网 时间:2024/05/18 07:47

1.application.yml和bootstrap.yml区别?

application.yml 是应用级别的配置 bootstrap.yml是系统级别的配,一般不变的放到bootstrap.yml 需要随业务变得放到application.yml

2.springboot新增改进注解

@Autowired                         忽略吧
@ActiveProfiles                    忽略吧
@Profile                        忽略吧
@Aspect                         忽略吧
@PointCut                        忽略吧
@Around                            忽略吧    
@AointCut                        忽略吧
@Repository                        忽略吧
@Service
@Component                        忽略吧
@Controller                        忽略吧
@RequestMapping                    忽略吧
@ControllerAdvice                忽略吧
@ExceptionHandler                忽略吧
@InitBinder                        (忽略吧)设置WebDataBinder,WebDataBinder用来自动绑定前台参数到mdel中
@ModelAttribute                    忽略吧
@Cacheable                        忽略吧
@CacheEvict                        忽略吧    
@CachePut                        忽略吧
@PostConstruct                    忽略吧
@PreDestroy                        忽略吧
@Resource                        忽略吧
@RequestMapping                    忽略吧
@RequestParam                    忽略吧
@PathVariable                    忽略吧
@RequestBody                    忽略吧
@ResponseBody                    忽略吧
@RequestHeader                    忽略吧
@Required                        忽略吧
@RestController                    忽略吧
@Scope                            忽略吧
@Transcational                    忽略吧
@Value                            忽略吧
@WebAppConfiguration            忽略吧

@Bean                            实例化bean
@SpringBootApplication            等价于@Configuration,@ComponentScan,@EnableAutoConfiguration
@ComponentScan                    扫描包
@Configuration                    配置类
@Import                            导入配置类或者Bean
@ImportResource                    导入XML配置文件
@EnableAutoConfiguration          这个SpringBoot根据已添加的jar依赖猜测并完成自动化配置(META-INF/spring.factories文件)    
@PropertySource                    加载属性文件,绑定值,必须使用properties文件
@ConfigurationProperties        绑定属性值,适用于类或方法(第三方组件),支持javax.validation注解校验
@EnableConfigurationProperties  开启对@ConfigurationProperties注解配置Bean的支持
@ConfigurationPropertiesBinding 绑定属性时属性转换

@RequestPart                    忽略吧(绑定“multipart/form-data”参数)
@ServletComponentScan            注入@WebServlet,@WebFilter,@WebListener的类
@EnableWebMvc                    (以前旧有)开启Web Mvc支持,关于RequestMappingHandlerMapping,RequestMappingHandlerAdapter
@ExceptionHandler                (以前旧有)全局处理异常,关于ExceptionHandlerExceptionResolver的
@GetMapping                        Spring4.3中引进的,组合注解,是@RequestMapping(method = RequestMethod.GET)的缩写
@PostMapping                    同上
@PutMapping                        同上
@DeleteMapping                    同上
@CrossOrigin                      ajax跨域
@JsonComponent                  自定义JSON序列化器和反序列化器
@EnableWebSocketMessagetBroker     开启WebSocket支持
@MessageMapping                    websocket

@EnableScheduling                开启对计划任务的支持
@Scheduled                        声明这是一个计划任务,方法返回类型为void,支持cron、fixDelay、fixRate
@EnableAsync                    开启对异步任务的支持
@Async                            异步任务方法或者异步类
@Qualifier                          可以单独在方法中使用,改进新特性吧,相当于@Autowired
@Primary                        唯一可用
@EnableAspectJAutoProxy            启用AOP,AnnotationAwareAspectJAutoProxyCreator
@EnableCaching                    开启缓存支持
@CacheConfig                      类中指定cacheNames的,对@CacheEvict(value = "user")重复定义的改进

@EnableSpringSecurity            开启spring seccurity支持
@EnableGlobalMethodSecurity     启用'basic'认证
@EnableAuthorizationServer      oauth2 access tokens
@EnableOAuth2Client                  安全
@EnableOAuth2Sso                安全

@EnableTransactionManagement    开启注解式事务的支持
@NoRepositoryBean                JPA
@EnableJpaRepositories            JPA
@EntityScan                        JPA
@Entity                            JPA
@Embeddable                        JPA
@MappedSuperclass                JPA
@NodeEntity                        Neo4j
@EnableNeo4jRepositories          Neo4j
@SolrDocument                     Solr
@Document                        Elasticsearch

@AutoConfigureAfter                auto-configuration
@AutoConfigureBefore            auto-configuration
@AutoconfigureOrder                auto-configuration
@Conditional                    可以标注在类上面,表示该类下面的所有@Bean都会启用配置
@ConditionalOnClass                某个class位于类路径上,才会实例化一个Bean
@ConditionalOnMissingClass         某个class类路径上不存在的时候,才会实例化一个Bean
@ConditionalOnBean                Bean条件
@ConditionalOnMissingBean        Bean条件
@ConditionalOnProperty            Property条件
@ConditionalOnResource            Resource条件
@ConditionalOnWebApplication    WebApplication条件
@ConditionalOnNotWebApplication    WebApplication条件
@ConditionalOnExpression        SpEL表达式条件


@EnableIntegration                启用spring-boot-starter-integration基于消息和其他传输协议的抽象,比如HTTP,TCP等
@EnableJms                        开启JMS支持
@JmsListener                    创建一个监听者端点,默认是支持事务性的
@EnableRabbit                    开启Rabbit支持(AMQP)
@RabbitListener                    创建一个监听者端点
@ManagementContextConfiguration    Actuator模块
@ExportMetricWriter                Actuator模块
@ExportMetricReader                Actuator模块
@ManagedResource                JMX
@ManagedAttribute                JMX
@ManagedOperation                JMX

@SpringBootTest                    Test,添加@RunWith(SpringRunner.class)    
@TestConfiguration                类似@Configuration
@TestComponent                    Test
@LocalServerPort                用于注入测试用例实际使用的端口
@MockBean                        Test
@SpyBean                        Test
@JsonTest                        测试对象JSON序列化和反序列化是否工作正常
@AutoConfigureJsonTesters         Test
@WebMvcTest                        检测单个Controller是否工作正常
@AutoConfigureMockMvc            注解一个non-@WebMvcTest的类
@DataJpaTest                    JPA测试
@AutoConfigureTestEntityManager Test
@AutoConfigureTestDatabase         真实DB/嵌入DB
@RestClientTest                    Test
@AutoConfigureRestDocs            Test
@Rule                            使用OutputCapture,捕获System.out和System.err输出
@TestRestTemplate                Test

3.@ConfigurationProperties vs @Value?

  特性              @ConfigurationProperties @Value    
  Relaxed绑定   Yes                      No
  Meta-data支持 Yes                      No
  SpEL表达式     No                       Yes
  Relaxed绑定:    松绑定,Environment属性名和bean属性名不需要精确匹配,如firstName可以表示为:
                属性                  说明
                person.firstName     标准驼峰规则
                person.first-name     虚线表示,推荐用于.properties和.yml文件中
                person.first_name     下划线表示,用于.properties和.yml文件的可选格式
                PERSON_FIRST_NAME     大写形式,使用系统环境变量时推荐
   Meta-data支持:支持spring元数据,满足spring原数据格式http://docs.spring.io/spring-boot/docs/1.4.1.RELEASE/reference/htmlsingle/#configuration-metadata    

4.常用对象

  AnnotationConfigApplicationContext
  AnnotationConfigEmbeddedWebApplicationContext
  .....

5.常见jar迁移  

  druid ->             druid-spring-boot-starter
  mybatis-spring -> mybatis-spring-boot-starter:MybatisAutoConfiguration(自动注入@Mapper)
  jedis ->            pring-boot-starter-data-redis

 ...

后续spring cloud笔记再补充

原创粉丝点击