spring/springboot 使用汇总

1 springboot 日志

本章参考自springboot指南:https://docs.spring.io/spring-boot/docs/2.2.3.RELEASE/reference/htmlsingle/#boot-features-logging

常用配置项:
# 指定包名下的最低日志等级(默认等级为root:info)
logging.level.包名 = debug 
# 指定日志的输出路径,使用的是spring.log作为默认文件
logging.path = /xxx/
#logging.file = /xxx/a.log
# 在控制台输出的日志的格式
# 指定文件中日志输出的格式
logging.pattern.console = 
logging.pattern.file = 
日志框架分类和选择

大概看看即可:https://www.bilibili.com/video/BV1Et411Y7tQ?p=20

自定义日志配置:

详见springboot指南4.4.6节

注:
日志框架直接识别:logback.xml
由springboot加载配置(推荐):logback-spring.xml

日志中使用profile进行区分

详见springboot指南4.4.7节
若想进行profile区分,首先必须使用springboot加载配置方式(logback-spring.xml)
方法:加上<springProfile name="dev"></springProfile >即可


2 springboot注释汇总

Spring缓存注解@Cacheable、@CacheEvict、@CachePut使用:

https://www.cnblogs.com/fashflying/p/6908028.html

@Conditional注解:

@ConditionalOnBean(A.class)
只有当A.class 在spring的applicationContext中存在时 这个当前的bean才能够创建
@ConditionalOnProperty(prefix = "ams", name = "menu.privilege.on", matchIfMissing = true)
当配置文件配置ams.menu.privilege.on=false的时候,被注释的Bean无效。
其中matchIfMissing=true的意思是:即使没有配置该property时,被注释的Bean也能生效(默认为false,也就是没有配置时会报错)。

详见:https://www.bilibili.com/video/BV1Et411Y7tQ?p=19

AOP的@interface
AOP的@Pointcut
AOP的@ControllerAdvice

详见:https://www.cnblogs.com/lenve/p/10748453.html
该注解可对controller进行增强,一般用在“controller的异常抛出后,跳转到指定页面

@ControllerAdvice
public class MyGlobalExceptionHandler {
    @ExceptionHandler(Exception.class)
    public ModelAndView customException(Exception e) {
        ModelAndView mv = new ModelAndView();
        mv.addObject("message", e.getMessage());
        mv.setViewName("myerror");
        return mv;
    }
}

springmvc部分

springboot扩展mvc部分:

https://www.cnblogs.com/javayihao/p/11994400.html

@Controller和@RestController的区别

1.@Controller修饰的Controller类,其方法返回的字符串,会配合视图解析器InternalResourceViewResolver匹配到对应的视图页面,如返回success,则会对应返回success.html或success.jsp等。
2.而@RestController则会返回success字符串,而且是方法返回什么,页面就接收到什么。
当使用@Controller时,需要方法返回JSON字符串,只需要再加上@RequestBody即可。因此,可以认为:
@RestController = @Controller+@RequestBody;

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot是一个用于构建独立的、生产级的Spring应用程序的框架。它提供了一种快速、简单的方式来创建基于Spring的应用程序,并且具有自动配置和约定优于配置的特性。Spring Boot支持多种场景,包括Web应用程序、RESTful服务、批处理应用程序、消息队列等。\[1\] 在使用Spring Boot时,我们可以创建一个主程序类,通常命名为MainApplication.java。在这个类中,我们使用@SpringBootApplication注解来告诉Spring Boot这是一个Spring Boot应用程序。然后,我们可以在main方法中使用SpringApplication.run方法来运行主程序。\[2\] 如果我们想要扫描特定的包,可以在主程序类的main方法中使用@ServletComponentScan注解,并指定要扫描的包路径。这样,Spring Boot将会扫描指定包下的组件,并将其注册到应用程序中。\[3\] 总结起来,Spring Boot是一个用于构建Spring应用程序的框架,它支持多种场景,并提供了自动配置和约定优于配置的特性。我们可以通过创建主程序类和使用注解来快速构建和运行Spring Boot应用程序。 #### 引用[.reference_title] - *1* *2* *3* [Springboot(大总结)](https://blog.csdn.net/qq_45387521/article/details/115669028)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值