继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

Springboot入门教程

幕布斯6054654
关注TA
已关注
手记 1135
粉丝 218
获赞 1009

一、  Springboot简介
Spring Boot是为了简化Spring应用的创建、运行、调试、部署等而出现的,使用它可以做到专注于Spring应用的开发,而无需过多关注XML的配置。 简单来说,它提供了一堆依赖打包,并已经按照使用习惯解决了依赖问题---习惯大于约定。   Spring Boot默认使用tomcat作为服务器,使用logback提供日志记录。     Spring Boot提供了一系列的依赖包,所以需要构建工具的支持:maven 或 gradle。
二、 Springboot启动器
spring-boot-starter
这是Spring Boot的核心启动器,包含了自动配置、日志和YAML。 2)spring-boot-starter-actuator
帮助监控和管理应用。 3)spring-boot-starter-amqp
通过spring-rabbit来支持AMQP协议(Advanced Message Queuing Protocol)。 4)spring-boot-starter-aop
支持面向方面的编程即AOP,包括spring-aop和AspectJ。 5)spring-boot-starter-artemis
通过Apache Artemis支持JMS的API(Java Message Service API)。 6)spring-boot-starter-batch
支持Spring Batch,包括HSQLDB数据库。 7)spring-boot-starter-cache
支持Spring的Cache抽象。 8)spring-boot-starter-cloud-connectors
支持Spring Cloud Connectors,简化了在像Cloud Foundry或Heroku这样的云平台上连接服务。 9)spring-boot-starter-data-elasticsearch
支持ElasticSearch搜索和分析引擎,包括spring-data-elasticsearch。 10)spring-boot-starter-data-gemfire
支持GemFire分布式数据存储,包括spring-data-gemfire。 11)spring-boot-starter-data-jpa
支持JPA(java Persistence API),包括spring-data-jpa、spring-orm、hibernate。 12)spring-boot-starter-data-MongoDB
支持mongodb数据,包括spring-data-mongodb。 13)spring-boot-starter-data-rest
通过spring-data-rest-webmvc,支持通过REST暴露Spring Data数据仓库。 14)spring-boot-starter-data-solr
支持Apache Solr搜索平台,包括spring-data-solr。 15)spring-boot-starter-freemarker
支持FreeMarker模板引擎。 16)spring-boot-starter-groovy-templates
支持Groovy模板引擎。 17)spring-boot-starter-hateoas
通过spring-hateoas支持基于HATEOAS的RESTful Web服务。 18)spring-boot-starter-hornetq
通过HornetQ支持JMS。 19)spring-boot-starter-integration
支持通用的spring-integration模块。 20)spring-boot-starter-jdbc
支持JDBC数据库。 21)spring-boot-starter-jersey
支持Jersey RESTful Web服务框架。 22)spring-boot-starter-jta-atomikos
通过Atomikos支持JTA分布式事务处理。 23)spring-boot-starter-jta-bitronix
通过Bitronix支持JTA分布式事务处理。 24)spring-boot-starter-mail
支持javax.mail模块。 25)spring-boot-starter-mobile
支持spring-mobile。 26)spring-boot-starter-mustache
支持Mustache模板引擎。 27)spring-boot-starter-Redis
支持redis键值存储数据库,包括spring-redis。 28)spring-boot-starter-security
支持spring-security。 29)spring-boot-starter-social-facebook
支持spring-social-facebook 30)spring-boot-starter-social-linkedin
支持pring-social-linkedin 31)spring-boot-starter-social-twitter
支持pring-social-twitter 32)spring-boot-starter-test
支持常规的测试依赖,包括JUnit、Hamcrest、Mockito以及spring-test模块。 33)spring-boot-starter-thymeleaf
支持Thymeleaf模板引擎,包括与Spring的集成。 34)spring-boot-starter-velocity
支持Velocity模板引擎。 35)spring-boot-starter-web
S支持全栈式Web开发,包括Tomcat和spring-webmvc。 36)spring-boot-starter-websocket
支持WebSocket开发。 37)spring-boot-starter-ws
支持Spring Web Services。
三、 一个简单的Springboot示例
Pom.xml配置
Springboot父工程依赖,会引入一些SpringBoot相关的一些依赖和配置,使用SpringBoot必须有这个parent

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

Springboot web启动器依赖(有了该依赖就可以使用SpringMvc相关的功能了)

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

之后写一个启动类即可完成Springboot项目的搭建

/**
 * @SpringBootApplication 相当于 @Configuration,@EnableAutoConfiguration对Configuration注解的扫描,@ComponentScan包扫描 这个注解必须要
*/@SpringBootApplication(scanBasePackages = {"com.yubin"})public class SpringBootStart {       public static void main(String[] args) {
                SpringApplication.run(SpringBootStart.class, args);
       }
}

四、SpringBoot如何实现SpringMvc
1、框架搭建及必要依赖的引入 使用之前的案例
2、新建一个启动类

/**
 * SpringBoot演示用例启动器类
 *
 * @Author YUBIN
 *//**
 * @SpringBootApplication 包含了@Configuration,@EnableAutoConfiguration和@ComponentScan
 * @Configuration 相当于Spring的xml配置文件;使用java代码可以检查类型安全
 * @EnableAutoConfiguration 自动配置
 * @ComponentScan 组件扫描,可自动发现和装配一些Bean
 */@SpringBootApplication(scanBasePackages = {"com.yubin.springboot"}) // scanBasePackages 属性配置包扫描@MapperScan(basePackages = "com.yubin.springboot.dao")@ServletComponentScan // 该注解使得Servlet、Filter、Listener 可以直接通过 @WebServlet、@WebFilter、@WebListener 注解自动注册,无需其他代码public class SpringBootDemoApplication {    public static void main(String[] args) {
        SpringApplication.run(SpringBootDemoApplication.class, args);
    }
}

3、新建一个IndexController类

@Controllerpublic class IndexController {

   @RequestMapping(value = {"/index","/show"},method = RequestMethod.GET) // 访问链接可以是index,也可以是show method属性现在请求方式
   @ResponseBody // 响应json格式的数据
   public Map<String, String> index() {
       Map<String, String> map = new HashMap<>();       map.put("俞斌", "是帅哥GET");       map.put("你好", "SpringBoot好方便");       return map;
   }
}

4、运行启动类测试
项目通过main方法启动之后,就可以进行访问了 http://localhost:8080/index
默认什么配置都没有的话,根路径为"/",端口号是 8080

webp

image.png


至此:SpringBoot下使用SpringMvc的简单案例就完成了

5、优化
(1)、在类上面使用@RestController就不需要再每个方法上都写上@ResponseBody注解了
这个注解是Spring4.0之后出来的 包含了@Controller和@ResponseBody两个注解


webp

image.png

(2)、方法上映射方式的注解@RequestMappint
一般我们在使用@RequestMapping的时候会限制该方法的请求方式GET或者POST,每次都需要在RequestMapping中书写method属性,会显得很麻烦,有没有好一点的办法呢?答案是肯定的
GET请求可以使用 @GetMapping


webp

image.png

POST请求可以使用 @PostMapping


webp

image.png

上面的这些注解是Spring4.3之后出来的
演示:

webp

image.png


六、  项目端口号设置
在0配置的情况下,项目的端口号默认是8080,可以在ServerProperties这个类中进行查看

webp

image.png


webp

image.png


进入ConfigurableEmbeddedServletContainer类

webp

image.png


webp

image.png


那么如何更改项目的端口号呢
在resources目录下新建一个application.properties文件或者application.yml文件
注意:这两个文件application.properties的优先级更高

webp

image.png


重新启动项目,端口号就会发生变化
七、  springboot启动默认图标
使用banner在线生成工具
http://www.bootschool.net/ascii;jsessionid=B94EBFF1EA05958A7AE2C0F9B9300D9C
默认是在resourcers目录下新建banner.txt文件,如果需要更改文件名可以在application.properties中书写
八、  Springboot Servlet注册
SpringBoot中有两种方式可以添加Servlet、Filter、Listener
1、代码注册通过ServletRegistrationBean、FilterRegistrationBean和ServletListenerRegistrationBean获得控制(在启动器中书写的)


@Bean // @Bean与@Configuration的区别是 @Bean必须有返回值 与spring配置文件中的bean标签同public ServletRegistrationBean servletRegistrationBean() {    return new ServletRegistrationBean(new Hello1Servlet(), "/helloServlet/*");
}

2、在 SpringBoot启动器 上使用@ServletComponentScan 注解后,Servlet、Filter、Listener 可以直接通过 @WebServlet、@WebFilter、@WebListener 注解自动注册,无需其他代码
具体的实现方式参照项目的 servlet、filter、listener包下的代码以及SpringBoot的启动器
Springboot如何配置在项目启动的时候就对servlet进行初始化呢?
像filter和servlet的一些属性可以通过注解的属性来设置
九、  SpringBoot拦截器
1、SpringBoot拦截器的定义步骤
(1)、创建我们自己的拦截器类并实现HandlerInterceptor接口


webp

image.png


webp

image.png


(2)、创建一个Java类继承WebMvcConfigurerAdapter,并重写addInterceptors方法。


webp

image.png


webp

image.png


(3)实例化我们自定义的拦截器,然后将对象手动添加到拦截器链中(在addInterceptors方法中添加)。

webp

image.png


2、SpringBoot拦截器的个人总结
(1)、当我访问自定义的Servlet的时候会被Spring的拦截器给拦截么?
不会。因为Servlet没有被Spring所管理
十、  SpringBoot MyBatis
1、pom.xml 配置maven依赖

    <!-- springboot和mybatis整合包 -->
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>1.1.1</version>
    </dependency>

    <!-- mysql 数据库驱动包 -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>

2、在配置文件application.properties中加上配置

# 数据库连接池spring.datasource.url=jdbc:mysql://127.0.0.1:3306/ssm
spring.datasource.username=root
spring.datasource.password=root123
spring.datasource.driver-class-name=com.mysql.jdbc.Driver#如果不使用注解的话可以在配置文件中配置mapper映射文件路径和类型别名#mybatis配置#类型别名mybatis.type-aliases-package=com.yubin.springboot.pojo#指定mapper.xml的位置mybatis.mapper-locations=classpath:mybatis/*.xml#mybatis.typeAliasesPackage=com.yubin.bean#mybatis.mapperLocations=classpath:com/yubin/mapper/*.xml

3、使用方式与SSM中的Mybatis基本相同
需要注意的是:
(1)、如何让容器扫描到Mapper接口呢?
方式一、在SpringBoot的启动器上面加上@MapperScan("com.yubin.dao")
方式二、在Mapper接口上加上@Repository注解(这个用于Mybatis的时候不行)
两者的区别是:方式一会代理指定包下的所有接口,方式二只会代理加了注解的接口

4、书写测试类

<!-- test 启动器 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency>
// SpringJUnit支持,由此引入Spring-Test框架支持!@RunWith(SpringRunner.class)// 指定我们SpringBoot工程的Application启动类@SpringBootTest(classes = SpringBootStart.class)// 由于是Web项目,Junit需要模拟ServletContext,因此我们需要给我们的测试类加上@WebAppConfiguration。@WebAppConfigurationpublic class MyTest {    @Autowired
    private CommonMapper commonMapper;    @Test
    public void test1() {        //PageHelper.startPage(1, 1);
        System.out.println(commonMapper.queryContent(new HashMap()));
    }
}



作者:俞斌2018
链接:https://www.jianshu.com/p/a7143180f692


打开App,阅读手记
1人推荐
发表评论
随时随地看视频慕课网APP