首页 > 其他分享 >【spring】@PropertySource 注解学习

【spring】@PropertySource 注解学习

时间:2024-03-15 17:31:59浏览次数:22  
标签:PropertySource 配置文件 spring dataSource Spring 注解 annotation 加载

@PropertySource介绍

@PropertySource是Spring框架中的一个注解,主要用于Java配置类中,用于引入额外的属性文件,以便在Spring应用上下文中使用这些属性。

在Spring 3.1引入Java配置后,我们可以通过@Configuration注解的类和@Bean注解的方法来进行组件扫描和依赖注入配置。但是,对于一些外部化配置(如数据库连接信息、邮件服务器配置等),我们通常会放在properties或yml文件中,这时就可以使用@PropertySource来加载这些属性文件。

使用示例
@Configuration
@PropertySource("classpath:application.properties")
public class AppConfig {

    @Autowired
    private Environment env;

    @Bean
    public DataSource dataSource() {
        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setDriverClassName(env.getProperty("jdbc.driverClassName"));
        dataSource.setUrl(env.getProperty("jdbc.url"));
        dataSource.setUsername(env.getProperty("jdbc.username"));
        dataSource.setPassword(env.getProperty("jdbc.password"));
        return dataSource;
    }
}

@PropertySource源码

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Repeatable(PropertySources.class)
public @interface PropertySource {

	String name() default "";

	String[] value();
	
	boolean ignoreResourceNotFound() default false;
	
	String encoding() default "";

	Class<? extends PropertySourceFactory> factory() default PropertySourceFactory.class;
}
 
源代码截图

@PropertySource属性介绍

  • name:表示加载的资源的名称,如果为空,则会根据加载的配置文件自动生成一个名称。
  • value:表示加载的资源的路径,这个路径可以是类路径,也可以是文件路径。
  • ignoreResourceNotFound:表示当配置文件未找到时,是否忽略文件未找到的错误。默认值为false,也就是说当未找到配置文件时,Spring启动就会报错。
  • encoding:表示解析配置文件使用的字符集编码。
  • factory:表示读取对应配置文件的工厂类,默认的工厂类是PropertySourceFactory。

@PropertySource使用场景

  1. 配置文件的加载:在 Spring 应用程序中,通常需要加载一些配置文件,如数据库连接信息、服务器端口等。使用 @PropertySource 注解可以方便地加载这些配置文件,并将它们注入到 Spring 应用程序上下文中。
  2. 多环境配置:在开发和部署应用程序时,可能需要针对不同的环境(如开发环境、测试环境、生产环境等)进行不同的配置。使用 @PropertySource 注解可以方便地加载不同环境的配置文件,并根据环境变量或配置文件名来选择加载哪个配置文件。
  3. 动态配置:在某些情况下,可能需要在运行时动态地更改配置信息。使用 @PropertySource 注解可以方便地加载动态配置文件,并将它们注入到 Spring 应用程序上下文中。
  4. 属性文件的组织:在属性文件中,可能需要使用前缀来组织属性。使用 @PropertySource 注解的 prefix 属性可以指定属性文件中的前缀,以便在属性文件中使用前缀来组织属性。

@PropertySource测试示例代码

PropertySource配置类
package com.yang.SpringTest.annotation.propertySourceLearn;

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

/**
 * <p> PropertySource配置类</p>
 *
 * @author By: chengxuyuanshitang
 * Package com.yang.SpringTest.annotation.propertySourceLearn
 * Ceate Time 2024-03-15 17:04
 */
@Configuration
@PropertySource(value = "classpath:propertySourceDemo.properties")
public class PropertySourceConfig {

}
propertySourceDemo.properties
who=chengxuyuanshitang

do=Learn spring
PropertySourceTest测试类
package com.yang.SpringTest.annotation.propertySourceLearn;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;

/**
 * <p>PropertySource测试类</p>
 *
 * @author By: chengxuyuanshitang
 * Package com.yang.SpringTest.annotation.propertySourceLearn
 * Ceate Time 2024-03-15 17:07
 */

public class PropertySourceTest {
    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(PropertySourceConfig.class);
        ConfigurableEnvironment environment = context.getEnvironment();
        System.out.println("who is ? =: " + environment.getProperty("who"));
        System.out.println("do something ? = :" + environment.getProperty("do"));
    }
}
运行结果




标签:PropertySource,配置文件,spring,dataSource,Spring,注解,annotation,加载
From: https://blog.csdn.net/wochunyang/article/details/136744244

相关文章

  • SpringBoot启动过程
    SpringApplication的run方法    使用SpringFactoriesLoader读取listener和ApplicationContextInitializer    创建环境变量,读取配置信息      创建上下文(IoC容器,ApplicationContext)    预初始化上下文(prepareContext):将配置类读取成BeanDefinition    ......
  • RabbitMq 集成到 SpringBoot
    1.最低配置使用1.添加依赖<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><version>2.3.2.RELEASE</version></dependency><dependency>&......
  • java毕业设计-基于springboot开发的会员制医疗预约服务管理信息系统-毕业论文+答辩PPT
    文章目录前言一、毕设成果演示(源代码在文末)二、毕设摘要展示1、开发说明2、需求分析3、系统功能结构三、系统实现展示1、系统功能模块2、管理员功能模块3、医生功能模块3、会员功能模块四、毕设内容和源代码获取总结java毕业设计-基于springboot开发的会员制医疗预......
  • Java毕业设计-基于springboot开发的4S店车辆管理系统-毕业论文+答辩PPT(附源代码+演示
    文章目录前言一、毕设成果演示(源代码在文末)二、毕设摘要展示1.开发说明2.需求分析3、系统功能结构三、系统实现展示1、系统登录2、管理员功能模块3、销售员功能模块4、维修员功能模块四、毕设内容和源代码获取总结Java毕业设计-基于springboot开发的4S店车辆管理系......
  • springBoot 配置 国产达梦数据库
    1.pom<!--达梦数据库驱动--> <dependency> <groupId>com.dm</groupId> <artifactId>DmJdbcDriver18</artifactId> <version>1.8</version> </dependency>maven中央仓库里面没有,需要手动安装到maven本地仓库mvni......
  • Spring IOC与工厂模式
    1.简单介绍在讲SpringIOC之前,有必要先来聊一下工厂模式(FactoryPattern)。工厂模式可将Java对象的调用者从被调用者的实现逻辑中分离出来。工厂模式是Java中最常用的设计模式之一。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。在工厂模式中,我们在创建对象......
  • 【已解决】Mybatis-plus中@TableLogic注解失效问题
    逻辑删除逻辑删除是指通过修改数据的状态或添加额外字段来表示数据的删除状态,而不是直接从数据库中物理删除数据记录。通常,会在数据库表中新增一个字段(如deleted),用来标识数据是否被删除。MyBatisPlus中实现逻辑删除在使用MyBatisPlus进行数据库操作时,实现逻辑删除......
  • 猫头虎分享已解决Bug | org.springframework.dao.DuplicateKeyException异常的正确解
    博主猫头虎的技术世界......
  • Spring Schedule定时任务看这一篇就够了
    SpringSchedule 是指Spring框架提供的定时任务调度功能。SpringSchedule允许开发者在应用程序中便捷地创建和管理定时任务,比如按固定频率执行某些操作,或者按照cron表达式设定复杂的调度规则。SpringSchedule功能的依赖直接或间接地包含在 spring-boot-starter 家族中的相......
  • Java基于 Springboot+Vue 的招生管理系统,前后端分离
    博主介绍:✌程序员徐师兄、8年大厂程序员经历。全网粉丝15w+、csdn博客专家、掘金/华为云/阿里云/InfoQ等平台优质作者、专注于Java技术领域和毕业项目实战✌......