SpringBoot学习-千里之行始于足下

SpringBoot 学习

1.SpringBoot概述

  1. 关于SpringBoot

    SpringBoot的设计目的是让项目尽可能快的启动和运行项目,而无需预先配置Spring。SpringBoot以一种固定的方式来构建可用于生产级别的应用程序。俗称:一个便捷搭建基于Spring工程的脚手架。

  2. 为什么要学习SpringBoot

    解决Java复杂的配置和混乱的依赖管理。Springboot简化了基于Spring的应用开发。

  3. SpringBoot的特点

    • 创建独立的Spring用用,为所有Spring的开发者提供一个非常快速的、广泛接受的入门体验。
    • 直接嵌入应用服务器,如Tomcat、Jetty、Undertow等;不需要部署war包。
    • 提供固定的启动器依赖去简化组件配置;实现开箱即用(启动器starter-其实就是SpringBoot提供的一个jar包),通过自己设置参数(.properties或.yml的配置文件),即可快速使用。
    • 自动地配置Spring和其他有需要的第三方依赖。
    • 提供了一些大型项目中常见的非功能性特效,如内嵌服务器、安全、指标、健康检测、外部化配置等。
    • 绝对没有代码生成,也无需XML配置。

2.SpringBoot入门

  1. 在pom.xml中添加基本依赖

    <!-- 副工程依赖 -->
    <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.1.5.RELEASE</version>
    </parent>
    
    <dependencies>
    		<!-- SpringBoot的依赖 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <!-- JSON依赖-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-json</artifactId>
            </dependency>
            <!-- Tomcat依赖-->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-tomcat</artifactId>
            </dependency>
            <!-- Hibernate Validator依赖-->
            <dependency>
                <groupId>org.hibernate.validator</groupId>
                <artifactId>hibernate-validator</artifactId>
            </dependency>
            <!-- Spring Web依赖-->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-web</artifactId>
                <version>5.1.7.RELEASE</version>
            </dependency>
            <!-- Spring Web MVC依赖-->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>5.1.7.RELEASE</version>
            </dependency>
    </dependencies>
    
  2. 通过注解@Component&@ComponentScan扫描装配 Bean

    User.java:

    package com.lin.demo;
    
    
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.stereotype.Component;
    
    //若不配置名称,默认为类开头小写
    @Component("user")
    public class User {
        @Value("1")
        private int id;
        @Value("user_name_1")
        private String userName;
        @Value("note1")
        private String note;
        
        /**  setter and getter  **/
    }
    

    为了让Spring IoC 容器装配这个类,需要改造类AppConfig

    AppConfig.java

    package com.lin.demo;
    
    
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    
    @Configuration
    @ComponentScan
    public class AppConfig {
    }
    
  3. 测试扫描

    ApplicationContext ctx
    	= new AnnotationConfigApplicationContext(AppConfig.class);
    User user = ctx.getBean(User.class);
    

3.Java配置

常用注解:

  • @Configuration:声明一个类作为配置类,代替xml文件
  • @Bean:声明在方法上,将方法的返回值加入Bean容器,代替标签
  • @Value:属性注入
  • @propertySource:指定外部属性文件
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值