栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

2021-11-17学习记录初识SpringBoot

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

2021-11-17学习记录初识SpringBoot

1.HelloWorld

1.IDEA创建maven工程时选择对应的模板可以为我们迅速创建一个SpringBoot项目,但自动生成的东西对于我这个初学者来说有点不友好,遂自己跟着教程一步步来。

2.创建一个普通maven工程

3.引入相关依赖(这里使用的版本过低,建议使用更高的版本)

 
        org.springframework.boot
        spring-boot-starter-parent
        2.1.3.RELEASE
    
    

        
            org.springframework.boot
            spring-boot-starter
            2.1.3.RELEASE
            compile
        
        
            org.springframework.boot
            spring-boot-starter-web
            2.1.3.RELEASE
            compile
        
        
            org.springframework.boot
            spring-boot-starter-log4j2
            2.1.3.RELEASE
            compile
        

这里就体现出来SpringBoot的第一个特点:

依赖管理。

通过写一个parent标签就能够统一我们所在项目所用的几乎全部的依赖的版本,避免了版本冲突带来的不必要的错误。

只需要配置项目所需场景的启动器既可以完成相关场景大部分依赖的配置,比如我们需要开发一个web项目,只需要引入spring-boot-starter-web依赖就可以编写我们的业务了,帮我们节省掉以前写各种配置文件的时间。

3.写启动类,在项目包下编写一个带有@SpringBootApplication的类,并写入main方法,方法体如图。

@SpringBootApplication
public class helloWorld {
    public static void main(String[] args) {
        SpringApplication.run(helloWorld.class, args);
    }  
}

此时启动main方法,便启动了我们的SpringBoot工程。我们编写一个Controllor类来更直观的感受SpringBoot的强大之处。

@Controller
public class HelloController {
    @RequestMapping("/hello")
    @ResponseBody
    public String hello(){
        return "Hello Spring Boot2";
    }

}

该控制器向浏览器返回了一个Hello Spring Boot2字符串。

启动main方法

  .   ____          _            __ _ _
 /\ / ___'_ __ _ _(_)_ __  __ _    
( ( )___ | '_ | '_| | '_ / _` |    
 \/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |___, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.3.RELEASE)

2021-11-17 17:59:10.270  INFO 20576 --- [           main] HelloBoot.helloWorld                     : Starting helloWorld on LAPTOP-LT9K5HVK with PID 20576 (D:programmingboot2targetclasses started by 86156 in D:programmingboot2)
2021-11-17 17:59:10.272  INFO 20576 --- [           main] HelloBoot.helloWorld                     : No active profile set, falling back to default profiles: default
2021-11-17 17:59:11.229  INFO 20576 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2021-11-17 17:59:11.246  INFO 20576 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2021-11-17 17:59:11.247  INFO 20576 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.16]
2021-11-17 17:59:11.252  INFO 20576 --- [           main] 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: [D:AuI18NJDKbin;C:WINDOWSSunJavabin;C:WINDOWSsystem32;C:WINDOWS;C:Program FilesCommon FilesOracleJavajavapath;C:ProgramDataOracleJavajavapath;D:app86156product11.2.0dbhome_1bin;C:Users86156Downloadsmysql-8.0.25-winx64mysql-8.0.25-winx64bin;C:Program FilesNVIDIA CorporationNVIDIA NvDLISR;";%JAVA_HOME%;%JRE_HOME%";"C:WindowsSystem32WindowsPowerShellv1.0;";D:Program Filesxshell7;D:Program FilesXFTP;C:Users86156AppDataLocalMicrosoftWindowsApps;%IntelliJ IDEA%;%IntelliJ IDEA Community Edition%;D:apache-maven-3.8.2-binapache-maven-3.8.2bin;;.]
2021-11-17 17:59:11.319  INFO 20576 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2021-11-17 17:59:11.320  INFO 20576 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1015 ms
2021-11-17 17:59:11.474  INFO 20576 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2021-11-17 17:59:11.592  INFO 20576 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2021-11-17 17:59:11.594  INFO 20576 --- [           main] HelloBoot.helloWorld                     : Started helloWorld in 1.692 seconds (JVM running for 2.737)

随着大大Spring字符画和一串串打印信息的输出,我们的项目启动成功了。
 

Tomcat initialized with port(s): 8080 (http)

Starting service [Tomcat]

Starting Servlet engine: [Apache Tomcat/9.0.16]

从这几行信息我们可以判断出项目是跑在tomcat上且占用的是8080窗口

在浏览器输入localhost:8080/hello

获得了期待的返回结果。 

2.其他知识

浅入依赖管理

SpringBoot的版本控制是依赖maven的特性完成的

我们进入spring-boot-starter-parent

发现他又依赖了spring-boot-dependencies,进入spring-boot-dependencies

 在这里配置了许多常用的依赖的版本,在我们用到这些依赖时,不指定版本就会默认使用SpringBoot统一管理的这些版本。若想要使用其他的版本我们可以自己指定,也可以在当前的pom文件下重写相关的properties标签。

3.注解扫描,默认情况下扫描启动类所在的包以及该包之下的包。我们也可以通过@SpringBootApplication的scanbasePackages来指定要扫描的包。

也可以将@SpringBootApplication拆分成
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan("")

这三个注解,并在ComponentScan中指定扫描路径。

4.SpringBoot的IoC

之前在使用Spring的IoC时,我们需要在配置文件中配置bean标签,或者写配置类的方法来完成bean的管理。这两种方法在SpringBoot中也是适用的。

首先准备两个类:User 与 Item

public class Item {
    Integer id;
    String name;
}
public class User {
    Integer uid;
    String username;
}

用xml配置:

在application.xml中创建对应的配置


    
    

    
        
        
    

 在Spring中,我们要指定ApplicationContext来加载该配置文件。在boot中,我们只需要用@Config标识一个配置类,并用@importResource(“配置文件所在路径”)注解来将我们在配置文件中写的bean放入容器中。

用配置类配置:

以传统xml配置的方法管理bean对象最后还是要创建一个配置类,不如直接用配置类管理bean对象。

准备配置类Config

@Configuration
public class config {


    @Bean //表示将返回对象的类添加到IoC容器中
    public User user(){ //返回值为需要管理的bean对象,方法名为bean对象的名称
        return new User(1,"YOSHINO");
    }
    @Bean  //表示将返回对象的类添加到IoC容器中
    public Item item(){  //返回值为需要管理的bean对象,方法名为bean对象的名称
        return new Item(1,"CUP");
    }
}

去主方法中查看是否将配置的bean对象放入IoC容器中里

@SpringBootApplication
public class helloWorld {
    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(helloWorld.class, args);   //获得IoC容器对象
        String[] names = context.getBeanDefinitionNames(); //获得IoC容器中的bean的名字
        for (String name:names){
            System.out.println(name);
        }
    }
}

在输出中找到了我们所添加的类。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/531276.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号