SpringBoot(1)

SpringBoot是2014年推出的简化Java EE程序开发的框架,它简化了依赖管理、配置文件,并内置Servlet容器,便于快速创建独立运行的Spring应用程序。通过自动配置和starter依赖,SpringBoot能够快速集成主流框架,并提供了生产级别的应用监控。学习SpringBoot前,需要掌握Spring框架、Maven及IDEA基础。本文将介绍如何创建第一个SpringBoot应用,包括创建Maven项目、配置启动器、编写控制器类等步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.什么是SpringBoot

        SpringBoot是Spring团队在2014年,伴随Spring4.0版本推出的一个新的框架。

        主要作用:SpringBoot是创建独立的,生产级的,基于Spring的应用程序变得容易,帮我们快速的创建出基于Spring的应用程序。

        SpringBoot是Spring团队在2014年,伴随Spring4.0版本推出的一个帮助开发者快速的建立出基于Spring框架的应用程序的新的框架。

        你在没有SpringBoot的时候我们创建基于Spring框架的应用程序,或者是整合SpringMVC、MyBatyis等等其他的框架时候,往往都需要先导入很多的依赖包,在导入依赖的时候少了/版本不匹配,都会造成创建应用程序/整合其他框架失败。除过导入依赖麻烦,还需要配置很多相关的配置文件【applicationContext.xml /MyBatis-config.xml..... 】。在部署创建完成好的应用程序时,还得配置好服务器环境。所以在以前没有SpringBoot的时候,我们对java EE程序的开发都是比较笨重的开发,需要繁多的配置,带来低下的开发效率和复杂的部署流程,还有第三方技术集成难度大。

        因此上 SpringBoot的出现实际上就是用来简化我们对javaEE程序的开发,帮助我们快速的创建出基于Spring的应用程序

        1.导入依赖包方便

        2.减少配置文件

        3.配置服务器环境

      SpringBoot的底层实际上还是使用的Spring技术,它其实就是通过整合Spring提供的针对不同领域的开发框架来达到简化Java EE 程序的开发步骤的
      Spring提供的针对不同领域的开发框架
      Spring Framework:提供IOC容器
      Spring Cloud:分布式框架
      Spring Data:数据库访问框架
      Spring Security:安全校验框架
      等等......
    利用SpringBoot创建的Spring应用程序,可以很轻松的实现Spring各种子框架的应用和整合。

SpringBoot的优点

        1.快速创建独立运行的Spring项目以及与主流框架集成 
        2.使用嵌入式的Servlet容器,应用无需打成WAR包 
        3.starters自动依赖与版本控制 
        4.大量的自动配置,简化开发,也可修改默认值 
        5.无需配置XML,无代码生成,开箱即用 
        6.准生产环境的运行时应用监控 
        7.与云计算的天然集成

在学习使用SpringBoot之前我们需要以下的一些基本内容:

        1.熟练使用Spring框架

        2.熟练使用Maven进行项目的构建和依赖包的管理

        3.熟练使用Eclipse或者IDEA

需求:浏览器发送hello请求,服务器接收请求并处理,响应Hello,world字符串。

        1、创建一个maven工程;(jar) 

        2、创建包名,项目名,项目类型【Maven Project】,打包方式【jar/war】,jdk版本

        3、选择springboot版本【2.6.4】

        4、在pom.xml文件中配置开发javaweb程序的启动器

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

可以帮助我们将开发基于Spring的web应用程序的所有依赖包全部导入

        5、在src/main/java下创建控制器类

package com.springboot.demo.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class TestHello {

    @RequestMapping("hello")
    @ResponseBody
    public String testHello(){
            return "hello,SpringBoot";
    }
}

        6、 由于我们自己创建的控制器类与SpringBoot的主类不在同一个包内,我们需要给主类上添加@ComponentScan(basePackages = "com.springboot.demo")如果我们自己创建的控制器类与SpringBoot的主类在同一个包中,那么这个注解不用加。

        7、运行主类中的主方法,应用程序自动部署,内嵌的Servlet容器自动启动

        8、浏览器测试"http://127.0.0.1:8080/hello"

        9、将上面的用户程序打包成jar包,通过java-jar xxxx.jar命令来执行

        右侧--MAVEN---选中项目---Lifecycle---install----运行 ---package----运行

         在当前项目的target目录下得到“项目名称-版本号.jar”[springbootdemo1-0.0.1-SNAPSHOT.jar]将这个jar包复制出去,编写一个与之对应的批处理文件,双击就可运行。

Spring JavaConfig

        之前的程序配置是需要依赖XML的配置方式

        现在主要使用基于java代码和Annotation注解的方式完成配置。减少配置文件和配置内容

基于XML的配置方式是这样的

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans.xsd">
 <bean id="studenBean" class="com.springboot.demo.bean.StudentBean"></bean>
</beans>

基于Java代码和Annotation注解的方式取代基于XML的配置方式

@Configuration
public class StudentConfig {
    @Bean
    public StudentBean stuben(){
        return  new StudentBean(perbean());
    }
}

        任何一个标注了@Configuration的Java类定义都是一个JavaConfig 配置类

        任何一个标注了@Bean的方法,其返回值将作为一个bean定义注册到Spring的IoC容器,方法名将默认成为该bean定义的id属性。

构造方法注入

package com.springboot.demo.bean;

//被调用者
public class PersonBean {

    public void  testPerson(){
        System.out.println("PersonBean类的实例方法");
    }
}
package com.springboot.demo.bean;

//调用者类
public class StudentBean {
    //依赖对象
    private PersonBean personBean;
    ///构造方法
    public StudentBean(){}
    public StudentBean(PersonBean personBean){
        this.personBean=personBean;
    }
    public void methodStudent(){
        personBean.testPerson();
        System.out.println("Student类的实例方法");
    }
}

set方法注入 

package com.springboot.demo.bean;

//被调用者
public class PersonBean {

    public void  testPerson(){
        System.out.println("PersonBean类的实例方法");
    }
}
package com.springboot.demo.bean;

import lombok.Setter;


//调用者类
public class StudentBean {
    //依赖对象
    @Setter
    private PersonBean personBean;

    public void methodStudent(){
        personBean.testPerson();
        System.out.println("Student类的实例方法");

    }


}

package com.springboot.demo.config;

import com.springboot.demo.bean.PersonBean;
import com.springboot.demo.bean.StudentBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class StudentConfig {
    @Bean
    public PersonBean perbean(){
       return   new PersonBean();
    }
    @Bean
    public StudentBean stubean(){
        StudentBean studentBean=new StudentBean();
        studentBean.setPersonBean(perbean());
        return studentBean;
    }
}

基于XML的配置方式

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
         http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="person" class="com.springboot.demo.bean.PersonBean"></bean>
    <bean id="student" class="com.springboot.demo.bean.StudentBean">
        <constructor-arg name="personBean" ref="person"></constructor-arg>
    </bean>
</beans>

基于Java代码和Annotation注解的方式取代基于XML的配置方式

package com.springboot.demo.config;

import com.springboot.demo.bean.PersonBean;
import com.springboot.demo.bean.StudentBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class StudentConfig {
    @Bean
    public PersonBean perbean(){
        return new PersonBean();
    }

    @Bean
    public StudentBean stuben(){
        return  new StudentBean(perbean());
    }
}

代替XML配置方式的常见注解

        1.@ComponentScan(basePackages="基础包名称")[主类上]配置自动扫描包,将java类上带有Component("自定义名称"),@Repository("自定义名称"),@Service("自定义名称"),@Controller注解的java类实例化, 代替了xml配置文件中

<context:component-scan base-package="基础包名称"></context:component-scan>

        2. @PropertySource("classpath:db.properties")读取从resources下xxxx.properties资源文件【有@Configuration的java类上,特别是数据源配置类】代替了xml配置文件中

<context:property-placeholder location="classpath:db.properties"></context:property-placeholder>

            @PropertySource---加载resources下一组xxx.properties资源文件

            @PropertySource({@PropertySource("classpath:1.properties"),

                                            @PropertySource("classpath:2.properties"),....})

         3.@Setter---设置在类上为类中成员变量提供setxxx();

                              设置在类中的成员变量上只为当前成员变量提供setxxx();

            @Getter---设置在类上为类中成员变量提供getxxx();

                               设置在类中的成员变量上只为当前成员变量提供getxxx();

             @ToString---设置在类上为当前类提供默认的toString();

              @AllArgsConstructor---设置在类上为当前类提供有参数的构造方法;

使用上面的注解需要:

                1.引入依赖:(版本自行选择)lombok依赖

                2.配置lombok插件

        4. @Value("${db.username}")----将资源文件【xxxx.properties】中的属性值,绑定到配置类【@Configuration的java类】中的成员变量上。

        代替了xml配置文件中

        <property name="driverClassName" value="${mydriver}"> </property>

        5. @import --- 将多个分开配置类【@Configuration的java类】,合并到一个配置类当中

package com.springboot.demo.config;

import com.springboot.demo.bean.PersonBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class PersonBeanConfig {
    @Bean
    public PersonBean person(){
        return new PersonBean();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值