SpringBoot_4

2023-10-29

1.@PropertySource
@PropertySource:加载指定的配置文件【properties】.
先前我们通过@ConfifigurationProperties加载全局配置文件中的值到javabean中,但是我们在具体使用的时候不会把所用的配置都保存在全局配置文件中的,可能会将不同的配置保存在不同的配置文件中,那么这时我们就需要@PropertySource注解为指定的javabean类加载指定的配置文件
例如:

package com.wangxing.springboot.bean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
@Component
@PropertySource(value = {"classpath:userbean.properties"})
@ConfigurationProperties(prefix = "person")
public class Userbean {
    private int userid;
    private String username;
    private int userage;
    private String useraddress;

    public int getUserid() {
        return userid;
    }

    public void setUserid(int userid) {
        this.userid = userid;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public int getUserage() {
        return userage;
    }

    public void setUserage(int userage) {
        this.userage = userage;
    }

    public String getUseraddress() {
        return useraddress;
    }

    public void setUseraddress(String useraddress) {
        this.useraddress = useraddress;
    }
}

person.userid=18
person.username=张三
person.userage=50
person.useraddress=西安
package com.wangxing.springboot.controller;
import com.wangxing.springboot.bean.Userbean;
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 UserController {
    @Autowired
    private Userbean userbean;
    @RequestMapping(value = "get")
    @ResponseBody
    public String get(){
        String userinfo=userbean.getUserid()+"\t"+userbean.getUsername()+"\t"+userbean.getUserage()+"\t"+userbean.getUseraddress();
        return  userinfo;
    }
}

2.@ImportResource
@ImportResource:导入基于XML的Spring的配置文件,让配置文件里面的内容生效

package com.wangxing.springboot.bean;

public class PersonBean {
    private int perid;
    private String pername;
    private int perage;

    public int getPerid() {
        return perid;
    }

    public void setPerid(int perid) {
        this.perid = perid;
    }

    public String getPername() {
        return pername;
    }

    public void setPername(String pername) {
        this.pername = pername;
    }

    public int getPerage() {
        return perage;
    }

    public void setPerage(int perage) {
        this.perage = perage;
    }

    public String getPeraddress() {
        return peraddress;
    }

    public void setPeraddress(String peraddress) {
        this.peraddress = peraddress;
    }

    private String peraddress;
}

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
      <bean id="personBean" class="com.wangxing.springboot.bean.PersonBean">
          <property name="perid" value="1004"></property>
          <property name="perage" value="22222"></property>
          <property name="pername" value="张三"></property>
          <property name="peraddress" value="西安"></property>

      </bean>
    <bean name="/get1" class="com.wangxing.springboot.controller.TestController">
        <property name="personBean" ref="personBean"></property>
    </bean>
</beans>
package com.wangxing.springboot.controller;

import com.wangxing.springboot.bean.PersonBean;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class TestController implements Controller {
    private PersonBean personBean;

    public PersonBean getPersonBean() {
        return personBean;
    }

    public void setPersonBean(PersonBean personBean) {
        this.personBean = personBean;
    }

    @Override


    public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
        String personinfo=personBean.getPerid()+"\t"+personBean.getPername()+"\t"+personBean.getPerage()+"\t"+personBean.getPeraddress();
        System.out.println(personinfo);
        return null;
    }
}

Spring Boot里面没有Spring的配置文件,我们自己编写的配置文件,也不能自动识别;
想让Spring的配置文件生效,加载进来;@ImportResource标注在一个主类上.

3.@Bean
@Bean添加在方法上,告诉springIOC容器创建javabean类对象
方法的返回值就是所要创建javabean类对象
方法的名称就是javabean类对象的名称
< bean id=“对象的名称” class=“所要创建javabean类对象”>
注意:@Bean必须出现在配置类中【@Configuration标注的java类】
例如:

package com.wangxing.springboot.bean;

public class Hello {
    public void gethello(){
        System.out.println("测试hello的方法");
    }
}

package com.wangxing.springboot.bean;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MyApp {
    @Bean
    public Hello gethello(){
        return  new Hello();
    }
}

package com.wangxing.springboot.controller.Hello;

import com.wangxing.springboot.bean.Hello;
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 controller {
    @Autowired
    private Hello hello;
    @RequestMapping(value = "hello")
    @ResponseBody
    public String getbean(){
        hello.gethello();
        return"Bean小测试";

    }
}

package com.wangxing.springboot.springbootdemo11;

import com.wangxing.springboot.bean.MyApp;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;

@SpringBootApplication
@ComponentScan("com.wangxing.springboot")
@Import(MyApp.class)
public class Springbootdemo11Application {

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

}

配置文件【application.properties】中的占位符
1、随机数
r a n d o m . v a l u e 、 {random.value}、 random.value{random.int}、${random.long}
r a n d o m . i n t ( 10 ) 、 {random.int(10)}、 random.int(10){random.int[1024,65536]}
例如:

package com.wangxing.springboot.controller.Hello;

import com.wangxing.springboot.bean.StudentBean;
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 StudentController {
    @Autowired
    private StudentBean studentBean;
    @RequestMapping(value = "student")
    @ResponseBody
    public String test(){
        System.out.println(studentBean.getStuid());
        System.out.println(studentBean.getSutage());
        System.out.println(studentBean.getStuname());
        System.out.println(studentBean.getStuaddress());
        return "成功了";
    }
}

package com.wangxing.springboot.bean;

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

@Component
@ConfigurationProperties(prefix = "student")
public class StudentBean {
    private int stuid;
    private String stuname;
    private int sutage;
    private String stuaddress;

    public int getStuid() {
        return stuid;
    }

    public void setStuid(int stuid) {
        this.stuid = stuid;
    }

    public String getStuname() {
        return stuname;
    }

    public void setStuname(String stuname) {
        this.stuname = stuname;
    }

    public int getSutage() {
        return sutage;
    }

    public void setSutage(int sutage) {
        this.sutage = sutage;
    }

    public String getStuaddress() {
        return stuaddress;
    }

    public void setStuaddress(String stuaddress) {
        this.stuaddress = stuaddress;
    }
}

Profiles
1.Profiles文件就是用来配置在不同环境下的配置数据。
2.因为在不同的环境下配置文件中配置的运行环境的数据是不同的,所以我们就需要灵活的在不同的运行环境下切换成对应的运行环境的数据,此时我们将不同的运行环境数据,配置到不同的配置文件中,通过在主配置文件application.properties中的spring.profiles.active属性完成切换。
测试.properties配置

application-dev.properties【开发环境配置】
server.port=8080
application-prod.properties【生产环境配置】
server.port=9090
application.properties 【主配置】
spring.profiles.active=prod 【指定使用生产环境配置】
或者
spring.profiles.active=dev 【指定使用开发环境配置】

测试.yml配置

application-devyml.yml【开发环境配置】
server: 
port: 8080
application-prodyml.yml【生产环境配置】
server: 
port: 9090
application.yml 【主配置】
spring: 
profiles: 
active: prodyml 【指定使用生产环境配置】
或者
spring: 
profiles: 
active: devyml 【指定使用开发环境配置】

上面是通过在1.主配置文件中切换运行环境配置
还可以通过配置2.运行环境参数配置视图窗口来指定具体使用哪一个运行环境
“–spring.profiles.active=dev“

还可以通过3.命令行运行jar的时候指定具体使用哪一个运行环境
java -jar testspringboot002-0.0.1-SNAPSHOT.jar --spring.profiles.active=dev;
还可以通过4.配置虚拟机参数指定具体使用哪一个运行环境;
“-Dspring.profiles.active=dev”

注意:运行环境配置文件的名称 application-{profiles}.properties/yml
主配置文件加载位置
spring boot 启动会扫描以下位置的application.properties或者 application.yml文件作为Spring boot的默认配置文件
– 项目根目录/config/
– 项目根目录/
– resource/config/
– resource:/
以上是按照优先级从高到低的顺序,所有位置的文件都会被加载,高优先级配置内容会覆盖低优先级配置内容。
SpringBoot会从这四个位置全部加载主配置文件;互补配置
我们也可以通过配置spring.config.location来改变默认配置
项目打包好以后,我们可以使用命令行参数的形式,启动项目的时候来指定配置文件的新位置;指定配置文件和默认加载的这些配置文件共同起作用形成互补配置;
java -jar testspringboot02-0.0.1-SNAPSHOT.jar --spring.confifig.location=F:/application.properties
外部配置加载顺序
Spring Boot 支持多种外部配置方式

  1. 命令行参数
  2. 来自java:comp/env的JNDI属性
  3. Java系统属性(System.getProperties())
  4. 操作系统环境变量
  5. RandomValuePropertySource配置的random.*属性值
  6. jar包外部的application-{profile}.properties或application.yml(带spring.profile)配置文件
  7. jar包内部的application-{profile}.properties或application.yml(带spring.profile)配置文件
  8. jar包外部的application.properties或application.yml(不带spring.profile)配置文件
  9. jar包内部的application.properties或application.yml(不带spring.profile)配置文件
    优先加载带profifile,再来加载不带profifile,由jar包外向jar包内进行寻找
  10. @Configuration注解类上的@PropertySource
  11. 通过SpringApplication.setDefaultProperties指定的默认属性
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

SpringBoot_4 的相关文章

  • JavaScript中使用画布实现笑脸火柴人

    在这之前 根本不知道JavaScript具体到底有多重要 现在才明白JavaScript也很强大 从网上看了几个js写的网页小游戏 我都惊呆了 以后一定要好好学习js
  • 【华为OD统一考试B卷

    在线OJ 已购买本专栏用户 请私信博主开通账号 在线刷题 运行出现 Runtime Error 0Aborted 请忽略 华为OD统一考试A卷 B卷 新题库说明 2023年5月份 华为官方已经将的 2022 0223Q 1 2 3 4 统一
  • 数据可视化 第4章

    第4章 添加表格QTableView 1 添加table model py 里面子类化QAbstractTableModel 实现自定义table model from PySide2 QtCore import Qt QAbstractT
  • An Introduction to UE4 Plugins

    An Introduction to UE4 Plugins Rate this Article 3 67 3 votes Approved for Versions 4 2 4 3 4 4 4 5 Contents hide
  • <OpenCV> Mat属性

    OpenCV的图像数据类型可参考之前的博客 https blog csdn net thisiszdy article details 120238017 OpenCV Mat类型的部分属性如下 size 矩阵的大小 s i z e
  • VMware虚拟机网络设置(三种网络模式)

    VMware虚拟机网络设置 三种网络模式 VMware网络使用windows虚拟机客户端时一般默认NAT模式自动可以上网 近日安装macos时上网却不行 网上搜索后自己整理出来 对三种模式自己的看法 首先 找到编辑 gt 虚拟网络编辑器 虚

随机推荐

  • k数和

    思路 这道题感觉是一个非常好的动态规划的题目 动态规划方程 d i j target d i 1 j target d i 1 j 1 target a i d i j t a
  • pigz搭配tar开启不了多线程,还是很慢

    Q pigz搭配tar开启不了多线程 还是很慢 A 注意你的压缩的文件夹或文件的名字不要包含 字符 其他字符未尝试
  • Java代码中对文件的操作

    引言 这几天的项目涉及到了文件的操作 我这边做一下整理 环境说明 jdk版本 1 8 0 311 对文件的操作 1 保存文件 保存文件 param file 文件 param path 文件保存目录 param name 保存后的文件名字
  • 怎么在浏览器的控制台上换行输入?

    使用快捷键 shift enter 如
  • python numpy.meshgrid()函数的用法

    numpy meshgrid xi kwargs 从一个坐标向量中返回坐标矩阵 参数 x1 x2 xn array like 表示网格坐标的一维数组 indexing xy ij 可选 笛卡尔 xy 默认值 或矩阵 ij 输出索引 spar
  • matlab跟踪目标图像边缘并计算长轴短轴

    题目如下 对图像进行处理和分析 跟踪出目标边缘 并计算出目标的长轴和短轴及方向 原始图像如图1所示 图1 原始图像 一 处理过程 1 边缘提取 目标边缘提取在图像处理学科中属于数学形态学 在提取目标边缘之前有认真思考过应该用什么方法 是用4
  • java的内存机制以及变量的作用域

    初学者对于Java的类和对象往往一头雾水 尤其是当涉及到程序细节的时候 出现混乱似乎在所难免 笔者根据自己的学习经验 认为 理解java的内存机制以及变量的种类和作用域 对于精确把握编程规则相当重要 一 java内存机制 java程序在内存
  • Springboot整合Mysql集群

    文章目录 一 方法一 1 1 默认配置 1 2 需要自定义配置 1 3 自定义数据库配置类 1 4对从库进行操作 在写一个配置类 一 方法一 1 1 默认配置 1 2 需要自定义配置 1 3 自定义数据库配置类 第一步 添加连接池驱动 第二
  • Java内存的Used、Committed、Max的区别

    不想看英文 可直接看最后的结论 A MemoryUsage object represents a snapshot of memory usage Instances of the MemoryUsage class are usuall
  • 【C语言进阶】文件操作(二)

    大家好我是沐曦希 文件操作 1 前言 2 文件的随机读写 2 1 fseek函数 2 2 ftell函数 2 3 rewind 3 文本文件和二进制文件 4 文件读取结束的判定 4 1 被错误使用的feof 5 文件缓冲区 6 写在最后 1
  • rsync远程同步实现快速、安全、高效的异地备份

    目录 一 rsync介绍 1 rsync是什么 2 rsync同步方式 3 rsync的特性 4 rsync的应用场景 5 rsync与cp scp对比 6 rsync同步源 二 rsync命令 1 常用选项 2 实例 本地复制对比 3 配
  • React(二):React开发神器Webpack

    React 一 React的设计哲学 简单之美 React 二 React开发神器Webpack React 三 理解JSX和组件 React 四 虚拟DOM Diff算法解析 React 五 使用Flux搭建React应用程序架构 上一篇
  • 基于LinearLayout的小标签(TextView)自动换行(修改)

    设计最初是因为公司项目需要多处显示多个小标签 并且需要多行展示 最开始使用的GridLayout 但是这个网格布局局限性太高 标签是动态的 内容也不定 用GridLayout就会有多行占用的各种显示问题 所以后来换成了LinearLayou
  • 【源码学习】正则表达式

    模式 Patterns 和修饰符 flags 正则表达式是提供了一种在文本中进行搜索和替换的强大的方式的模式 在 JavaScript 中 我们可以通过 RegExp 对象使用它们 也可以与字符串方法结合使用 正则表达式 正则表达式 可叫作
  • 如何制作一个微信小程序【微信小程序是怎么做的】

    为什么现在这么多人使用微信小程序呢 因为微信小程序除了便捷易开发 公司企业可以用来做小程序展示官网 商家也可以做小程序商城 甚至个人也可以拥有自己的小程序 那么如何制作一个微信小程序 微信小程序是怎么做的呢 下面给大家说说简单的流程 一 要
  • IGBT的绘制与逆变器的绘制-Visio制图总结【电控类】(三)

    在逆变器非线性补偿研究方向的文章中看到了一些逆变器的示意图 发现均有手绘的痕迹 碰巧在AxGlyph软件中有IGBT这个元件 就截图仿着画了一张IGBT的图 下文中会给出一张IGBT图片和两张逆变器图片 绘图步骤 把截到的IGBT图片粘贴到
  • 一篇搞定Linux和IOS或Android通讯(usbmuxd、libimobiledevice、libusb、AOA)

    1 Linux要与苹果手机通讯需要两个组件 1 usbmuxd 是苹果的一个服务 这个服务主要用于在USB协议上实现多路TCP连接 将USB通信抽象为TCP通信 苹果的iTunes Xcode 都直接或间接地用到了这个服务 参考链接 htt
  • 阻止回车提交表单

  • UNCTF2022 wp Misc magic_word

    知识点 GBK编码 UTF 8编码 零宽度隐写 原题链接 WP 通过文档开篇打油诗中的锟斤拷可以很明显知道此题和UTF 8编码与GBK编码有关 将内容通过UTF 8编码可以得到提示 零宽隐写 将全文清除格式可以得到正文 再使用解码工具解码后
  • SpringBoot_4

    1 PropertySource PropertySource 加载指定的配置文件 properties 先前我们通过 ConfifigurationProperties加载全局配置文件中的值到javabean中 但是我们在具体使用的时候不