Springboot_入门 helloworld

实现目标:输入http://localhost:8080/hello,得到helloworld

hello.java

package Class;

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

/*
@SpringBootApplication 来标注一个主程序类Cannot resolve org.apache.tomcat.embed:tomcat-embed-core:8.5.20
,说明这是一个SpringBoot应用
打开@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 {

@SpringBootConfiguration:SpringBoot的配置类---配置文件  配置类也是容器中的组件
@EnableAutoConfiguration:开启自动配置
      @AutoConfigurationPackage:自动配置包 将主配置类及下面所有子包里面的所有组件扫描到Spring容器中
            @Import({Registrar.class}) spring的底层注解,给容器导入一个组件{Registrar.class}
      @Import({AutoConfigurationImportSelector.class}) 导入哪些组件的选择器 将所有需要的组件以全类名的方式返回
      这些组件就会被添加到容器中,会给组件导入非常多的自动配置类,就是给容器导入这个场景需要的所有组件,并配置好这些组件
      有了自动配置类,就免去了我们手动编写配置注入功能组件等的工作

      J2EE的整体整合解决方案和自动配置都在spring-boot-autoconfigure-2.0.0.RELEASE.jar里
 */
@SpringBootApplication
public class hello {
    public static void main(String[] args) {
        //spring应用启动起来
        SpringApplication.run(hello.class,args);
    }
}

helloController.java

package Class.Control;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class helloController {
    @ResponseBody
    @RequestMapping("/hello")
    public String h(){
        return "helloworld";
    }
}

pom.xml

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.RELEASE</version>
    </parent>
    <!--上述代码的父项目是
      <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-dependencies</artifactId>
      <version>2.0.0.RELEASE</version>
      <relativePath>../../spring-boot-dependencies</relativePath>
  </parent>
  springboot版本仲裁中心:
  以后我们导入依赖默认是不需要写版本(没有在dependencies里面管理的依赖自然需要声明版本号)

  spring-boot-starter-web:帮我们导入web模块正常运行所依赖的组件
  spring-boot-starter:spring-boot场景启动器

  springboot将所有的功能场景都抽取出来,做成一个个的starters(启动器),只需要在项目里面引入这些starter
  相关场景的所有依赖都会导入进来,要用什么功能就导入什么场景的启动器
  -->

    <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>
                <version>2.0.0.RELEASE</version>
            </plugin>
        </plugins>
    </build>
</project>

猜你喜欢

转载自www.cnblogs.com/zuiaimiusi/p/12453243.html