• SpringBoot(一) 入门篇,简单配置


    编辑工具:Intellij IDEA

    一.SpringBoot的应用:

    1.创建文件

    2.项目结构

    3.开始构建springboot项目,项目结构

    第一步创建Person类:

    package com.oda.springboot.bean;
    
            import org.springframework.boot.context.properties.ConfigurationProperties;
            import org.springframework.stereotype.Component;
    
    //@Component:把普通pojo类实例化到spring容器中,相当于配置文件中的<bean id="">
    @Component
    //@ConfigurationProperties:application.properties文件中的属性前缀
    @ConfigurationProperties(prefix = "person") public class Person { private String name; private int age; public Person() { } public Person(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }

    第二步创建HelloController类

    package com.oda.springboot.controller;
    
    import com.oda.springboot.bean.Person;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    //@RestController:不能返回视图,相当于@Controller+@ResponseBody
    @RestController
    public class HelloController {
    
        @Autowired
        private Person person;
        @RequestMapping("/")
        public String first(){
            return "Hello World!";
        }
    
        @RequestMapping("/index")
        public String index(){
            return "name:"+person.getName()+"age:"+person.getAge();
        }
    }

    第三 步填写application,properties文件

    server.port=8080
    server.servlet.context-path=/zm/
    person.name=zm
    perosn.age=23

    第四步:启动SpringbootApplication类(右击,run)

    访问:http://localhost:8080/zm/

     再访问:http://localhost:8080/zm/index

    注意:yml文件是key,value形式的,写yml文件中自定义的属性如果不变色(变色才是正确的),具体如下图,value值前面空一格就好了。

  • 相关阅读:
    Linux 基础与应用教程 003(权限管理命令简单基础)
    Linux 基础与应用教程 002
    Android:监听音键并屏蔽系统的音量调节
    Android:用Seekbar来调节屏幕亮度
    Android:获得一个竖的seekbar
    python之路(4)高阶函数和python内置函数
    python之路(3)函数和匿名函数
    python之路(2)集合(set)和字符串格式化
    python之路(1)数据类型
    SD从零开始03-04
  • 原文地址:https://www.cnblogs.com/zhaomin08240115/p/9145449.html
Copyright © 2020-2023  润新知