【Spring Boot】07 @PropertySource 和 @ImportResource

Spring Boot自定义配置属性源(PropertySource) 主要介绍了Spring Boot自定义配置属性源(PropertySource),小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧 立即下载

@PropertySource :加载指定的配置文件

@PropertySource(value = {"classpath:person.properties"})
@Component
public class Person {
    private String lastName;
    private Integer age;
    private Boolean boss;
    private Date birth;

    private Map<String, Object> maps;
    private List<Object> lists;
    private Dog dog;

@ImportResource:导入 Spring 的配置文件,让配置文件里面的内容生效

Spring Boot 里面没有 Spring 的配置文件,我们自己编写的配置文件,也不能自动识别;

如果想让 Spring 的配置文件生效加载进来,把@ImportResource标注在一个配置类上

package com.memory.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ImportResource;

// 导入 Spring 的配置文件让其生效
@ImportResource(locations = {"classpath:beans.xml"})
@SpringBootApplication
public class SpringBoot02ConfigApplication {

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

不来编写 Spring 的配置文件

<?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="helloService" class="com.memory.springboot.service.HelloService"></bean>
</beans>

SpringBoot 推荐给容器中添加组件的方式:推荐使用权注解的方式

1、配置类 —— Spring 配置文件
2、使用 @Bean 给容器中添加组件
package com.memory.springboot.config;

import com.memory.springboot.service.HelloService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Configuration:指明当前类是一个配置类,就是来代替之前的 Spring 配置文件
 *
 * 在配置文件中用 <bean></bean> 标签添加组件
 */
@Configuration
public class MyAppConfig {

    // 将方法的返回值添加到容器中:容器中的这个组件默认的 Id 就是方法名
    @Bean
    public HelloService helloService(){
        System.out.println("配置类 @Bean 给容器中添加组件");
        return new HelloService();
    }
}
opencv实现“找茬” // 111.cpp : 定义控制台应用程序的入口点。 // #include "stdafx.h" #include #include #include using namespace std; #define CVX_RED CV_RGB(0xff,0x00,0x00) #define CVX_BLUE CV_RGB(0x00,0x00,0xff)//定义宏,轮廓边框颜色 in 阅读详情

相关推荐

如何使用AutoDl平台跑自己的模型(纯干货,步骤很详细)

记录我使用AutoDL云服务器来跑自己模型的全过程

qq_45073592的博客 3万+

SpringBoot中的 @PropertySource

前言 我们在使用 SpringBoot 的过程中经常会接触到需要注入配置文件中的信息,就比如 resources 目录下的 application.properties,因为 SpringBoot 中默认的配置文件就是它,所以我们不需要额外的配置,直接在其中写上我们需要的信息即可,但是要是我们想自己在其他目录下建配置文件该怎么办呢,那么坐好了,我要开始放大招了。。。 示例 示例目录

张先森的博客 1万+

【生产实践】SSH服务弱加密算法修复指南(附完整操作流程)

SSH服务弱加密算法漏洞修复方案 检测发现SSH服务存在支持弱加密算法漏洞,主要风险包括: 使用arcfour、aes128-cbc等易被破解的算法 面临密钥恢复、重放攻击等安全威胁 提供两种修复方案: 推荐方案:升级OpenSSH至最新版本 替代方案:修改SSH配置禁用弱算法(需注意可能影响旧客户端连接) 修复步骤: 编辑/etc/ssh/sshd_config 注释或删除弱算法配置 添加强加密算法(如aes128-ctr等) 重启SSH服务 注意事项: 建议先在测试环境验证 可能需配合客户端升级 操作前

SunMy的博客 3100

Spring Boot 注解(五)@PropertySource

用法: @PropertySource 注解提供便利和声明的机制添加 PropertySource 到Spring的 Environment @PropertySource 和 @Value组合使用,可以将自定义属性文件中的属性变量值注入到当前类的使用@Value注解的成员变量中 @PropertySource 和 @ConfigurationProperties组合使用,可以将属性文件与一个Java类绑定,将属性文件中的变量值注入到该Java类的成员变量中 例子: db.properties.

yiguang_820的博客 1702

玄机-Linux权限维持-后门

玄机-Linux权限维持-后门

m0_46439803的博客 764

基于立创开源ESP32步进电机驱动板硬件设计与优化实践

本文详细介绍了基于立创开源平台的ESP32步进电机驱动板硬件设计与优化实践。重点解析了ESP32-PICO-D4与A4950驱动芯片的选型搭配、电源电路噪声隔离、PCB布局布线技巧以及保护电路设计,为开发者提供了从原理图到PCB的完整优化方案,有效提升驱动板的性能与稳定性。

7z8x2c4v6b9的博客 647

Spring Boot @PropertySource 加载配置文件、@ImportResource 导入Spring 配置文件、YamlPropertiesFactoryBean 读取 yml 文件

目录 需求背景 项目结构 @PropertySource 加载指定配置文件 资源配置文件 测试运行 需求背景 1、通过《Spring Boot 全局配置文件》与《@Value 取赋值详解与 @ConfigurationProperties 对比》已经知道使用“@Value”注解与“@ConfigurationProperties”可以从全局配置文件“application.prope...

蚩尤后裔-汪茂雄 6万+

SAP S4 MM模块自动记账全流程解析:从评估范围到科目分组的实战配置

本文深入解析SAP S4 MM模块自动记账的全流程,从核心概念评估范围与评估控制入手,详细阐述了评估类、科目分组等关键配置的逻辑关系。通过实战配置步骤,指导如何建立从物料移动(如收货、发料)到自动生成准确财务凭证的完整体系,确保库存价值与总账实时同步,为企业构建可靠的财务业务一体化基石。

weixin_29313547的博客 423

Spring Boot 中的 PropertySource 是什么,原理,如何使用

首先,您需要在应用程序中定义属性。在上面的代码中,我们定义了两个属性:myapp.name 和 myapp.version。这些属性将在应用程序中使用。

程序猿徐师兄的博客 1912

AI大模型驱动的智能座舱研发体系重构

AI大模型正重构智能座舱研发体系的核心逻辑,需以场景认知为起点、混合架构为支柱、AI原生生态为落脚点。未来,研发体系需持续融合认知科学、边缘计算与开放生态理论,推动座舱从“工具”进化为“伙伴”。

我的专栏 1916

R数据分析:潜在剖面分析LPA的做法与解释

Latent Profile Analysis (LPA) tries to identify clusters of individuals (i.e., latent profiles) based on responses to a series of continuous variables (i.e., indicators). LPA assumes that there are unobserved latent profiles that generate patterns of resp.

Codewar的博客 1万+

SpringBoot - @PropertySource注解使用详解

SpringBoot默认能够读取resources目录下的配置文件,当我们的配置文件不在SpringBoot默认能够读取的目录下怎么办?@PropertySource,可以加载指定的、非application.properties文件的、未在SpringBoot默认加载目录的属性文件(*.properties)到Spring容器中,@PropertySource目前还不支持对YML配置的读取。 ......

记录并分享 2726

Vue3 - 完美解决html2canvas截图不全问题,截图只截取当前可视区域导出图片不全,截屏导出的图片显示不全只有一部分或缺一块,vue3移动端H5网页项目使用html2canvas插件长截图!

vue3,html2canvas,移动端H5网页,截图不全,html2canvas长截图不全,保存长截图,页面滚动导致截图不全,网页下方截屏不到怎么办,底部一部分无法截屏到,保存的图片少了一大块,H5海报截图不完整,怎么截取滚动条下方的内容,截图图时图片不全,截图图片只有一半的问题,底部内容空白,截图空白问题的解决,截图不全怎么办,html2canvas.js网页截图功能(解决截图不全问题,移动端ios系统截图不完整,html2canvas截图太长又不能删减截取不到,使用HTML2Canvas在H5页面上实

🏆 前端领域优质创作者 1万+

SpringBoot中@ConfigurationProperties,@PropertySource和@ImportResource

前言:最近在复习springboot的相关知识,所以在这里记录一下方便自己以后复习。 1.@ConfigurationProperties 标注了这个注解的类,告诉springboot,所有的相关属性,都与配置文件里设置的属性进行绑定。用@ConfigurationProperties中可以设置prefix = "xxxx"前缀来标识。 student: name: jack age:...

莫失莫忘的博客 2000

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

从零开始的Spring Boot学习【七】【@PropertySource和@ImportResource】

我们本章来讲一下这两个注解: @PropertySource @ImportResource @PropertySource: 在Spring Boot中,默认是会加载application.properties和application.yml文件中的数据。但是如果我们放实体类中的属性值,不想写到这个全局的配置文件中,我们要怎么办?这个时候@PropertySource这个注解就发挥作用了...

KnightHONG的博客 1501

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

@ImportResource和@PropertySource两个注解的区别

先看第一种方法;@ImportResource @ImportResource:注解的文件格式和内容应该是xml文件。并且xml内容中必须包含以下信息。 注解:@ImportResource(“classpath:Beans.xml”) <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springfr...

M983373615的博客 1955

Spring注解之@ImportResource和@PropertySource分析

1:写在前面 之前我们使用spring,最多的还是通过xml配置文件的方式来配置spring bean等内容,随着注解的广泛和spring4中Java config的引入,xml配置文件方式逐步被替换,但是如果是想要使用xml配置文件方式的话,也可以通过@ImportResource注解来实现,下面我们来一起看下如何使用。 2:ImportResource ...

wang0907的博客 671

(Spring Boot教程四 )使用注解@Value、@PropertySource、@ImportResource、@bean进行配置,以及配置文件加载顺序

上一篇教程我们使用了application.properties和application.yml进行项目的配置,这篇博客将告诉你更多Spring Boot项目配置的方式 目录 0.项目结构及代码 1.使用@Value进行配置 1.1 简介 1.2 可注入类型 1.2.1 注入类型和注入方法 1.2.2 字面量:包括布尔、数值型、字符串等。 1.2.3数组(List) 1.2.4...

zhanggonglalala的博客 4501

spring boot注解之@PropertySource和@ImportResource

@PropertySource:加载指定的配置文件 @PropertySource(value = "classpath:person.properties") @Component @ConfigurationProperties(prefix = "person") public class Person { @ImportResource:导入Spring的配置文件,让配置文件中的内容生效;...

weixin_44535446的博客 176

@PropertySource和@ImportResource和@Bean的使用

@PropertySource和@ImportResource和@Bean的使用 @PropertySource:加载指定的配置文件: /** * 将配置文件中配置的每一个属性的值,映射到这个组件中 * @ConfigurationProperties:告诉Springboot将本类中的所有属性和配置文件中相关的配置进行绑定; * prefix = "person":指明与配置文件中哪个下面的所有属性进行一一映射。 * * @ConfigurationProperties:默认从全局配置文件

cui1004的博客 216

(六)SpringBoot入坑-5@PropertySource和@ImportResource

文章目录@PropertySource@ImportResource推荐方式通过@Bean @PropertySource @propertySource : 加载指定的配置文件 创建一个person.properties文件 person.last-name=李四 person.boss=true #list 类型 person.lists=a,b,c #map 类型 person.maps.k...

张不帅 1191

spring boot @ImportResource和配置类

@PropertySource:默认会加载application.properties/application.yml文件中的数据: 例如@PropertySource(value={"classpath:conf.properties"}) 加载conf.properties 但是@PropertySource只能加载properties,不能加载yml @...

zyfzhangyafei的专栏 1710

Spring Boot注解之@PropertySource与@ImportResource

@PropertySource:加载指定的配置文件。 @Component @ConfigurationProperties(prefix = "person") @PropertySource(value = {"classpath:person.properties"}) public class Person { //@Value("${person.last-name}") ...

Dannie的博客 231

SpringBoot——【@PropertySource()和@ImportResource()注解说明】

@PropertySource()和@ImportResource()注解说明 @PropertySource() 加载指定配置文件 将指定配置文件中配置、映射的指定类的属性值中、 如: 这是一个类要映射指定配置文件的值到类中 @Component @ConfigurationProperties(prefix = "person") @PropertySource(value = {"classpath:person.properties"})//加载person.properties publi

weixin_44432069的博客 403

@PropertySource注解和@ImportResource注解

@ConfigurationProperties(prefix = “person”)的作用是告诉spring boot该类在配置文件中的前缀是什么,但是类的属性值的配置必须写在全局配置文件中,这样一来等到项目越来越庞大的时候,单靠一个全局配置文件就显得太过臃肿,所以@PropertySource注解应运而生。 @PropertySource注解: 向spring boot说明该类的配置文件的地址。 package com.springboot.bean; import org.springframewo

Liang_toto的博客 439

Spring Boot入门(8):@PropertySource和@ImportResource的区别

1、@PropertySource导入制定配置文件 ​ (1)、application.properties是全局配置文件@ConfigurationProperties用来加载全局配置文件中的配置信息进行映射,如果所有东西都放咋全局配置文件中则太不合理。可以专门写一个person.properties专门的配置文件。 ​ (2)、在person.properties中配置JavaBean的pe...

weixin_42701161的博客 190

spring-boot学习四:spring boot配置@PropertySource、@ImportResource、@Bean

1.@PropertySource:导入指定配置文件 我们前面看到的加载配置信息的时候,前提是我们将配置信息都写在了spring boot的全局配置文件application.properties(或者application.yaml)文件中。spring boot还为我们准备了导入指定配置文件的注解@PropertySource; 1.1使用示例 建立一个Student类,里...

weixin_30916125的博客 207
上一篇: 【Spring Boot】06 @Value 与 @ConfigurationProperties
下一篇: 新建vue项目卡在Project description
ZyhMemory
博客等级 码龄10年 43粉丝 165原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值