SpringBoot笔记

学习材料:https://ke.qq.com/course/282793

一  spring boot的启动方式:

1 main
SpringApplication.run(Application.class, args);
new SpringApplicationBuilder(Application.class).run(args);
设置banner关闭:new SpringApplication(Application.class).setBannerMode(Banner.Mode.OFF);
    new SpringApplicationBuilder(Application.class).bannerMode(Banner.Mode.OFF);
2 spring boot: run
3 java -jar **.jar


二 springboot参数设置
1 @Autowired
  private ApplicationArguments applicationArguments;
  applicationArguments.getNonOptionArgs() //获取main方法config配置传入的参数;
2 java -jar **.jar password=123456
3 springboot的主要参数来源,优先级由上到下
命令行参数
servletConfig/ServletContext;
操作系统环境变量
application-{profile}.properties或者YAML文件;
application.properties或者YAML文件;
eg: server.port = 8090
server.context-path=/aaa
4 SpringBoot的properties绑定机制
默认从application.properties中加载配置
资源文件(application.properties)加载顺序,优先级由上到下
1 当前目录/config子目录
2 当前目录
3 classpath下的/config子目录
4 classpath
修改application.properteis命名使之生效
1 可以通过命令行的方式 java -jar **.jar --spring.config.name=app666
--spring.config.location=配置文件具体加载地址
三 ConfigurationProperties参数绑定 
1 java config方式
2 类是自己写的(类和properties属性有对应关系)
@Component 注解类
@ConfigurationProperties(prefix="db"/"db") 其中db是properties文件的属性的前缀,例如 db.username会对应username属性
3 类如果是第三方的,可以将注解
@ConfigurationProperties("db")写在生成对象的方法上
4 支持松绑定,即user_name可绑定到username等
5 可以在命令行中绑定 --db.username=“username”
6 通过@Value("${db.username}") 将properties中的值注入给属性
四 springboot热部署 
步骤1
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional> 依赖不传递
</denpendency>
步骤2  修改完成后重新编译
热部署的原理是当发现classpath内容有修改时,重启服务
可以通过配置,配置静态文件(.html/.css等)修改不必重新启动,也可以直接热部署
五 集成 mybatis
1 引入依赖
  <dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.0</version>
  </denpendency>
2 正常完成mapper接口和mapper.xml
3 mybatis-spring-boot-starter提供了以下配置信息(具体参考MyBatisProperties对象)
mybatis.configLocation mytais的配置文件地址
mybatis.mapperLocations 映射文件地址(接口和xml在同一目录可以不必配置)
mytais.typeAliasesPackage 别名扫描包
logging.level.mapper所在包名=debug 输出sql执行语句
4 使用@MapperScan标签扫描mapper接口(必须是到mapper包层),在Application.java中
5 可以在pom文件中的build标签中添加如下配置,使src下的资源也可以加载到classes目录
<resources>
<resource>
<directory>src/main.java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
六 springboot配置事务
1 基于注解的事务管理步骤
a,在主配置类上@EnableTransactionManagement表示开启事务注解驱动支持,等价xml中的<tx:annotation-driven/>
b,在需要事务的service上贴@Transactional,表示该类下面的所有方法都需要事务;
2 基于xml的事务管理
a, 添加依赖(主要是要添加支持AspectJ表达式的支持)
<denpendency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
<optional>true</optional>
</denpendency>
a, 在resource目录下添加application-context.xml(事务配置文件)
b, 在配置类中添加@ImportResource("classpath:application-context.xml")
c, 在配置类中添加txManager的管理
@Bean
public PlatformTransactionManager txManager(DataSource dataSource)
{
DataSourceTransactionManager txManager = new DataSourceTransactionManager();
txManager.setDataSource(dataSource);
return txManager;
}
七 自动装配@EnableAutoConfiguration
加载META-INF/*.factories
SpringFactoryLoader 执行 import *.factories文件中的类(配置类,利用properties的变量创建对象)
@Configuration //声明配置类
@ConfigurationProperties("db") //引入properties中的变量初始化实例对象
将实例对象保存在ApplicationContext中,可以用AutoWired引入
SpringBoot的web开发
1 静态资源
  a, 默认情况下,SpringBoot会从classpath下的/static,/public,/resources,/META-INF/resources下加载静态资源;
  b, 可以通过修改spring.resources.staticLocations来修改静态资源加载路径;
  eg:spring.resources.static-location=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,classpath:/mydir;
  c, 因为应用是打成jar,所以之前的src/main/webapp不会加载;
2 springmvc集成freemarker
  a,controller 返回 视图名称及model数据;
  b,进入视图解析器,找到配置中的Freemarker的配置对象,找到设置的模板路径
templateLoaderPath:"/WEB-INF/views"
Template template = config.getTemplate("index" + ".ftl")
  c, index.ftl +Model数据,生成html文件,通过response响应给客户; 
3 springboot集成freemarker
  a, 引入spring-boot-starter-freemarker;
  b, springboot对freemarker的配置:
spring.freemarker.enabled = true //是否开启freemarker支持;
spring.freemarker.allow-request-override = false //是否允许request中的属性覆盖model中同名属性,默认false
spring.freemarker.expose-request-attritues = ture //开启request属性expose,默认false
srping.freemarker.allow-session-override = false //是否允许session中的属性覆盖model中同名属性,默认false
spring.freemarker.expose-session-attributes=ture //开启session属性expose,默认false
spring.freemarker.cache = false //是否支持模板缓存,默认false
spring.freemarker.charset=UTF-8 //模板编码
spring.freemarker.content-type=text/html //模板contenttype
spring.freemarker.prefix="**" //加载模板时的前缀
srping.freemarker.suffix="**" //加载模板时候的后缀
spring.freemarker.settings.*="**" //直接配置freemarker的参数
spring.freemarker.template-loader-path=classpath:/templates/ //模板加载地址
九 springboot对Controller层统一处理异常
1 使用@ControllerAdvice定义统一的异常处理类,而不是在每个Controller中逐个定义
  @ExceptionHandler用来定义函数针对的异常类型
  eg: @ControllerAdvice
  public class ErrControllerAdvice
  {
@ExceptionHandler(Exception.class)//异常类,可以配置多种异常
public String handlerException(Exception ex, HandlerMethod handlerMethod, Model model)
{
model.setAttribute("errMsg", ex.getMessage());
System.out.println(handlerMethod.getBean().getClass());
System.out.println(handlerMethod.getMethod().getName());//发生异常的方法名
return "error";
}
  }
2 统一的异常页面
  a, springboot默认情况下,把所有错误都重新定位到/error这个处理路径上,由BasicErrorController类完成处理;
  b, springboot提供了默认的替换错误页面的路径
  c, 静态错误页面默认结构
src/resources/public/error/404.html(401.html, 5xx.html)
  d, 也可以使用模板页面
     src/resources/templates/error/404.ftl (401.ftl, 5xx.ftl)
十 servlet配置
1 定义tomcat容器
server.port : 8090 //定制监听端口
server.session.timeout // session有效时间
2 添加servlet组件
方法a, 在配置类上添加@ServletComponentScan 会自动扫描添加了@WebServlet, @WebFilter和@WebListener类
方法b, 在配置类里通过创建ServletListenerRegistrationBean,ServletRegistrationBean和FilterRegistrationBean完成注册;

十一 springboot 上传
1 使用MultipartFile完成,Springboot使用servlet3中的Part对象完成上传,而不是fileupload;
2 上传相关配置
spring.http.multipart.enabled=true //允许上传
spring.http.multipart.maxFileSize=1MB //允许最大的单文件上传大小,单位kb,mb
spring.http.multipart.maxRequestSize=10mb //允许最大请求的大小
3 也可以通过创建MultipartConfigElement的bean来配置上传
4 上传文件的处理
因为应用一般是jar,一般会把上传的文件放到其它位置,目录映射通过spring.resources.static-locations来设置

十二 springboot配置拦截器
1, 创建拦截器类,集成HandlerInterceptorAdapter,重写各种方法;
2 Application config类集成WebMvcConfigurerAdapter,重写addInterceptors方法
3 在addInterceptor中设定拦截的url,registry.addPathPatterns("/*");
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值