《SpringBoot实战》笔记1

1.1.2 Spring Boot精要

自动配置:针对很多Spring应用程序常见的应用功能,SpringBoot能提供相关配置。

起步依赖:告诉SpringBoot需要什么功能,他就能引入需要的库。

命令行界面:SPringBoot的可选特性,借此只需写代码就能完成完整的应用程序。

Actuator:让你能深入运行中的SpringBoot程序,一探究竟。

1.自动配置

@Bean 
public JdbcTemplate jdbcTemplate(DataSource dataSource) 
{   return new JdbcTemplate(dataSource); } 

这段非常简单的Bean声明创建了一个JdbcTemplate的实例,注入了一个DataSource依赖。
当 然,这意味着你还需要配置一个DataSource的Bean,这样才能满足依赖。
假设你将配置一个嵌 入式H2数据库作为DataSource Bean,完成这个配置场景的代码大概是这样的

@Bean 
public DataSource dataSource() {   
return new EmbeddedDatabaseBuilder()           
.setType(EmbeddedDatabaseType.H2)           
.addScripts('schema.sql', 'data.sql')           .
build(); } 

这个Bean配置方法创建了一个嵌入式数据库,并指定在该数据库上执行两段SQL脚本。
build() 方法返回了一个指向该数据库的引用。

这两个Bean配置方法都不复杂,也不是很长,但它们只是典型Spring应用程序配置的一小部 分。
除此之外,还有无数Spring应用程序有着完全相同的方法。
所有需要用到嵌入式数据库和 JdbcTemplate的应用程序都会用到那些方法。
简而言之,这就是一个样板配置。

《SpringBoot实战》笔记1_第1张图片


<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0modelVersion>

    <groupId>com.springBootgroupId>
    <artifactId>springboot01artifactId>
    <version>1.0-SNAPSHOTversion>

    
    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>2.0.0.RELEASEversion>
        <relativePath/> 
    parent>

    
    <properties>
        <java.version>1.8java.version>
    properties>

    
    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>
    dependencies>

project>
package com.springboot;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ Author     :ShaoWei Sun.
 * @ Date       :Created in 10:53 2018/11/27
 */
@RestController
public class HelloController {
    //@GetMapping("/hello")
    @RequestMapping("/hello")
    public String hello() {
        return "SpringBoot 第一个程序";
    }

}
package com.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @ Author     :ShaoWei Sun.
 * @ Date       :Created in 10:52 2018/11/27
 */
//@SpringBootApplication==@Configuration,@EnableAutoConfiguration,@ComponentScan
//扫包范围:在启动类上加上@SpringBootApplication注解,当前包下或者子包下所有的类都可以扫到
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class,args);
    }
}

运行main函数,后台信息

"C:\Program Files\Java\jdk1.8.0_221\bin\java.exe" -XX:TieredStopAtLevel=1 -noverify -Dspring.output.ansi.enabled=always -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=50580 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Djava.rmi.server.hostname=localhost -Dspring.liveBeansView.mbeanDomain -Dspring.application.admin.enabled=true "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2018.3.1\lib\idea_rt.jar=50581:C:\Program Files\JetBrains\IntelliJ IDEA 2018.3.1\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_221\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_221\jre\lib\rt.jar;D:\myWorkplace\springboot01\target\classes;D:\Java\maven-repository\repository\org\springframework\boot\spring-boot-starter-web\2.0.0.RELEASE\spring-boot-starter-web-2.0.0.RELEASE.jar;D:\Java\maven-repository\repository\org\springframework\boot\spring-boot-starter\2.0.0.RELEASE\spring-boot-starter-2.0.0.RELEASE.jar;D:\Java\maven-repository\repository\org\springframework\boot\spring-boot\2.0.0.RELEASE\spring-boot-2.0.0.RELEASE.jar;D:\Java\maven-repository\repository\org\springframework\boot\spring-boot-autoconfigure\2.0.0.RELEASE\spring-boot-autoconfigure-2.0.0.RELEASE.jar;D:\Java\maven-repository\repository\org\springframework\boot\spring-boot-starter-logging\2.0.0.RELEASE\spring-boot-starter-logging-2.0.0.RELEASE.jar;D:\Java\maven-repository\repository\ch\qos\logback\logback-classic\1.2.3\logback-classic-1.2.3.jar;D:\Java\maven-repository\repository\ch\qos\logback\logback-core\1.2.3\logback-core-1.2.3.jar;D:\Java\maven-repository\repository\org\slf4j\slf4j-api\1.7.25\slf4j-api-1.7.25.jar;D:\Java\maven-repository\repository\org\apache\logging\log4j\log4j-to-slf4j\2.10.0\log4j-to-slf4j-2.10.0.jar;D:\Java\maven-repository\repository\org\apache\logging\log4j\log4j-api\2.10.0\log4j-api-2.10.0.jar;D:\Java\maven-repository\repository\org\slf4j\jul-to-slf4j\1.7.25\jul-to-slf4j-1.7.25.jar;D:\Java\maven-repository\repository\javax\annotation\javax.annotation-api\1.3.2\javax.annotation-api-1.3.2.jar;D:\Java\maven-repository\repository\org\springframework\spring-core\5.0.4.RELEASE\spring-core-5.0.4.RELEASE.jar;D:\Java\maven-repository\repository\org\springframework\spring-jcl\5.0.4.RELEASE\spring-jcl-5.0.4.RELEASE.jar;D:\Java\maven-repository\repository\org\yaml\snakeyaml\1.19\snakeyaml-1.19.jar;D:\Java\maven-repository\repository\org\springframework\boot\spring-boot-starter-json\2.0.0.RELEASE\spring-boot-starter-json-2.0.0.RELEASE.jar;D:\Java\maven-repository\repository\com\fasterxml\jackson\core\jackson-databind\2.9.4\jackson-databind-2.9.4.jar;D:\Java\maven-repository\repository\com\fasterxml\jackson\core\jackson-annotations\2.9.0\jackson-annotations-2.9.0.jar;D:\Java\maven-repository\repository\com\fasterxml\jackson\core\jackson-core\2.9.4\jackson-core-2.9.4.jar;D:\Java\maven-repository\repository\com\fasterxml\jackson\datatype\jackson-datatype-jdk8\2.9.4\jackson-datatype-jdk8-2.9.4.jar;D:\Java\maven-repository\repository\com\fasterxml\jackson\datatype\jackson-datatype-jsr310\2.9.4\jackson-datatype-jsr310-2.9.4.jar;D:\Java\maven-repository\repository\com\fasterxml\jackson\module\jackson-module-parameter-names\2.9.4\jackson-module-parameter-names-2.9.4.jar;D:\Java\maven-repository\repository\org\springframework\boot\spring-boot-starter-tomcat\2.0.0.RELEASE\spring-boot-starter-tomcat-2.0.0.RELEASE.jar;D:\Java\maven-repository\repository\org\apache\tomcat\embed\tomcat-embed-core\8.5.28\tomcat-embed-core-8.5.28.jar;D:\Java\maven-repository\repository\org\apache\tomcat\embed\tomcat-embed-el\8.5.28\tomcat-embed-el-8.5.28.jar;D:\Java\maven-repository\repository\org\apache\tomcat\embed\tomcat-embed-websocket\8.5.28\tomcat-embed-websocket-8.5.28.jar;D:\Java\maven-repository\repository\org\hibernate\validator\hibernate-validator\6.0.7.Final\hibernate-validator-6.0.7.Final.jar;D:\Java\maven-repository\repository\javax\validation\validation-api\2.0.1.Final\validation-api-2.0.1.Final.jar;D:\Java\maven-repository\repository\org\jboss\logging\jboss-logging\3.3.2.Final\jboss-logging-3.3.2.Final.jar;D:\Java\maven-repository\repository\com\fasterxml\classmate\1.3.4\classmate-1.3.4.jar;D:\Java\maven-repository\repository\org\springframework\spring-web\5.0.4.RELEASE\spring-web-5.0.4.RELEASE.jar;D:\Java\maven-repository\repository\org\springframework\spring-beans\5.0.4.RELEASE\spring-beans-5.0.4.RELEASE.jar;D:\Java\maven-repository\repository\org\springframework\spring-webmvc\5.0.4.RELEASE\spring-webmvc-5.0.4.RELEASE.jar;D:\Java\maven-repository\repository\org\springframework\spring-aop\5.0.4.RELEASE\spring-aop-5.0.4.RELEASE.jar;D:\Java\maven-repository\repository\org\springframework\spring-context\5.0.4.RELEASE\spring-context-5.0.4.RELEASE.jar;D:\Java\maven-repository\repository\org\springframework\spring-expression\5.0.4.RELEASE\spring-expression-5.0.4.RELEASE.jar" com.springboot.Application . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.0.0.RELEASE) 2020-04-24 08:54:54.907 INFO 9756 --- [ main] com.springboot.Application : Starting Application on DESKTOP-4UFCOPC with PID 9756 (D:\myWorkplace\springboot01\target\classes started by admin in D:\myWorkplace\springboot01) 2020-04-24 08:54:54.907 INFO 9756 --- [ main] com.springboot.Application : No active profile set, falling back to default profiles: default 2020-04-24 08:54:54.937 INFO 9756 --- [ main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@2cbb3d47: startup date [Fri Apr 24 08:54:54 CST 2020]; root of context hierarchy 2020-04-24 08:54:55.487 INFO 9756 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http) 2020-04-24 08:54:55.497 INFO 9756 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2020-04-24 08:54:55.497 INFO 9756 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet Engine: Apache Tomcat/8.5.28 2020-04-24 08:54:55.507 INFO 9756 --- [ost-startStop-1] o.a.catalina.core.AprLifecycleListener : The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: [C:\Program Files\Java\jdk1.8.0_221\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;D:\Program Files (x86)\maven\apache-maven-3.6.3-bin\apache-maven-3.6.3\bin;C:\Program Files\Java\jdk1.8.0_221\bin;C:\Program Files\Git\cmd;C:\Program Files\TortoiseGit\bin;C:\Program Files\nodejs\;C:\Program Files\JetBrains\IntelliJ IDEA 2018.3.1\plugins\maven\lib\maven3\bin;C:\Users\admin\AppData\Local\Microsoft\WindowsApps;C:\Users\admin\AppData\Roaming\npm;.] 2020-04-24 08:54:55.557 INFO 9756 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2020-04-24 08:54:55.557 INFO 9756 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 620 ms 2020-04-24 08:54:55.627 INFO 9756 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean : Servlet dispatcherServlet mapped to [/] 2020-04-24 08:54:55.627 INFO 9756 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'characterEncodingFilter' to: [/*] 2020-04-24 08:54:55.627 INFO 9756 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'hiddenHttpMethodFilter' to: [/*] 2020-04-24 08:54:55.627 INFO 9756 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'httpPutFormContentFilter' to: [/*] 2020-04-24 08:54:55.627 INFO 9756 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean : Mapping filter: 'requestContextFilter' to: [/*] 2020-04-24 08:54:55.797 INFO 9756 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@2cbb3d47: startup date [Fri Apr 24 08:54:54 CST 2020]; root of context hierarchy 2020-04-24 08:54:55.837 INFO 9756 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/hello]}" onto public java.lang.String com.springboot.HelloController.hello() 2020-04-24 08:54:55.837 INFO 9756 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity

浏览器输入http://localhost:8080/hello

在这里插入图片描述

你可能感兴趣的:(springBoot教程)