springboot初级入门demo以及架构组成部分

本文详细介绍如何使用Maven创建SpringBoot项目,配置pom文件,集成SpringBoot父依赖,以及使用@SpringBootApplication注解创建应用入口。通过创建TestController实现第一个HelloWorld案例,展示SpringBoot内嵌Tomcat启动过程。

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

首先,创建工程需要的pom文件,这里的工程,我们采用maven工程部署;

<properties>
        <java.version>1.8</java.version>
    </properties>
    <!--集成springboot的父依赖-->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.7.RELEASE</version>
    </parent>

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

    <!--打可执行jar包-->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

然后创建一个spring的总入口,Application.java

package com.ysl;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

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

此时的main方法是可以直接运行的,虽然没有任何功能和显示。

这里是运行的日志,可以简易的看出步骤:

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

2018-03-03 10:28:24.028  INFO 6358 --- [           main] com.ysl.Application                      : Starting Application on master with PID 6358 (/home/workspace/springboottest/target/classes started by ysl in /home/workspace/springboottest)
2018-03-03 10:28:24.038  INFO 6358 --- [           main] com.ysl.Application                      : No active profile set, falling back to default profiles: default
2018-03-03 10:28:24.218  INFO 6358 --- [           main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@1b37288: startup date [Sat Mar 03 10:28:24 CST 2018]; root of context hierarchy
2018-03-03 10:28:27.001  INFO 6358 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2018-03-03 10:28:27.025  INFO 6358 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2018-03-03 10:28:27.026  INFO 6358 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.20
2018-03-03 10:28:27.141  INFO 6358 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2018-03-03 10:28:27.142  INFO 6358 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 2936 ms
2018-03-03 10:28:27.294  INFO 6358 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Mapping servlet: 'dispatcherServlet' to [/]
2018-03-03 10:28:27.299  INFO 6358 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2018-03-03 10:28:27.300  INFO 6358 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2018-03-03 10:28:27.300  INFO 6358 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2018-03-03 10:28:27.300  INFO 6358 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
2018-03-03 10:28:27.669  INFO 6358 --- [           main] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@1b37288: startup date [Sat Mar 03 10:28:24 CST 2018]; root of context hierarchy
2018-03-03 10:28:27.755  INFO 6358 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
2018-03-03 10:28:27.756  INFO 6358 --- [           main] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2018-03-03 10:28:27.795  INFO 6358 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/webjars/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-03-03 10:28:27.796  INFO 6358 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-03-03 10:28:27.835  INFO 6358 --- [           main] o.s.w.s.handler.SimpleUrlHandlerMapping  : Mapped URL path [/**/favicon.ico] onto handler of type [class org.springframework.web.servlet.resource.ResourceHttpRequestHandler]
2018-03-03 10:28:28.143  INFO 6358 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
2018-03-03 10:28:28.252  INFO 6358 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2018-03-03 10:28:28.261  INFO 6358 --- [           main] com.ysl.Application                      : Started Application in 5.148 seconds (JVM running for 5.974)

此日志打印完毕之后,代表springboot的搭建完毕,内嵌tomcat服务器,监听端口号为8080,此程序现在正在运行中。

我们再main方法中,有用到@SpringBootApplication注解,这里我们有必要讲解一下,这个最关键的注解:

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(
    excludeFilters = {@Filter(
    type = FilterType.CUSTOM,
    classes = {TypeExcludeFilter.class}
), @Filter(
    type = FilterType.CUSTOM,
    classes = {AutoConfigurationExcludeFilter.class}
)}
)
public @interface SpringBootApplication {
    @AliasFor(
        annotation = EnableAutoConfiguration.class,
        attribute = "exclude"
    )
    Class<?>[] exclude() default {};

    @AliasFor(
        annotation = EnableAutoConfiguration.class,
        attribute = "excludeName"
    )
    String[] excludeName() default {};

    @AliasFor(
        annotation = ComponentScan.class,
        attribute = "basePackages"
    )
    String[] scanBasePackages() default {};

    @AliasFor(
        annotation = ComponentScan.class,
        attribute = "basePackageClasses"
    )
    Class<?>[] scanBasePackageClasses() default {};
}

@Configuration : 表示Application作为sprig配置文件存在 


@EnableAutoConfiguration: 启动spring boot内置的自动配置 


@ComponentScan : 扫描bean,路径为Application类所在package以及package下的子路径,这里为 com.ysl,在spring boot中bean都放置在该路径已经子路径下。

 

 

很明显,上面的操作连个HelloWorld都没有出来,远远满不足我们的需求。在com.ysl下创建子包controller,在该包下创建TestController.java,内容如下:

package com.ysl.controller;

import org.springframework.web.bind.annotation.Mapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {

    @RequestMapping("/")
    public String home(){
        return "hello,springboot";
    }

}

启动程序,然后直接输入localhost://8080就可以看到hello.springboot了。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值