SpringBoot学习记录二、基本使用

来源:互联网 发布:漫步者s1000 知乎 编辑:程序博客网 时间:2024/06/16 14:12

一、基本pom依赖

<parent><!--识别application**.properties或者application**.yml资源文件,指定java版本,默认是1.6,设置默认编码UTf-8    指定版本信息,这样在自己的pom.xml中不写版本时候,会自动继承父pom文件的版本--><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.8.RELEASE</version><relativePath/></parent><properties><!--由于父版本指定的jdk版本为1.6,如果想使用更好级别的版本在这里配置--><java.version>1.8</java.version></properties><dependencies><!--导入web开发所需支持--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!--导入测试所需的maven依赖--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><!--热部署,同时需要注意使编辑器支持热部署--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><!--为true表示热部署不会传递,如果其他项目依赖这个pom文件,想支持热部署,需要手工导入这个依赖--><optional>true</optional></dependency></dependencies><build><plugins><!--导入一些常用插件:     常用:mvn package 生成一个可执行jar包           mvn spring-boot:run 直接使用默认tomcat启动--><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build>

二、写一个helloworld

       首先,先看一下spring-boot的项目结构


      Boottest3Application:作为主类,可以扫描它当前包以及子包中的注解。

@SpringBootApplicationpublic class Boottest3Application {public static void main(String[] args) {SpringApplication.run(Boottest3Application.class, args);}}

       @SpringBootApplication:组合注解,配置它相当于配置了@Configuration ,@EnableAutoConfiguration,@ComponentScan.

                                                 @Configuration:相当于配置文件的beans,

                                                 @EnableAutoConfiguration:根据添加的jar猜想需要的依赖并进行导入

                                                 @ComponentScan:自动扫描包下面的@Controller,@Service@Component,@Repository并进行注入。

         SpringAppication:作为启动项,直接运行main就可以启动项目。

@RestController  //在Controller包下,用以访问public class HelloController {    @GetMapping("/hello")    public String hello(){        return "哈喽,沃德";    }} 
        

          SpringApplicaiton.run:运行它后,控制台输出日志,没有错误后,就启动成功了。


 

    ps:渣渣一枚,如有不对请大佬指出                                          

        

原创粉丝点击