【spring】@PropertySource 注解学习

@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"));
    }
}
运行结果




相关推荐

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-16 21:50:02       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-16 21:50:02       106 阅读
  3. 在Django里面运行非项目文件

    2024-03-16 21:50:02       87 阅读
  4. Python语言-面向对象

    2024-03-16 21:50:02       96 阅读

热门阅读

  1. 什么是去中心化,如何去中心化

    2024-03-16 21:50:02       36 阅读
  2. 安装elasticsearch、kibana

    2024-03-16 21:50:02       45 阅读
  3. 架构师六大派Solid

    2024-03-16 21:50:02       41 阅读
  4. Android Activity切换动画多种实现方式

    2024-03-16 21:50:02       40 阅读
  5. Redis 简介与使用

    2024-03-16 21:50:02       42 阅读
  6. QT6.6 android下fftw-3.3.10库编译及调用so库方法

    2024-03-16 21:50:02       39 阅读
  7. KMM初探

    KMM初探

    2024-03-16 21:50:02      37 阅读
  8. Linux 服务器环境搭建

    2024-03-16 21:50:02       38 阅读
  9. Winform编程详解八:Label 标签控件

    2024-03-16 21:50:02       39 阅读