java框架之springboot二:springboot配置获取

爸爸去哪了2之熊猫三胞胎 爸爸去哪了2之熊猫三胞胎     2023-02-15     507

关键词:

java框架之Spring boot二:SpringBoot配置获取

resources文件夹中的目录结构:
static:保存所有的静态资源;js,css,images
templates:保存所有的模板页面;
application.properties:配置文件,可以修改一些默认配置

配置文件的作用:修改SpringBoot自动配置的默认值;SpringBoot在底层都给我们自动配置好

配置文件书写类型主要有两中,一种为properties,一种是yaml。这边是使用properties。

配置文件的书写:

server.port=8081
#idea使用的是UTF-8,properties使用的ascll码,所以会乱码
#Person
person.name=lala
person.age=18
person.birth=2017/12/15
person.boss=false
person.maps.k1=v1
person.maps.k2=15
person.lists=a,b,c
person.dog.name=dog
person.dog.age=15

对应bean类的书写,Person:

package com.example.demo.bean;


import java.util.Date;
import java.util.Map;
import java.util.List;

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

@Component
@ConfigurationProperties(prefix="person")
public class Person 
    /**/
    private String name;
    private Integer age;
    private Boolean boss;
    private Date birth;
    private Map<String, Object> maps;
    private List<Object> lists;
    private Dog dog;
    public Person() 
        super();
    
    public Person(String name, Integer age, Boolean boss, Date birth, Map<String, Object> maps, List<Object> lists,
            Dog dog) 
        super();
        this.name = name;
        this.age = age;
        this.boss = boss;
        this.birth = birth;
        this.maps = maps;
        this.lists = lists;
        this.dog = dog;
    
    public String getName() 
        return name;
    
    public void setName(String name) 
        this.name = name;
    
    public Integer getAge() 
        return age;
    
    public void setAge(Integer age) 
        this.age = age;
    
    public Boolean getBoss() 
        return boss;
    
    public void setBoss(Boolean boss) 
        this.boss = boss;
    
    public Date getBirth() 
        return birth;
    
    public void setBirth(Date birth) 
        this.birth = birth;
    
    public Map<String, Object> getMaps() 
        return maps;
    
    public void setMaps(Map<String, Object> maps) 
        this.maps = maps;
    
    public List<Object> getLists() 
        return lists;
    
    public void setLists(List<Object> lists) 
        this.lists = lists;
    
    public Dog getDog() 
        return dog;
    
    public void setDog(Dog dog) 
        this.dog = dog;
    
    @Override
    public String toString() 
        return "Person [lastName=" + name + ", age=" + age + ", boss=" + boss + ", birth=" + birth + ", maps="
                + maps + ", lists=" + lists + ", dog=" + dog + "]";
    


对应bean类的书写,Dog:

package com.example.demo.bean;



public class Dog 
    private String name;
    private Integer age;
    public Dog() 
        super();
    
    public Dog(String name, Integer age) 
        super();
        this.name = name;
        this.age = age;
    
    public String getName() 
        return name;
    
    public void setName(String name) 
        this.name = name;
    
    public Integer getAge() 
        return age;
    
    public void setAge(Integer age) 
        this.age = age;
    
    @Override
    public String toString() 
        return "Dog [name=" + name + ", age=" + age + "]";
    


实现类的书写:

package com.example.demo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.bean.Person;

@RestController
public class PersonController 

    @Autowired
    private Person person;

    @RequestMapping("person")
    public String  person()
        return person.toString();
    

现在我们来说明一下,实现类的两个注解:
@Component:@component就是说把这个类交给Spring管理
@ConfigurationProperties(prefix=”person”):使用@ConfigurationProperties,它可以把同类的配置信息自动封装成实体类

springboot框架学习学前掌握之重要注解-通过java的配置方式进行配置spring

...点注解理解声明:本文是《凯哥陪你学系列-框架学习之springboot框架学习》中springboot框架学习学前掌握之重要注解(2)-通过java的配置方式进行配置spring.在上一节《springboot框架学习学前掌握之重要注解(1)-spring的java配置方式》我们... 查看详情

springboot框架学习学前掌握之重要注解-java配置方式

...建  声明:  本文是《凯哥陪你学系列-框架学习之springboot框架学习》中学前掌握之重要注解(1)  java配置是spring4.x推荐的撇嘴方式。可以完全代替xml配置。  1:重点注解  @configuration和@bean注解  说明:  @configura... 查看详情

java框架之springboot二:springboot配置获取

java框架之Springboot二:SpringBoot配置获取resources文件夹中的目录结构:static:保存所有的静态资源;js,css,imagestemplates:保存所有的模板页面;application.properties:配置文件,可以修改一些默认配置配置文件... 查看详情

java进阶之springboot

...置开发和构建,打包和部署应用程序----------创建:1.使用SpringBootCLI工具2.使用SpringSTSIDE3.使用SpringIni 查看详情

springboot---之ssm框架整合

1.pom依赖:即:spring-boot的基本jar----内置springmvc和springThymeleafjar热部署jar---方便二次加载ctrl+f9再次编译MybatisjarMysqljar<dependency> <groupId>org.springframework.boot</groupId><artifactId>spr 查看详情

java框架之springboot一:hellospringboot!

java框架之Springboot一:helloSpringBoot!springboot的意义:简化搭建spring框架的过程,节省时间,让程序员把更多的时间放在实现内部逻辑上。springboot搭建的快速方法:1.登陆https://start.spring.io/2.选择自己需要选择的... 查看详情

springboot+mybatis框架之@selectprovider注解方式搭建

...成。这次使用@SelectProvider标签的方式搭建一次。一、搭建SpringBoot的项目  https://start.spring.io/自己配置SpringBoot的项目,点击“GenerateProject”按钮就可以下载下来一个配置好的SpringBoot项目。   二、项目结构   &nbs 查看详情

springboot(spring)

一:spring的介绍   Spring是一个开源框架,它由RodJohnson创建。它是为了解决企业应用开发的复杂性而创建的。  它是一个容器框架,用来装javabean(java对象),中间层框架(万能胶)可以起一个连接作用,比如说把Struts... 查看详情

二java框架之spring注解开发(代码片段)

文章目录1.IOC/DI注解开发1.1Component注解@Component@Controller@Service@Repository1.2纯注解开发模式1.3注解开发bean管理@Scope@PostConstruct@PreDestroy1.4注解开发依赖注入@Autowired@Qualifier@Value@PropertySource1.5第三方bean... 查看详情

网红框架springboot2.x之框架简介及环境搭建

SpringBoot基于Spring框架进行“变态级“封装和扩展,由于上手简单、配置简单、集成简单,使得SpringBoot一跃成为近几年Java开发界的网红,加之众多的开源同僚的鼎力支持,为SpringBoot框架构建起了强大的开发生态圈。SpringBoot1.x已... 查看详情

java之springboot+springsecurity+vue实现后台管理系统的开发二后端(代码片段)

Java之SpringBoot+SpringSecurity+Vue实现后台管理系统的开发【一、前端】跳转Java之SpringBoot+SpringSecurity+Vue实现后台管理系统的开发【二、后端】跳转Java之SpringBoot+SpringSecurity+Vue实现后台管理系统的开发【三、系统权限... 查看详情

java框架之springboot三:springboot自定义配置一

java框架之Springboot三:SpringBoot自定义配置一私有化配置文件刚才我们介绍了在主配置文件汇中配置对应的文件,如果我们想要自定义配置文件该怎么处理呢?现在就要给大家介绍我们的@PropertySource注解。@PropertyS... 查看详情

java之springboot入门到精通idea版springboot整合其他框架junit,redis,mybatis(一篇文章精通系列)中(代码片段)

SpringBoot整合其他框架【Junit,Redis,MyBatis】一、SpringBoot整合Junit①搭建SpringBoot工程②引入starter-test起步依赖③编写测试类(1)在启动类傍边其他类④添加测试相关注解⑤编写测试方法二、SpringBoot整合Redis1、搭建SpringBoot工... 查看详情

spring框架之ioc容器和aop详解(代码片段)

主要分析点:一、Spring开源框架的简介 二、Spring下IOC容器和DI(依赖注入Dependencyinjection)三、Spring下面向切面编程(AOP)和事务管理配置 一、Spring开源框架的简介   Spring是一个开源框架,Spring是于2003年兴起的一个轻量... 查看详情

##走过路过不容错过之##javaee框架篇一spring

...Spring中@Autowired和@Resource的区别?你给我说一下SpringBoot吧?SpringCloud的常用组件挨个介绍一下?SpringBoot和SpringMVC与springClou 查看详情

springboot之参数

spring.config.name指的是springboot启动的入口配置文件。默认是application.properties/yaml/yml.如:java-jarxxx.jar--spring.config.name=myApplication;spring.config.location作用与spring.config.name一样。但是,要比spring.config.name指定的 查看详情

java面试之springboot/springcloud

104.什么是springboot?springboot是为spring服务的,是用来简化新spring应用的初始搭建以及开发过程的。105.为什么要用springboot?配置简单独立运行自动装配无代码生成和 xml配置提供应用监控易上手提升开发效率106.springboot核心配... 查看详情

java面试之springboot/springcloud

104.什么是springboot?springboot是为spring服务的,是用来简化新spring应用的初始搭建以及开发过程的。105.为什么要用springboot?配置简单独立运行自动装配无代码生成和 xml配置提供应用监控易上手提升开发效率106.springboot核心配... 查看详情