Spring学习笔记_06——@PropertySource

@PropertySource

1. 讲解

@PropertySource注解是Spring中提供的一个通过指定配置文件位置来加载配置文件的注解,并且可以将配置文件中的内容存放到Spring的环境变量中。

除了可以通过Spring的环境变量读取配置项之外,还可以通过@Value注解获取配置项的值。

Spring中还提供了一个@PropertySources注解,在@PropertySources注解中,可以引入多个@PropertySource注解。

@PropertySource注解只能标注到类上,能够通过指定配置文件的位置来加载配置文件

@PropertySource注解除了可以加载properties配置文件外,也可以加载xml配置文件和yml配置文件。如果加载yml配置文件时,可以自定义PropertySourceFactory实现yml配置文件的解析操作

2. 源码

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

	
        // 表示加载的资源的名称,如果为空,则会根据加载的配置文件自动生成一个名称。
	String name() default "";
	
        // 表示加载的资源的路径,这个路径可以是类路径,也可以是文件路径。
	String[] value();

        // 表示当配置文件未找到时,是否忽略文件未找到的错误。默认值为false,也就是说当未找到配置文件时,Spring启动就会报错。
	boolean ignoreResourceNotFound() default false;

        // 表示解析配置文件使用的字符集编码
	String encoding() default "";

        // 表示读取对应配置文件的工厂类,默认的工厂类是PropertySourceFactory。
	Class<? extends PropertySourceFactory> factory() default PropertySourceFactory.class;

}
/**
 * @author Phillip Webb
 * @since 4.0
 */
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface PropertySources {
	PropertySource[] value();
}

3. 场景

在基于Spring的注解开发项目的过程中,由于不再使用Spring的XML文件进行配置,如果将配置项直接写到类中,就会造成配置项与类的紧耦合,后续对于配置项的修改操作非常不方便,不利于项目的维护和扩展。此时,可以将这些配置项写到properties文件或者yml文件中,通过@PropertySource注解加载配置文件。

另外,如果项目本身就存在大量的properties配置文件或者yml配置文件,也可以统一由Spring的@PropertySource注解进行加载。

4. Demo

  • 创建自定义的ProperSourceFactory
import org.springframework.core.env.Environment;  
import org.springframework.core.env.MapPropertySource;  
import org.springframework.core.env.PropertySource;  
import org.springframework.core.env.PropertySourceFactory;  
  
import java.io.IOException;  
import java.io.InputStream;  
import java.util.HashMap;  
import java.util.Map;  
import java.util.Properties;  
  
public class CustomPropertySourceFactory implements PropertySourceFactory {  
  
    @Override  
    public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException {  
        // 这里你可以自定义加载属性的逻辑,例如解密等  
        Properties properties = new Properties();  
        try (InputStream inputStream = resource.getInputStream()) {  
            properties.load(inputStream);  
        }  
  
        // 将属性放入一个 Map 中,并创建 MapPropertySource  
        Map<String, Object> source = new HashMap<>();  
        for (String name : properties.stringPropertyNames()) {  
            source.put(name, properties.getProperty(name));  
        }  
  
        return new MapPropertySource(name, source);  
    }  
}
  • 创建一个演示用的配置文件
app.name=My Spring Demo  
app.version=1.0.0
  • 创建一个配置类
import org.springframework.context.annotation.Configuration;  
import org.springframework.context.annotation.PropertySource;  
import org.springframework.context.annotation.PropertySources;  
import org.springframework.core.env.EncodedResource;  
import org.springframework.core.io.support.ResourcePropertySource;  
  
@Configuration  
@PropertySources({  
    @PropertySource(value = "classpath:custom.properties", factory = CustomPropertySourceFactory.class)  
})  
public class AppConfig {  
    // 配置类不需要其他内容,只需包含 @PropertySources 注解即可  
}
  • 创建一个测试类
import org.springframework.beans.factory.annotation.Value;  
import org.springframework.context.ApplicationContext;  
import org.springframework.context.annotation.AnnotationConfigApplicationContext;  
  
public class PropertySourceDemo {  
  
    @Value("${app.name}")  
    private String appName;  
  
    @Value("${app.version}")  
    private String appVersion;  
  
    public static void main(String[] args) {  
        ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);  
        PropertySourceDemo demo = context.getBean(PropertySourceDemo.class);  
  
        System.out.println("App Name: " + demo.appName);  
        System.out.println("App Version: " + demo.appVersion);  
    }  
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值