2. Spring Boot 启程

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

(1) 引入依赖 :




    shopping
    com.cloud.project
    1.0-SNAPSHOT

4.0.0

order-service



    1.8





    
        
            org.springframework.boot
            spring-boot-dependencies
            2.1.4.RELEASE
            pom
            import
        
    





    
        org.springframework.boot
        spring-boot-starter-web
    





    
        
            org.springframework.boot
            spring-boot-maven-plugin
        
    


(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.
 * 

* 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

你可能感兴趣的:(2. Spring Boot 启程)