@property源码

class property(object):
    """
    property(fget=None, fset=None, fdel=None, doc=None) -> property属性值
    
   fget是一个用于获取属性值的函数,fset是设置属性值的函数,fdel是删除属性值的方法,doc则是创建属性的文档字符串,典型的托管属性x实例如下:
    
    class C(object):
        def getx(self): return self._x
        def setx(self, value): self._x = value
        def delx(self): del self._x
        x = property(getx, setx, delx, "I'm the 'x' property.")
    
   装饰器使定义新属性或修改现有属性变得容易:
    
    class C(object):
        @property
        def x(self):
            "I am the 'x' property."
            return self._x
        @x.setter
        def x(self, value):
            self._x = value
        @x.deleter
        def x(self):
            del self._x
    """
    def deleter(self, *args, **kwargs): # real signature unknown
        """ Descriptor to change the deleter on a property. """
        pass

    def getter(self, *args, **kwargs): # real signature unknown
        """ Descriptor to change the getter on a property. """
        pass

    def setter(self, *args, **kwargs): # real signature unknown
        """ Descriptor to change the setter on a property. """
        pass

    def __delete__(self, *args, **kwargs): # real signature unknown
        """ Delete an attribute of instance. """
        pass

    def __getattribute__(self, *args, **kwargs): # real signature unknown
        """ Return getattr(self, name). """
        pass

    def __get__(self, *args, **kwargs): # real signature unknown
        """ Return an attribute of instance, which is of type owner. """
        pass

    def __init__(self, fget=None, fset=None, fdel=None, doc=None): # known special case of property.__init__
        pass

    @staticmethod # known case of __new__
    def __new__(*args, **kwargs): # real signature unknown
        """ Create and return a new object.  See help(type) for accurate signature. """
        pass

    def __set__(self, *args, **kwargs): # real signature unknown
        """ Set an attribute of instance to value. """
        pass

    fdel = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default

    fget = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default

    fset = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default

    __isabstractmethod__ = property(lambda self: object(), lambda self, v: None, lambda self: None)  # default


property对象使用(https://blog.youkuaiyun.com/StrongbyTime/article/details/86422195)

### 使用 `@PropertySource` 动态加载不同环境配置文件 为了实现动态加载不同的环境配置文件,在应用程序启动时可以根据当前激活的环境来决定加载哪个 `.properties` 文件。这通常通过组合使用 `@PropertySource` 和 Spring Boot 提供的多环境支持机制完成。 当应用运行于特定环境中时,可以通过设置活跃概要 (`active profile`) 来控制哪些配置会被实际加载。例如: ```java import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Profile; @Configuration @Profile("dev") @PropertySource("classpath:application-dev.properties") public class DevEnvironmentConfig { // 开发环境下的额外Bean定义或其他配置逻辑 } ``` 对于生产环境,则创建另一个类似的配置类并标注为 `prod`: ```java import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Profile; @Configuration @Profile("prod") @PropertySource("classpath:application-prod.properties") public class ProdEnvironmentConfig { // 生产环境下的额外Bean定义或其他配置逻辑 } ``` 这样做的好处是可以针对每种部署情况定制化参数而无需修改源码本身[^1]。 另外一种方式是在单个配置类中利用条件判断语句或者 SpEL 表达式来达到相同的效果。比如下面的例子展示了如何基于活动profile名称有条件地注册属性资源: ```java import org.springframework.beans.factory.config.YamlPropertiesFactoryBean; import org.springframework.core.env.PropertiesPropertySource; import org.springframework.core.io.support.EncodedResource; import org.springframework.core.io.ClassPathResource; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import java.util.Properties; @Configuration public class DynamicPropertySourceConfig { @Bean public Properties dynamicProps() { String activeProfile = System.getProperty("spring.profiles.active"); EncodedResource resource = new EncodedResource(new ClassPathResource( "application-" + (activeProfile != null ? activeProfile : "default") + ".properties")); YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean(); factory.setResources(resource.getResource()); return factory.getObject(); } // 将上述获取到的Properties对象转换成PropertySources并加入到Environment中... } ``` 需要注意的是,如果希望更灵活地管理外部化的配置项,还可以考虑采用 `Spring Cloud Config Server` 或者其他专门的服务发现工具和服务端解决方案[^4]。 #### 关键点总结: - 可以根据不同的环境创建带有相应 `@Profile` 注解的不同配置类,并在其内部声明各自的 `@PropertySource`。 - 利用程序入口处传递给 JVM 参数 `-Dspring.profiles.active=envName` 来切换所使用的配置集。 - 对于更加复杂的场景,可以在同一个配置类里编写逻辑去适应多种可能的情况。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值