SpringBoot开发过程之一

项目搭建

创建完成

编写一个简单的类然后在配置文件设置属性,在controller中获取该属性值

写一个studnet实体类

package com.bdqn.spring_boot_10_31_5.entity;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

//实体类注入容器
@Component
//将该对象实例一个对象给容器,可以在配置文件中进行配置,student是统一的
@ConfigurationProperties(prefix="student")
public class Student {
    private String name;
    private int age;

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public Student() {

    }

    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;
    }
}

在实体类上交以后然后在配置文件中可以进行属性配置

student.name=jin
student.age=19

最后写controller

package com.bdqn.spring_boot_10_31_5.controller;

import com.bdqn.spring_boot_10_31_5.entity.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
public class getStudent {
    //自动注入
    @Autowired
    private Student student;
    
    //外部访问路径
    @RequestMapping("/sayHello")
    //设置返回的方式,该注解是返回给访问对象string字符串
    @ResponseBody
    public String show(){
        return "获取到"+student.getName()+student.getAge();
    }
}

猜你喜欢

转载自blog.csdn.net/jinqianwang/article/details/83586155