SpringBoot简单尝试
阅读原文时间:2022年05月22日阅读:1
配置在类路径下autoconfigure下(多瞅瞅)

@SpringBootApplication里的重要注解(@Configuration,@EnableAutoConfiguration,@ComponentScan三个注解的组合。)

    @ComponentScan:默认就会装配标识了@Controller/@Service/@Component/@Repository注解的类到spring容器中

        @Component是一个元注解,意思是可以注解其他类注解泛指组件,当组件不好归类的时候,我们可以使用这个

        我们可以使用这个注解进行标注,作用就相当于XML配置<bean id="" class=""></bean>

    @EnableAutoConfiguration: 开启自动装配

        @AutoConfigurationPackage:自动配置包

            @Import({Registrar.class}):给容器中导入组件(扫描启动类同级的包及子包的所有组件扫描到spring容器中)
                                                (规定包只能在启动类同级目录)

        @Import({AutoConfigurationImportSelector.class}):导入容器所需要的组件,并配置好组件(使用x组件,配置好x)

    @SpringBootConfiguraztion: 支持JavaConfig的方式来进行配置(使用Configuration配置类等同于XML文件)。

        @Configuration: @configuration标注配置类 = mvc写的配置文件(也是组件)

        @Component:     说明是一个spring组件

Springboot在启动时候从类路径的 META-INF/spring.factories 中获取EnableAutoConfiguration指定的值,将这些值
作为自动配置类导入到容器中

1、基本配置

(1)、POM
//启动器 都以spring-boot-starter开头不用写版本,有版本管理库,定义spring的应用场景,下面是springboot在web的应用场景
(导入web环境所有依赖)
//官网有各种启动器   可以查看pom首个parent(groupId)=》parent(groupId)=》parent 就是springboot版本管理
//                                    (没有的需要手动声名版本)
//可以进去看启动器的具体场景应用依赖
<dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
</dependency>

(2)、静态资源配置
搜索查看WebMvcAutoConfiguration配置类即可看到添加资源处理器(addResourceHandlers),根据源码可以在resources下
创建public和resources优先级为(public < static < resources)

(3)、Ico图标配置
放在static下即可加载

(4)、404,500配置
在templates下创建error文件夹里面放404.html 500.html即可

2、自定义配置(Yml)

//基本语法
yaml:
    person:
    age: 18
    name: 张三
    lis:
        - 第一个
        - 第二个
    mas: {money: 9999999}
    veges:
        color:  绿色
        type: 黄瓜

    指定开发环境:
        1.spring.profiles.active=dev/*激活application-dev.properties*/
        2.yml支持文档块,以---(三个减号)回车分模块  spring:profiles:dev
            spring:profiles:active: dev
    配置文件优先级:
        classpath:==》classpath:config/ ==》file:/==》file:config/

3、日志配置

(1)、POM日志简单配置
logging.file.path:d:/mine    //保存日志
logging.pattern.console        //控制台输入日志格式
logging.pattern.file        //保存日志时候日志的格式
//yml
#指定日志等级并保存日志到path    不加字符直接写格式报错
logging:
  file:
    path: d:/logs
  level:
    cn.bdqn: info
  pattern:
    console: 日志:%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n
    file: 日志:%msg%n

(2)、日志格式
%d表示日期时间
%thread表示线程名
%-5level:级别从左显示5个字符宽度
%logger{50}表示logger名字最长50个字符,否则按照句点分割
%msg:日志消息
%n:换行符
%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n
                                        属性    解释
logging.config=    日志配置文件的位置。例如,classpath:logback.xml。
logging.exception-conversion-word=%wEx    转换异常时使用的转换字。
logging.file=    设置保存日志的日志文件
logging.file.max-history=0    are neat
logging.file.max-size=10MB    设置日志文件最大大小
logging.level.(cn.bdqn)=    设置日志等级
logging.path=    日志文件的位置,例如/var/log
logging.pattern.console=    定义打印的日志格式
logging.pattern.dateformat=yyyy-MM-dd HH:mm:ss.SSS    设置日志日期格式
logging.pattern.file=    定义输出到日志文件的日志格式
logging.register-shutdown-hook=false    当初始化日志系统时,为其注册一个关闭钩子。

4、热部署

热部署                //老实讲,这个玩意有问题,好多时候网页会出错。
先上原理
    深层原理是使用了两个ClassLoader,一个Classloader加载那些不会改变的类(第三方Jar包),另一个ClassLoader
加载会更改的类称为restart ClassLoader,这样在有代码更改的时候,原来的restart ClassLoader 被丢弃,重新创建一个
restart ClassLoader,由于需要加载的类相比较少,所以实现了较快的重启时间。
(1)修改pom文件
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
            <scope>runtime</scope>
        </dependency>

(2)maven插件设置参数也就是在maven打包插件加一个configurattion
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <configuration>
                        <fork>true</fork>
                        <addResources>true</addResources>
                    </configuration>
                </plugin>
            </plugins>
        </build>

(3)编译设置:File->Settings->Build,Execution,Deployment->Compiler:选中 Build project automatically

(4)运行设置:ctrl+shift+a 调起搜索,输入registry,点击第一项,打开Registry编辑框,选中compiler.automake
.allow.when.app.running

**网页热部署**
  spring:
    thymeleaf:
      cache: false

1、常用的th标签

关键字

功能介绍

案例

th:id

替换id

th:text

文本替换

description

th:utext

支持html的文本替换

conten

th:object

替换对象

th:value

属性赋值

th:with

变量赋值运算

th:style

设置样式

th:style="'display:' + @{(${sitrue} ? 'none' : 'inline-block')} + ''"

th:onclick

点击事件

th:onclick="'getCollect()'"

th:each

属性赋值

tr th:each="user,userStat:${users}">

th:if

判断条件

th:unless

和th:if判断相反

Login

th:href

链接地址

Login />

th:switch

多路选择 配合th:case 使用

th:case

th:switch的一个分支

User is an administrator

th:fragment

布局标签,定义一个代码片段,方便其它地方引用

th:include

布局标签,替换内容到引入的文件

/>

th:replace

布局标签,替换整个标签到引入的文件

th:selected

selected选择框 选中

th:selected="(${xxx.id} == ${configObj.dd})"

th:src

图片类地址引入

App Logo

th:inline

定义js脚本可以使用变量

<!--引入静态-->
<img th:src="@{/images/tianshi.png}">

<!--背景url-->
<ul th:style="'background-image: url('+@{/images/tupian.png}+');'">

<!--a标签 通过controller解析-->
<a th:href="${#httpServletRequest.getContextPath()+'/story/chongzhi'}"</a>
<a th:href="@{/story/cz(id=1,money=999.0)}">屠龙宝刀,点击就送</a>

<!--拼接图片-->
<img th:src="${#httpServletRequest.getContextPath()+'/images/'+giveGift.giftImg}">

<!--传递a=1 controller接受也必须int a-->
<!--不行就重启-->
阅读全文'">'"

3、thymeleaf展示数据

<!--a+span-->
<a th:each="AnimeType : ${listAnimeType}" href="">
       <span th:text="${AnimeType.animeTypeName}"></span>
</a>

<!--a+Table-->
<table>
    <tr th:each="Vote : ${listVotePerson}">
        <a th:text="${Vote.voteTitle}"></a>
    </tr>
</table>

<!--遍历单个数据(不用th:each)--->
<span th:text="${voteNeeds.get(0).vote.voteTitle}"></span>

<!--格式化LocalDateTime)--->
${#temporals.format(pageInfo.riqi, 'yyyy-MM-dd HH:mm')}

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章