侧边栏壁纸
  • 累计撰写 781 篇文章
  • 累计创建 1 个标签
  • 累计收到 1 条评论
标签搜索

SpringBoot

Dettan
2021-04-10 / 0 评论 / 0 点赞 / 109 阅读 / 25,086 字
温馨提示:
本文最后更新于 2022-04-30,若内容或图片失效,请留言反馈。部分素材来自网络,若不小心影响到您的利益,请联系我们删除。
优点
1.
内置了servlet容器,可以以jar包形式独立运行.内置Tomcat和Jetty容器
2.
提供了一系列starter简化maven配置
3.
自动装配spring, 会根据在类路径里的jar包,类,为jar包里的类自动配置bean,极大减少配置量
4.
SpringBoot提供基于http ssh telnet对运行时的项目进行监控。提供了一系列大型项目中常见的非功能性特性,如安全、指标,健康检测、外部配置等
5.
极大的提高了开发、部署效率。

常用注解

1.
@RestController@Controller指定一个类,作为控制器的注解 ,并说明其区别
2.
@RequestMapping方法级别的映射注解,这一个用过Spring MVC的小伙伴相信都很熟悉
3.
@EnableAutoConfiguration@SpringBootApplication是类级别的注解,根据maven依赖的jar来自动猜测完成正确的spring的对应配置,只要引入了spring-boot-starter-web的依赖,默认会自动配置Spring MVC和tomcat容器
4.
@Configuration类级别的注解,一般这个注解,我们用来标识main方法所在的类,完成元数据bean的初始化。
5.
@ComponentScan类级别的注解,自动扫描加载所有的Spring组件包括Bean注入,一般用在main方法所在的类上
6.
@ImportResource类级别注解,当我们必须使用一个xml的配置时,使用@ImportResource和@Configuration来标识这个文件资源的类。
7.
@Autowired注解,一般结合@ComponentScan注解,来自动注入一个Service或Dao级别的Bean
8.
@Component类级别注解,用来标识一个组件,比如我自定了一个filter,则需要此注解标识之后,Spring Boot才会正确识别。
9.
springboot是如何管理事务的?直接写@transactional注解就行,在方法就是方法事务,类上就是类事务。

@WebServlet
@WebFilter
@WebListener
用了devTool之后访问jsp页面会直接下载
用maven启动会下载jar包,从源码里直接启动不会自动下载jar包.
@Transactional 事务要加上这个东西.
@ComponentScan 后面啥都不写的话,会自动扫描同低级包下的东西.



Our primary goals are:
Provide a radically faster and widely accessible getting-started experience for all Spring development.
Be opinionated out of the box but get out of the way quickly as requirements start to diverge from the defaults.
Provide a range of non-functional features that are common to large classes of projects (such as embedded servers, security, metrics, health checks, and externalized configuration).
Absolutely no code generation and no requirement for XML configuration.

静态资源路径
- classpath:/META-INF/resources/
- classpath:/resources/
- classpath:/static/
- classpath:/public
启动类
springBoot内嵌tomcat服务器。

配置类
@Bean 在方法前,表示这个方法的返回值 作为Bean归Spring 管。

拦截器
13.2 Maven
Maven users can inherit from the spring-boot-starter-parent project to obtain sensible defaults. The parent project provides the following features:
Java 1.8 as the default compiler level.
UTF-8 source encoding.
Dependency Management section
, inherited from the spring-boot-dependencies pom, that manages the versions of common dependencies. This dependency management lets you omit <version> tags for those dependencies when used in your own pom.
An execution of the repackage goalrepackagewith aexecution id.
Sensible resource filtering.
Sensible plugin configuration (exec pluginGit commit IDshade)., and
Sensible resource filtering for application.propertiesapplication.ymlapplication-dev.propertiesapplication-dev.yml)
and including profile-specific files (for example,and
Note that, since the application.properties and application.yml files accept Spring style placeholders (${…​}), the Maven filtering is changed to use @..@placeholders. (You can override that by setting a Maven property called resource.delimiter.)

启动器


下面的清单显示了一个典型的布局:
com
 +- example
     +- myapplication
         +- Application.java
         |
         +- customer
         |   +- Customer.java
         |   +- CustomerController.java
         |   +- CustomerService.java
         |   +- CustomerRepository.java
         |
         +- order
             +- Order.java
             +- OrderController.java
             +- OrderService.java
             +- OrderRepository.java

属性注入方式

Bean注入
@Autowired注入
@Autowired
private JdbcProperties prop;
  • 构造函数注入
private JdbcProperties prop;
public JdbcConfig(Jdbcproperties prop){
    this.prop = prop;
}
  • 声明有@Bean的方法参数注入
@Bean
public Datasource dataSource(JdbcProperties prop){
    // ...
}</code></pre></div>            <div style="width: 100%; max-width: 996px; margin-top: 2px;">
                <div style="color: inherit; fill: inherit;">
                    <div style="display: flex;">
<div data-root="true" style="max-width: 100%; width: 100%; white-space: pre-wrap; word-break: break-word; padding: 3px 2px;"><br></div></div>
                        <div style="padding-left: 1.5em; display: flex; flex-direction: column;">
</div>
</div>
</div>
            <div style="width: 100%; max-width: 996px; margin-top: 2px;">
                <div style="color: inherit; fill: inherit;">
                    <div style="display: flex;">
<div data-root="true" style="max-width: 100%; width: 100%; white-space: pre-wrap; word-break: break-word; padding: 3px 2px;">@SpringBootApplication </div></div>
                        <div style="padding-left: 1.5em; display: flex; flex-direction: column;">
</div>
</div>
</div>
            <div style="width:100%;max-width:996px;margin-top:1px;">
                <div style="display:flex;align-items:flex-start;width:100%;padding-left:2px;color:inherit;">
                    <div style="margin-right:2px;width:24px;display:flex;align-items:center;justify-content:center;flex-grow:0;flex-shrink:0;min-height:calc((1.5em + 3px) + 3px);">
<div style="font-size:1.5em;line-height:1;margin-bottom:0.1em;">•</div>
</div>
                    <div style="flex: 1 1 0;min-width:1px;display:flex;flex-direction:column;">
                        <div style="display: flex;">
                            <div data-root="true" style="max-width:100%;width:100%;white-space:unset;word-break:break-word;padding:3px 2px;text-align:left;">
<span style="                        /*code*/
font-size:85%;border-radius:3px;padding:0.2em 0.4em;color:#EB5757;line-height:normal;">@EnableAutoConfiguration</span>: enable <span style="">Spring Boot’s auto-configuration mechanism</span></div>
</div>
</div>
</div>
</div>
            <div style="width:100%;max-width:996px;margin-top:1px;">
                <div style="display:flex;align-items:flex-start;width:100%;padding-left:2px;color:inherit;">
                    <div style="margin-right:2px;width:24px;display:flex;align-items:center;justify-content:center;flex-grow:0;flex-shrink:0;min-height:calc((1.5em + 3px) + 3px);">
<div style="font-size:1.5em;line-height:1;margin-bottom:0.1em;">•</div>
</div>
                    <div style="flex: 1 1 0;min-width:1px;display:flex;flex-direction:column;">
                        <div style="display: flex;">
                            <div data-root="true" style="max-width:100%;width:100%;white-space:unset;word-break:break-word;padding:3px 2px;text-align:left;">
<span style="                        /*code*/
font-size:85%;border-radius:3px;padding:0.2em 0.4em;color:#EB5757;line-height:normal;">@ComponentScan</span>: enable <span style="                        /*code*/
font-size:85%;border-radius:3px;padding:0.2em 0.4em;color:#EB5757;line-height:normal;">@Component</span> scan on the package where the application is located (see <span style="">the best practices</span>)</div>
</div>
</div>
</div>
</div>
            <div style="width:100%;max-width:996px;margin-top:1px;">
                <div style="display:flex;align-items:flex-start;width:100%;padding-left:2px;color:inherit;">
                    <div style="margin-right:2px;width:24px;display:flex;align-items:center;justify-content:center;flex-grow:0;flex-shrink:0;min-height:calc((1.5em + 3px) + 3px);">
<div style="font-size:1.5em;line-height:1;margin-bottom:0.1em;">•</div>
</div>
                    <div style="flex: 1 1 0;min-width:1px;display:flex;flex-direction:column;">
                        <div style="display: flex;">
                            <div data-root="true" style="max-width:100%;width:100%;white-space:unset;word-break:break-word;padding:3px 2px;text-align:left;">
<span style="                        /*code*/
font-size:85%;border-radius:3px;padding:0.2em 0.4em;color:#EB5757;line-height:normal;">@Configuration</span>: allow to register extra beans in the context or import additional configuration classes</div>
</div>
</div>
</div>
</div>
            <div style="width: 100%; max-width: 996px; margin-top: 2px;">
                <div style="color: inherit; fill: inherit;">
                    <div style="display: flex;">
<div data-root="true" style="max-width: 100%; width: 100%; white-space: pre-wrap; word-break: break-word; padding: 3px 2px;"><br></div></div>
                        <div style="padding-left: 1.5em; display: flex; flex-direction: column;">
</div>
</div>
</div>
            <div style="width: 100%; max-width: 996px; margin-top: 2px;">
                <div style="color: inherit; fill: inherit;">
                    <div style="display: flex;">
<div data-root="true" style="max-width: 100%; width: 100%; white-space: pre-wrap; word-break: break-word; padding: 3px 2px;"><br></div></div>
                        <div style="padding-left: 1.5em; display: flex; flex-direction: column;">
</div>
</div>
</div>
            <div style="width: 100%; max-width: 996px; margin-top: 2px;">
                <div style="color: inherit; fill: inherit;">
                    <div style="display: flex;">
<div data-root="true" style="max-width: 100%; width: 100%; white-space: pre-wrap; word-break: break-word; padding: 3px 2px;"><br></div></div>
                        <div style="padding-left: 1.5em; display: flex; flex-direction: column;">
</div>
</div>
</div>
            <div style="width:100%;max-width:996px;margin-top:2em;margin-bottom:4px;">
                <div style="display:flex;width:100%;font-weight:600;font-size:1.875em;line-height:1.3;color:inherit;">
                    <div data-root="true" style="max-width:100%;width:100%;white-space:unset;word-break:break-word;padding:3px 2px;">
<span style="font-weight:600;">24.3 Application Property Files</span></div>
</div>
</div>
            <div style="width: 100%; max-width: 996px; margin-top: 2px;">
                <div style="color: inherit; fill: inherit;">
                    <div style="display: flex;">
<div data-root="true" style="max-width: 100%; width: 100%; white-space: pre-wrap; word-break: break-word; padding: 3px 2px;">原先spring.xml里的配置都 转到  application.properties里了。</div></div>
                        <div style="padding-left: 1.5em; display: flex; flex-direction: column;">
</div>
</div>
</div>
            <div style="width: 100%; max-width: 996px; margin-top: 2px;">
                <div style="color: inherit; fill: inherit;">
                    <div style="display: flex;">
<div data-root="true" style="max-width: 100%; width: 100%; white-space: pre-wrap; word-break: break-word; padding: 3px 2px;"><span style="                        /*code*/
font-size:85%;border-radius:3px;padding:0.2em 0.4em;color:#EB5757;line-height:normal;">SpringApplication</span> loads properties from <span style="                        /*code*/
font-size:85%;border-radius:3px;padding:0.2em 0.4em;color:#EB5757;line-height:normal;">application.properties</span> files in the following locations and adds them to the Spring <span style="                        /*code*/
font-size:85%;border-radius:3px;padding:0.2em 0.4em;color:#EB5757;line-height:normal;">Environment</span>:</div></div>
                        <div style="padding-left: 1.5em; display: flex; flex-direction: column;">
</div>
</div>
</div>
            <div style="width:100%;max-width:996px;margin-top:1px;margin-bottom:1px;">
                <div style="display:flex;align-items:flex-start;width:100%;padding-left:2px;">
                    <div style="margin-right:2px;width:24px;display:flex;align-items:center;justify-content:center;flex-grow:0;flex-shrink:0;min-height:calc((1.5em + 3px) + 3px);">
                        <span>1.</span>
</div>
                    <div style="flex: 1 1 0;min-width:1px;display:flex;flex-direction:column;">
                        <div style="display: flex;">
                            <div data-root="true" style="max-width:100%;width:100%;white-space:unset;word-break:break-word;padding:3px 2px;text-align:left;">
A <span style="                        /*code*/
font-size:85%;border-radius:3px;padding:0.2em 0.4em;color:#EB5757;line-height:normal;">/config</span> subdirectory of the current directory</div>
</div>
</div>
</div>
</div>
            <div style="width:100%;max-width:996px;margin-top:1px;margin-bottom:1px;">
                <div style="display:flex;align-items:flex-start;width:100%;padding-left:2px;">
                    <div style="margin-right:2px;width:24px;display:flex;align-items:center;justify-content:center;flex-grow:0;flex-shrink:0;min-height:calc((1.5em + 3px) + 3px);">
                        <span>2.</span>
</div>
                    <div style="flex: 1 1 0;min-width:1px;display:flex;flex-direction:column;">
                        <div style="display: flex;">
                            <div data-root="true" style="max-width:100%;width:100%;white-space:unset;word-break:break-word;padding:3px 2px;text-align:left;">
The current directory</div>
</div>
</div>
</div>
</div>
            <div style="width:100%;max-width:996px;margin-top:1px;margin-bottom:1px;">
                <div style="display:flex;align-items:flex-start;width:100%;padding-left:2px;">
                    <div style="margin-right:2px;width:24px;display:flex;align-items:center;justify-content:center;flex-grow:0;flex-shrink:0;min-height:calc((1.5em + 3px) + 3px);">
                        <span>3.</span>
</div>
                    <div style="flex: 1 1 0;min-width:1px;display:flex;flex-direction:column;">
                        <div style="display: flex;">
                            <div data-root="true" style="max-width:100%;width:100%;white-space:unset;word-break:break-word;padding:3px 2px;text-align:left;">
A classpath <span style="                        /*code*/
font-size:85%;border-radius:3px;padding:0.2em 0.4em;color:#EB5757;line-height:normal;">/config</span> package</div>
</div>
</div>
</div>
</div>
            <div style="width:100%;max-width:996px;margin-top:1px;margin-bottom:1px;">
                <div style="display:flex;align-items:flex-start;width:100%;padding-left:2px;">
                    <div style="margin-right:2px;width:24px;display:flex;align-items:center;justify-content:center;flex-grow:0;flex-shrink:0;min-height:calc((1.5em + 3px) + 3px);">
                        <span>4.</span>
</div>
                    <div style="flex: 1 1 0;min-width:1px;display:flex;flex-direction:column;">
                        <div style="display: flex;">
                            <div data-root="true" style="max-width:100%;width:100%;white-space:unset;word-break:break-word;padding:3px 2px;text-align:left;">
The classpath root</div>
</div>
</div>
</div>
</div>
            <div style="width: 100%; max-width: 996px; margin-top: 2px;">
                <div style="color: inherit; fill: inherit;">
                    <div style="display: flex;">
<div data-root="true" style="max-width: 100%; width: 100%; white-space: pre-wrap; word-break: break-word; padding: 3px 2px;"><br></div></div>
                        <div style="padding-left: 1.5em; display: flex; flex-direction: column;">
</div>
</div>
</div>
            <div style="width: 100%; max-width: 996px; margin-top: 2px;">
                <div style="color: inherit; fill: inherit;">
                    <div style="display: flex;">
<div data-root="true" style="max-width: 100%; width: 100%; white-space: pre-wrap; word-break: break-word; padding: 3px 2px;">same to </div></div>
                        <div style="padding-left: 1.5em; display: flex; flex-direction: column;">
</div>
</div>
</div>
            <div style="width: 100%; max-width: 996px; margin-top: 2px;">
                <div style="color: inherit; fill: inherit;">
                    <div style="display: flex;">
<div data-root="true" style="max-width: 100%; width: 100%; white-space: pre-wrap; word-break: break-word; padding: 3px 2px;"><br></div></div>
                        <div style="padding-left: 1.5em; display: flex; flex-direction: column;">
</div>
</div>
</div>
            <div style="width:100%;max-width:996px;margin-top:1px;margin-bottom:1px;">
                <div style="display:flex;align-items:flex-start;width:100%;padding-left:2px;">
                    <div style="margin-right:2px;width:24px;display:flex;align-items:center;justify-content:center;flex-grow:0;flex-shrink:0;min-height:calc((1.5em + 3px) + 3px);">
                        <span>1.</span>
</div>
                    <div style="flex: 1 1 0;min-width:1px;display:flex;flex-direction:column;">
                        <div style="display: flex;">
                            <div data-root="true" style="max-width:100%;width:100%;white-space:unset;word-break:break-word;padding:3px 2px;text-align:left;">
<span style="                        /*code*/
font-size:85%;border-radius:3px;padding:0.2em 0.4em;color:#EB5757;line-height:normal;">file:./config/</span></div>
</div>
</div>
</div>
</div>
            <div style="width:100%;max-width:996px;margin-top:1px;margin-bottom:1px;">
                <div style="display:flex;align-items:flex-start;width:100%;padding-left:2px;">
                    <div style="margin-right:2px;width:24px;display:flex;align-items:center;justify-content:center;flex-grow:0;flex-shrink:0;min-height:calc((1.5em + 3px) + 3px);">
                        <span>2.</span>
</div>
                    <div style="flex: 1 1 0;min-width:1px;display:flex;flex-direction:column;">
                        <div style="display: flex;">
                            <div data-root="true" style="max-width:100%;width:100%;white-space:unset;word-break:break-word;padding:3px 2px;text-align:left;">
<span style="                        /*code*/
font-size:85%;border-radius:3px;padding:0.2em 0.4em;color:#EB5757;line-height:normal;">file:./</span></div>
</div>
</div>
</div>
</div>
            <div style="width:100%;max-width:996px;margin-top:1px;margin-bottom:1px;">
                <div style="display:flex;align-items:flex-start;width:100%;padding-left:2px;">
                    <div style="margin-right:2px;width:24px;display:flex;align-items:center;justify-content:center;flex-grow:0;flex-shrink:0;min-height:calc((1.5em + 3px) + 3px);">
                        <span>3.</span>
</div>
                    <div style="flex: 1 1 0;min-width:1px;display:flex;flex-direction:column;">
                        <div style="display: flex;">
                            <div data-root="true" style="max-width:100%;width:100%;white-space:unset;word-break:break-word;padding:3px 2px;text-align:left;">
<span style="                        /*code*/
font-size:85%;border-radius:3px;padding:0.2em 0.4em;color:#EB5757;line-height:normal;">classpath:/config/</span></div>
</div>
</div>
</div>
</div>
            <div style="width:100%;max-width:996px;margin-top:1px;margin-bottom:1px;">
                <div style="display:flex;align-items:flex-start;width:100%;padding-left:2px;">
                    <div style="margin-right:2px;width:24px;display:flex;align-items:center;justify-content:center;flex-grow:0;flex-shrink:0;min-height:calc((1.5em + 3px) + 3px);">
                        <span>4.</span>
</div>
                    <div style="flex: 1 1 0;min-width:1px;display:flex;flex-direction:column;">
                        <div style="display: flex;">
                            <div data-root="true" style="max-width:100%;width:100%;white-space:unset;word-break:break-word;padding:3px 2px;text-align:left;">
<span style="                        /*code*/
font-size:85%;border-radius:3px;padding:0.2em 0.4em;color:#EB5757;line-height:normal;">classpath:/</span></div>
</div>
</div>
</div>
</div>
            <div style="width: 100%; max-width: 996px; margin-top: 2px;">
                <div style="color: inherit; fill: inherit;">
                    <div style="display: flex;">
<div data-root="true" style="max-width: 100%; width: 100%; white-space: pre-wrap; word-break: break-word; padding: 3px 2px;"><br></div></div>
                        <div style="padding-left: 1.5em; display: flex; flex-direction: column;">
</div>
</div>
</div>
            <div style="width: 100%; max-width: 996px; margin-top: 2px;">
                <div style="color: inherit; fill: inherit;">
                    <div style="display: flex;">
<div data-root="true" style="max-width: 100%; width: 100%; white-space: pre-wrap; word-break: break-word; padding: 3px 2px;"><br></div></div>
                        <div style="padding-left: 1.5em; display: flex; flex-direction: column;">
</div>
</div>
</div>
            <div style="width: 100%; max-width: 996px; margin-top: 2px;">
                <div style="color: inherit; fill: inherit;">
                    <div style="display: flex;">
<div data-root="true" style="max-width: 100%; width: 100%; white-space: pre-wrap; word-break: break-word; padding: 3px 2px;"><br></div></div>
                        <div style="padding-left: 1.5em; display: flex; flex-direction: column;">
</div>
</div>
</div>
            <div style="width:100%;max-width:996px;margin-top:1em;margin-bottom:1px;">
                <div style="display:flex;width:100%;font-weight:600;font-size:1.25em;line-height:1.3;color:inherit;">
                    <div data-root="true" style="max-width:100%;width:100%;white-space:unset;word-break:break-word;padding:3px 2px;">
<span style="font-weight:600;">4.1.2.@EnableAutoConfiguration</span></div>
</div>
</div>
            <div style="width: 100%; max-width: 996px; margin-top: 2px;">
                <div style="color: inherit; fill: inherit;">
                    <div style="display: flex;">
<div data-root="true" style="max-width: 100%; width: 100%; white-space: pre-wrap; word-break: break-word; padding: 3px 2px;">关于这个注解,官网上有一段说明:</div></div>
                        <div style="padding-left: 1.5em; display: flex; flex-direction: column;">
</div>
</div>
</div>
            <div style="width: 100%; max-width: 996px; margin-top: 2px;">
                <div style="color: inherit; fill: inherit;">
                    <div style="display: flex;">
<div data-root="true" style="max-width: 100%; width: 100%; white-space: pre-wrap; word-break: break-word; padding: 3px 2px;">&gt; The second class-level annotation is `@EnableAutoConfiguration`. This annotation&gt; tells Spring Boot to “guess” how you want to configure Spring, based on the jar&gt; dependencies that you have added. Since `spring-boot-starter-web` added Tomcat&gt; and Spring MVC, the auto-configuration assumes that you are developing a web&gt; application and sets up Spring accordingly.</div></div>
                        <div style="padding-left: 1.5em; display: flex; flex-direction: column;">
</div>
</div>
</div>
            <div style="width: 100%; max-width: 996px; margin-top: 2px;">
                <div style="color: inherit; fill: inherit;">
                    <div style="display: flex;">
<div data-root="true" style="max-width: 100%; width: 100%; white-space: pre-wrap; word-break: break-word; padding: 3px 2px;">简单翻译以下:</div></div>
                        <div style="padding-left: 1.5em; display: flex; flex-direction: column;">
</div>
</div>
</div>
            <div style="width: 100%; max-width: 996px; margin-top: 2px;">
                <div style="color: inherit; fill: inherit;">
                    <div style="display: flex;">
<div data-root="true" style="max-width: 100%; width: 100%; white-space: pre-wrap; word-break: break-word; padding: 3px 2px;">&gt; 第二级的注解`@EnableAutoConfiguration`,告诉SpringBoot基于你所添加的依赖,去“猜测”你想要如何配置Spring。比如我们引入了`spring-boot-starter-web`,而这个启动器中帮我们添加了`tomcat`、`SpringMVC`的依赖。此时自动配置就知道你是要开发一个web应用,所以就帮你完成了web及SpringMVC的默认配置了!总结,SpringBoot内部对大量的第三方库或Spring内部库进行了默认配置,这些配置是否生效,取决于我们是否引入了对应库所需的依赖,如果有那么默认配置就会生效。所以,我们使用SpringBoot构建一个项目,只需要引入所需框架的依赖,配置就可以交给SpringBoot处理了。除非你不希望使用SpringBoot的默认配置,它也提供了自定义配置的入口。</div></div>
                        <div style="padding-left: 1.5em; display: flex; flex-direction: column;">
</div>
</div>
</div>
            <div style="width: 100%; max-width: 996px; margin-top: 2px;">
                <div style="color: inherit; fill: inherit;">
                    <div style="display: flex;">
<div data-root="true" style="max-width: 100%; width: 100%; white-space: pre-wrap; word-break: break-word; padding: 3px 2px;"><br></div></div>
                        <div style="padding-left: 1.5em; display: flex; flex-direction: column;">
</div>
</div>
</div>
            <div style="width: 100%; max-width: 996px; margin-top: 2px;">
                <div style="color: inherit; fill: inherit;">
                    <div style="display: flex;">
<div data-root="true" style="max-width: 100%; width: 100%; white-space: pre-wrap; word-break: break-word; padding: 3px 2px;"><br></div></div>
                        <div style="padding-left: 1.5em; display: flex; flex-direction: column;">
</div>
</div>
</div>
</div>
0

评论区