aa

1565449407427

SpringBoot简介

1565449407427

1565449431283

1565449442875

微服务

第一个SpringBoot例子

1、maven的一些配置:

​ 指定镜像;

​ 指定本地库路径;

​ 指定编译运行的jdk版本:

<profile>   
    <id>jdk‐1.8</id>   
    <activation>     
        <activeByDefault>true</activeByDefault>     
        <jdk>1.8</jdk>   
    </activation>   
    <properties>     
        <maven.compiler.source>1.8</maven.compiler.source>     					 	 			<maven.compiler.target>1.8</maven.compiler.target>     			 		  				<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>   </properties> 
</profile>

2、创建一个maven工程(jar形式):

1565449080649

​ 不指定骨架;要指定JDK版本;

3、pom.xml中加入相关依赖;

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.6.RELEASE</version>
</parent>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

4、编写一个主程序;

/**
 * @SpringBootApplication 标注一个主程序类,说明这是一个Spring Boot应用
 */
@SpringBootApplication
public class HelloWorldMainApplication {

    public static void main(String[] args) {

        /**
         * 将Spring Boot应用启动起来
         * 已经内嵌了tomcat,不需要再进行指定了;
         */
        SpringApplication.run(HelloWorldMainApplication.class,args);
    }
}

5、编写业务逻辑代码;

@Controller
public class HelloController {
    @ResponseBody  // 要想将数据返回给客户端,就要加这个注解,SpringMVC的注解
    @RequestMapping("/hello")
    public String hello(){
        return "Hello World!";
    }
}

6、运行测试验证:

1565449644600

​ 不需要加项目名;

7、简化部署:

​ 以前需要加应用打成war包,放到tomcat服务器上,如果目标环境没有tomcat,还需要进行安装配置一下等其他操作;

​ SpringBoot应用不需要打成war包,打成可执行的jar包直接运行;

7.1、pom.xml中添加如下 spring boot maven 插件:

<build>
    <plugins>
        <!-- 这个插件,可以将应用打包成一个可执行的jar包;-->
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

7.2、执行打包命令:

1565450268435

1565450275957

​ 上面是打成jar包之后所在的路径;

7.3、进入到jar包所在路径:

1565450815580

空白处输入快捷键:win+R,然后输入cmd进入命令窗口;

切换到对应路径,然后使用java -jar 命令执行:

1565450869488

再在浏览器里面输入之前的请求地址,可以看到请求成功得到响应;

ctrl + c:退出应用;

第一个例子Hello World 探究

1、先看 pom.xml 文件:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.6.RELEASE</version>
</parent>

​ 所有的Spring Boot应用的pom.xml文件中,都引用这个父项目;

​ 进入这个父项目,发现此父项目依赖另一个父项目,如下:

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-dependencies</artifactId>
    <version>2.1.6.RELEASE</version>
    <relativePath>../../spring-boot-dependencies</relativePath>
</parent>

​ 再进入这个父项目看看:

1565623644940

​ 可以看到,这里面定义了每一个依赖的版本;此父项目才是真正管理Spring Boot应用里面的所有依赖版本;可

以将它称为Spring Boot的版本仲裁中心;以后我们导入依赖默认是不需要写版本的,当然没有在dependencies里

面管理的依赖自然需要声明版本号;

2、再看一下启动器:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

​ 将 spring-boot-starter-web 分成两部分来看:spring-boot-starter 和 web;

​ spring-boot-starter:spring-boot场景启动器;帮我们导入了web模块正常运行所依赖的组件(依赖的版本都受到父项目进行仲裁);

​ Spring Boot有很多这样的场景启动器;Spring Boot将所有功能都抽取成一个个分开的功能场景(starter 启动器),你想使用哪个功能,就引入哪个功能对应的场景启动器,然后相关场景的所有依赖都会被引入进来,而且版本由Spring Boot自己自动控制;比如上面的想使用web功能,就引入 spring-boot-starter-web 功能场景启动器;

1565624547456

使用Spring Initializer快速创建Spring Boot项目

1565451480031

1565451490247

1565451498886

​ 根据需要来选择对应的功能服务;

1565451529847

1565451538779

1565451546470

​ 这五个没用可以删掉;

配置嵌入式Servlet容器

SpringBoot默认使用Tomcat作为嵌入式的Servlet容器;

如何修改和定制Servlet容器相关的配置

方法1、修改和 server 相关的配置((ServerProperties【应该也是WebServerFactoryCustomizer】):

server:
  servlet:
    context-path: /curd

// 通用的Servlet容器设置
server.xxx

// Tomcat的设置
server.tomcat.xxx

方法2:编写一个EmbeddedServletContainerCustomizer:嵌入式的Servlet容器的定制器;来修改Servlet容器的 配置:

​ EmbeddedServletContainerCustomizer 在Spring Boot2.0以上不支持了,使用 WebServerFactoryCustomizer 来代替;

// 定制嵌入式的Servlet容器相关规则
    @Bean
    public WebServerFactoryCustomizer<ConfigurableWebServerFactory> webServerFactoryCustomizer() {
        return new WebServerFactoryCustomizer<ConfigurableWebServerFactory>() {
            @Override
            public void customize(ConfigurableWebServerFactory factory) {
                factory.setPort(8081);
            }
        };
    }

​ 在Spring Boot中会有很多的xxxCustomizer帮助我们进行定制配置;

注册 Servlet 三大组建【Servlet、Filter、Listener】

​ 由于创建的SpringBoot应用默认是以jar包的方式启动嵌入式的Servlet容器来启动SpringBoot的web应用,不是标准的web应用目录结构(有/webapp/WEB-INF/web.xml,可以将三大组件注册在web.xml中),没有web.xml文 件;

​ ServletRegistrationBean:

// 注册一个Servlet
    @Bean
    public ServletRegistrationBean myServlet(){
        ServletRegistrationBean registrationBean = new ServletRegistrationBean(new MyServlet(),"/myServlet");
        registrationBean.setLoadOnStartup(1);
        return registrationBean;
    }

​ FilterRegistrationBean:

// 注册一个Filter
    @Bean
    public FilterRegistrationBean myFilter(){
        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        registrationBean.setFilter(new MyFilter());
        registrationBean.setUrlPatterns(Arrays.asList("/filter1","/filter2"));
        return registrationBean;
    }

​ ServletListenerRegistrationBean:

// 注册一个Listener
    @Bean
    public ServletListenerRegistrationBean myListener(){
        ServletListenerRegistrationBean<MyListener> registrationBean = new ServletListenerRegistrationBean<>(new MyListener());
        return registrationBean;
    }

替换为其他嵌入式Servlet容器

​ 默认支持三种Servlet容器:

​ Tomcat:默认使用、Jetty、Undertow

​ 引入Jetty容器:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
        <exclusion>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <groupId>org.springframework.boot</groupId>
        </exclusion>
    </exclusions>
</dependency>

<!-- 引用其他的Servlet容器:Jetty -->
<dependency>
    <artifactId>spring-boot-starter-jetty</artifactId>
    <groupId>org.springframework.boot</groupId>
</dependency>

使用外置的Servlet容器

​ 嵌入式Servlet容器:

​ 应用直接打成可执行的jar包执行;

​ 有点:简单、便携;

​ 缺点:默认不支持JSP、优化和定制比较复杂(使用定制器【ServerProperties、自定义

EmbeddedServletContainerCustomizer】,自己编写嵌入式Servlet容器的创建工厂

【EmbeddedServletContainerFactory】);

​ 外置的Servlet容器:

​ 外面安装一个Servlet容器,以Tomcat为例,应用以war包的方式打包;

创建一个war包形式(需要有webapp相关目录)的SpringBoot应用:

1565534536674

​ 以之前的方式创建Spring Boot应用,只是这里的Packing选为War;

1565534594451

​ 生成的应用的完整目录结构是这样的,还没有webapp相关目录,需要手动创建;

1565534648154

1565534654983

1565534662838

​ 双击;

1565534680360

​ 直接点击OK;

1565534706242

​ 点击Yes,就会创建webapp目录;

1565534728094

​ 点击+号;

1565534748287

​ 将此路径改为:D:\project\sgg_springboot\springboot_14\src\main\webapp\WEB-INF\web.xml;然后点击OK,这样就创建了web.xml文件;

​ 点击Apply后,就可以看到完整的webapp目录;

1565534876233

运行war包形式的Spring Boot项目

​ 可以将应用打成war包放到服务器上运行;也可以把服务器整合进idea里面来启动,这样更方便,本地开发的时候都是这样使用,见下面;

1565535830793

1565535835963

​ 点击+号;

1565535863406

1565535881060

​ 点击+号;

1565535898300

​ 选第一个;

1565535914932

​ 选第二个;

1565535928502

​ 将这里的根路径改一下,只要/即可;

1565535944186

​ 一些配置如上;

1565535952172

​ 创建一个jsp页面;

1565535996529

​ 启动项目;

1565536009639

​ 访问成功;

JDBC:

1566551748375

​ idea中连接mysql时报了一个错,错误描述和解决方法如下:

https://blog.csdn.net/sikefeng/article/details/80200540

​ 上面的错误解决完之后还是没有连接成功,是因为连接属性的字段名选错了:

1566566715304

​ 这里选的时 username 和 password,而不是 data-username 和 password;

​ 启动时可以执行建表sql和插入数据sql,默认只需要将文件命名为:

schema‐*.sql、data‐*.sql 
默认规则:
	schema.sql,schema‐all.sql; 
可以使用    
	schema:            
		‐ classpath:department.sql       
		指定位置

​ Spring Boot2.x 和 Spring Boot1.x 在启动时执行sql语句上的区别:

https://blog.csdn.net/hyunbar/article/details/82967658

1565449407427

posted @ 2019-10-30 21:52  kehuaihan  阅读(387)  评论(0编辑  收藏  举报