springboot的第一个项目+@SpringBootApplication

重点提要 :

       1)@SpringBootApplication--标识是一个springboot项目,标注的这个类是项目的主配置类。

             SpringApplication.run(启动类);进行根目录下启动

       2)@Controller标识是一个controller;

            @RequestMapping--url映射;

            @ResponseBody--将String字符串返回到页面上。

            (其他:repsonse.getWriter().write();modelAndView;)

       3)@Controller+@ResponseBody=@RestController

创建springboot的方式 :

     1)maven项目 添加spring-boot依赖,创建

      2)IDEA17版以上-springInitializer快速创建

           a. resource目录下:

              static-保存所有的静态资源(js/css/images)

              template--保存所有的模板页面(springboot默认jar使用嵌入的tomcate,默认不支持jsp)

                                       可以使用模板引擎(freemarker/velocity/thtmeleaf等)

              application.properties: 声明变量(比如下 server.port=服务端口)

                                       

       3)maven archetype命令方式创建

1.新建maven工程

   <groupId>com.suguo.springboot</groupId>
   <artifactId>springboot-assemble</artifactId>
   <version>1.0-SNAPSHOT</version>

2.添加依赖

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.9.RELEASE</version>
</parent>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

3.编写启动类

@SpringBootApplication
public class MyApplication {
    public static void main(String[] args) {
        SpringApplication.run(MyApplication.class);
    //SpringApplication只能运行@SpringBootApplication标注的类,否则启动报错
    }
}
@Controller
public class DemoController {
    @RequestMapping("/hello")
    @ResponseBody
    public  String  hello(){
        return "Hello world";
    }
}

注意:这里一定要新建包,放在自定义的包下,否则会启动失败。

      错误代码:

            

     demoController也要放在自定义包下,否则会报错如下:

           

改正后:

             

              

 

     1)为什么一定要新建包呢

     原因:是@ComponentScan的扫描范围的问题。

        @springbootapplication的扫描包路径是主类下所有包。

         扫描路径为:classpath:/*/*.class

         报错信息如下:

            

 org.springframework.beans.factory.BeanDefinitionStoreException: Failed to read candidate component class: URL      [jar:file:/E:/repository/org/springframework/boot/spring-boot-autoconfigure/1.5.9.RELEASE/spring-boot-autoconfigure- 1.5.9.RELEASE.jar!/org/springframework/boot/autoconfigure/jdbc/DataSourceAutoConfiguration$EmbeddedDatabaseConfiguration.class]; nested exception is java.lang.IllegalStateException: Could not evaluate condition on org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration$EmbeddedDatabaseConfiguration due to org/springframework/dao/DataAccessException not found. Make sure your own configuration does not rely on that class. This can also happen if you are @ComponentScanning a springframework package (e.g. if you put a @ComponentScan in the default package by mistake)

 ----- 具体见如下@springbootapplication的解析  

        改正后扫描路径为classpath*:demo/**/*.class;

         

   2)为什么HelloController访问hello会404呢?

    --因为@SpringBootApplication的@AutoConfigurationPackage扫描的是主类及其下面的子包。

      /hello请求未被扫描映射到,索引访问404.

      

4.@SpringbootApplication注解解析

   1)一级注解依赖    

  @Inherited //可继承的意思
  @SpringBootConfiguration---//springboot应用注解
  @EnableAutoConfiguration//可自动注入
  @ComponentScan( //包扫描注解--excludeFilters --排除的filter
       excludeFilters = {@Filter(
              type = FilterType.CUSTOM,
              classes = {TypeExcludeFilter.class}),        
           @Filter(type = FilterType.CUSTOM,
             classes = {AutoConfigurationExcludeFilter.class}
            )}
  )  

    a.TypeExcludeFilter

    b.AutoConfigurationExcludeFilter

  2)@SpringBootConfiguration-springBoot的配置类注解

  @Target({ElementType.TYPE})
  @Retention(RetentionPolicy.RUNTIME)
  @Documented
  @Configuration//标注在配置类(配置文件)上。
  public @interface SpringBootConfiguration {
  }

      

 3)@EnableAutoConfiguration--开启自动配置

@Target({ElementType.TYPE})//作用在目标类上
@Retention(RetentionPolicy.RUNTIME)//运行时生效
@Documented//文档编写
@Inherited //可继承
@AutoConfigurationPackage//自动配置包
@Import({EnableAutoConfigurationImportSelector.class})//自动导入
public @interface EnableAutoConfiguration {
    String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration";
    Class<?>[] exclude() default {};//排除的类
    String[] excludeName() default {};//排除的名称
}

@AutoConfigurationPackage

   -将主配置类(@SpringBootApplication标注的类)的所在包及下面所有子包里面的所有注解扫描到spring容器。

     @Import-spring的底层注解,给容器中导入一个组件,导入的组件由AutoConfigurationPackages.Registrar.class决定。

       

      

 

EnableAutoConfigurationImportSelector-开启自动配置类的导包选择器

   -将所有需要导入的组件以全类名的方式返回,这些组件就会到添加到容器中。

   会给容器导入非常多的自动配置类(xxAutoConfiguratio);就是给容器中导入这个场景需要的所有组件,并配置好这些组件。

----优点:不用手动再去配置功能组件。

  如下 注解元信息:

  

 注解导入的自动配置类:org.springframework.boot.autoconfigure.AutoConfigurationImportSelector#selectImports

      

   A.这些自动配置类的导入:

   org.springframework.core.io.support.SpringFactoriesLoader#loadFactoryNames

  ---springboot启动的时候从类路径下的 META-INF/spring.factorie获取EnableAutoConfiguration指定的值。

  将这些值作为自动配置类导入容器中,自动配置类就生效,进行自动配置工作。

  

/org/springframework/boot/spring-boot-autoconfigure/1.5.9.RELEASE/spring-boot-autoconfigure-1.5.9.RELEASE.jar!/org/springframework/boot/autoconfigure

---springboot自动配置关键包--自动配置添加和j2EE的重要组件的大整合。

3)@ComponentScan

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
@Documented
@Repeatable(ComponentScans.class)//
public @interface ComponentScan{}

 

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值