2. Spring Boot 启程

我们可以通过官方的 https://start.spring.io/ 自行创建一个Spring Boot 项目,不过本篇文章选择自己创建一个Spring Boot 项目, 总需时间不超过5分钟即可。

(1) 引入依赖 :

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0     http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
    <artifactId>shopping</artifactId>
    <groupId>com.cloud.project</groupId>
    <version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>order-service</artifactId>


<properties>
    <java.version>1.8</java.version>
</properties>


<!--因为是maven module, 因此需要将parent dependency 放在 dependencyManagement.-->
<dependencyManagement>
    <dependencies>
        <dependency><!-- Import dependency management from Spring Boot -->
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-dependencies</artifactId>
            <version>2.1.4.RELEASE</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>


<!-- 目的是要写一个 http rest 项目-->
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>


<!-- spring boot 提供的一个用于打包成可运行的jar插件,使用 package 命令在 target下面生成 executable jar-->
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>
</project>

(2) 编写main函数:

@SpringBootApplication(scanBasePackages = "com.shopping.*")// 可以去掉scanBasePackages
public class ShoppingStarter {

public static void main(String[] arg){

    SpringApplication.run(ShoppingStarter.class, arg);

}
}

注意到下面的SpringBootApplication 注解,实际上他是一个复合注解,类属性包括了exclude,excludeName,scanBasePackages,scanBasePackageClasses, 很多人在开发Controller时候,由于不和main类在同一个文件中,导致 Controller 404 error, 可以加scanBasePackage,当然我们这边也可以使用Spring Boot 官方提供的最佳实践,就是把SpringBootApplication 注解的类放在 “default package ” 中, 这样就不用加 scanPackage, 因为其他的都会在这个子路径下面, 如上所示,对于exclude* 属性,因为后面我们会加入很多引用的starter,很多情况下我们不需要进行AutoConfigured,可以使用这个属性进行excluded.

  @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 {

/**
 * Exclude specific auto-configuration classes such that they will never be applied.
 * @return the classes to exclude
 */
@AliasFor(annotation = EnableAutoConfiguration.class)
Class<?>[] exclude() default {};

/**
 * Exclude specific auto-configuration class names such that they will never be
 * applied.
 * @return the class names to exclude
 * @since 1.3.0
 */
@AliasFor(annotation = EnableAutoConfiguration.class)
String[] excludeName() default {};

/**
 * Base packages to scan for annotated components. Use {@link #scanBasePackageClasses}
 * for a type-safe alternative to String-based package names.
 * @return base packages to scan
 * @since 1.3.0
 */
@AliasFor(annotation = ComponentScan.class, attribute = "basePackages")
String[] scanBasePackages() default {};

/**
 * Type-safe alternative to {@link #scanBasePackages} for specifying the packages to
 * scan for annotated components. The package of each class specified will be scanned.
 * <p>
 * Consider creating a special no-op marker class or interface in each package that
 * serves no purpose other than being referenced by this attribute.
 * @return base packages to scan
 * @since 1.3.0
 */
@AliasFor(annotation = ComponentScan.class, attribute = "basePackageClasses")
Class<?>[] scanBasePackageClasses() default {};

 }

编写简单的Controller:

   /**
 * @program: shopping
 * @author: Eric
 * @create: 2019-05-04 07:58
 **/

@RestController
@ComponentScan
public class ShoppingController {
@RequestMapping(value = "/shopping")
public String shopping(){
    return "shopping";
    }

}

通过页面访问 : localhost:8080/shopping, 浏览器输出 Shopping。

因为本项目是后续相关的项目启动的一个demo,比较简单,所以就不提供github地址,不过朋友们可以关注我的github,后续的代码都会发布在个人的github上面,有问题的话,大家都可以在github上面给我提,希望可以与各位共同学习进步。

github : https://github.com/1991lin/shopping

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。