软件框架SpringBoot-实现使用@Component@Data@Configuration@Bean(配置类控制类实体类)等方法实现将配置文件从8080端口显示在网页上

一、前言

1.该程序代码是使用idea2021.12版本编写的,若使用其他软件请对照好配置;

2.这个程序具体的内容我忘了,只知道使用@Component@ConfigurationProperties@Data
@Configuration@Bean@RestController@Autowired@GetMapping等方法写的,具体实现的功能就是新建几个.yml和.java文件,实现在.yml文件中配置访问路径(输入信息)通过localhost:8080端口实现在网页上输出;

3.下面写的是这个表需要建的结构和代码段以及运行的结果;

4.这个博文讲的,我已将代码包发布到了我的资源里,有需要的可以直接下载并导入到自己的id里,看看能不能运行;

5.网页运行的链接在下面;

二、结构

首先需要建包,这里就不多说了,我建的包名为unit2-3;

依次在包中src-main-java-com-example-unit23文件中新建三个软件包,名称分别为configuration、trolleconr、vo;

其次在configuration软件包中新建MyConfiguration这个Java类文件,这个是实现配置类的

在次在trolleconr软件包中新建StudentController这个Java类文件,这个是用来配置控制类的

最后在vo软件包中新建Student这个Java类类文件,这个是用来配置实体类的

同时还需要在src-main-resources软件包中新建application.yml文件,这个是用来配置访问路径以及学生信息的

以上为本代码的结构,下面为代码的结构,画框的就是需要编写的代码,下面会依次将各代码段粘贴

三、代码段

1.MyConfiguration.java类代码

package com.example.unit23.configuration;

import com.example.unit23.vo.Student;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

//配置类
@Configuration
public class MyConfiguration {
    //组件配置,写在方法名上,返回一个Bean对象
    @Bean
    public String msg(){
        return "这是配置类中的返回信息";
    }
    @Bean
    public Student mystu(){
        return new Student();
    }
}

2.StudentController类代码

package com.example.unit23.controller;

import com.example.unit23.vo.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

//控制类
@RestController
public class StudentController {
    @Autowired
    protected  String msg;
    @Autowired
    private Student student;

    @GetMapping("/test")
    public String test(){
        return msg;
    }
    @GetMapping("/student")
    public String getStu(){
        return student.toString();
    }
}

 3.Student.java类代码

package com.example.unit23.vo;

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

import java.util.List;
//实体类
@Component
@ConfigurationProperties(prefix = "student")
@Data
public class Student {
    private String name;
    private String home;
    private List<String> fruits;
}

4.application.yml类代码

#配置访问路径
server:
  servlet:
    context-path: /index



#配置学生信息
student:
   name: CSDN
   home: 中国北京
   fruits:
     - 代码
     - 编程
     - 计算
     - 算法

四、网页链接代码

建议使用谷歌、火狐等浏览器;

http://localhost:8080/index/student

五、运行结果

1.id控制台运行的结果

2.在谷歌浏览器上显示的链接

 以上为本程序的相关内容,因为我忘了这个程序的具体使用方法,所以就无法讲解了,有需要或了解的可以看看,谢谢!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

姜鸿阳

谢谢您!感谢您的支持!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值