美文网首页Spring
[Spring Boot]入门HelloWorld

[Spring Boot]入门HelloWorld

作者: 小球学java | 来源:发表于2016-10-28 16:58 被阅读0次

简介##

目前,很多企业级的Java web应用绝大部分都用到了spring框架,然后繁杂的xml以及annotation配置,往往使得开发花费大量时间去配置以及维护。spring boot项目是基于spring框架的项目进行整合,使得开发能够快速创建可以独立运行的spring应用,Spring Boot 可以自动配置 Spring 的各种组件,并不依赖代码生成和 XML 配置文件。Spring Boot 可以大大提升使用 Spring 框架时的开发效率。极大的减少了开发在配置上的时间,使得开发可以专注于业务逻辑,并且springboot可以以jar的方式部署。

特性##

springboot有如下特性:

  • 创建可以独立运行的spring应用
  • 直接嵌入jetty和tomcat服务器,不需要部署war
  • 使用更简洁的maven配置
  • 不需要xml配置
  • 提供可以直接在生产环境中使用的功能,如性能指标、应用信息和应用健康检查。

环境准备##

  • jdk配置
  • idea开发工具
  • maven本地仓库配置

工程创建启动##

1.新建一个maven工程命名spring-boot-helloworld,如图


工程结构图

2.添加spring-boot的pom依赖,需要添加仓库地址,可以确保顺利下载相关jar
<pre>

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

<!--版本号,使用utf8,jdk1.8-->
<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <!--web应用相关包括springmvc等-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!--提供自动配置功能,此处用到了@SpringBootApplication-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-autoconfigure</artifactId>
    </dependency>
</dependencies>

<!--配置仓库地址,就可以下载相应的jar,如果不配置,可能会下载不了maven依赖的jar包-->
<repositories>
    <repository>
        <id>spring-releases</id>
        <url>https://repo.spring.io/libs-release</url>
    </repository>
</repositories>
<pluginRepositories>
    <pluginRepository>
        <id>spring-releases</id>
        <url>https://repo.spring.io/libs-release</url>
    </pluginRepository>
</pluginRepositories>

<!--构建springboot使用的maven插件-->
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build></pre>

3.此时工程中的maven仓库中jar的依赖,如图:


jar包依赖图

4.在src/main/java/com/swun目录下创建Application.java,添加代码如下

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

    String[] excludeName() default {};

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

    @AliasFor(
        annotation = ComponentScan.class,
        attribute = "basePackageClasses"
    )
    Class<?>[] scanBasePackageClasses() default {};
}
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration
public @interface SpringBootConfiguration {
}

5.启动springboot应用,右击->run Application.java 或者maven -clean ->maven install找到target目录下面的spring-boot-helloworld-1.0-SNAPSHOT.jar,运行java -jar spring-boot-helloworld-1.0-SNAPSHOT.jar

命令行启动 启动成功

至此,spring boot启动成功!

相关文章

网友评论

    本文标题:[Spring Boot]入门HelloWorld

    本文链接:https://www.haomeiwen.com/subject/pbjeuttx.html