zoukankan      html  css  js  c++  java
  • 高级装配—运行时注入


    当我们经常用如下的硬解码方式来配置文件:

    <bean id="SgtPeppers" class="com.CDDemo.SgtPeppers" p:title="sgt" p:song="Twinkle, twinkle, little start">
            <property name="title" value="sgt"/>
            <property name="song" value="Twinkle, twinkle, little start"/>
        </bean>

    但有时我们需要避免硬解码,需要想要这些值在运行时确定,Spring提供了两种在运行时求值的方式:

    • 属性占位符
    • Spring表达式语言(SpEL)

    1.注入外部的值

    ​ 在Spring中,处理外部值得最简单方式就是申明属性源并通过Spring的Enviroment来检索属性.例如:

    @Configuration
    @PropertySource("classpath:app.properties")
    public class ExpressionTest {
        @Autowired
        Environment environment;
    
        @Test
        public BlankDisc disc(){
            return new BlankDisc(
    
                    environment.getProperty("disc.title"),
                    environment.getProperty("disc.artist")
            );
        }
    }

    app.properties

    disc.title="titeTest"
    disc.artist="Twinkle, twinkle, little start"

    2.深入学习Spring的Environment

    Environment的getProperty()方法有四个重载的变种形式:

    • String getProperty(String key)
    • String getProperty(String key,String defaultValue)
    • T getProperty(String key, Class type)
    • T getProperty(String key, Class type,T defaultValue)

    前两种形式的getProperty()方法都是返回String类型的值。后两种重载方法不是直接返回字符串,而是将获取到的值转化为指定类型的值,比如获取连接池中维持连接的总数量:

    String disc = env.getProperty("disc.title","dukh");

    除了属性相关的功能外,Environment还提供了一些方法来检查哪些profile处于激活状态:

    • String [] getActiveProfiles():返回激活profile名称的数组;
    • String [] getDefaultProfile():返回默认profile名称的数组;
    • boolean acceptsPrifiles(String ... profiles):如果environment支持给定的profile返回true。
    • 3.解析属性占位符

      ​ Spring一直支持将属性定义到外部的属性文件中,并使用占位符值将其插入到Spring bean中.在Spring装配中,占位符的形式为使用"${...}"包装的属性名称.

      比如我们常见的使用数据库连接池的方式:

    • <!-- 读取db.properties文件. 读取到数据库信息 -->
          <context:property-placeholder location="classpath:db.properties"/>
          <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
              <property name="driverClassName" value="${jdbc.driver}"></property>
              <property name="url" value="${jdbc.url}"></property>
              <property name="username" value="${jdbc.username}"></property>
              <property name="password" value="${jdbc.password}"></property>
          </bean>

      这里需要注意的是使用Spring context命名空间的元素将为你生成ProoertySourcesPlacehodlerConfigurer bean.

    5.SpEL运算符

    SpEL提供了几种运算符,这些运算符可以用在SpEL表达式中的值上。

    运算符类型运算符
    算术运算 +、-、*、/、%、^
    关系运算 <、>、==、<=、>=、lt、gt、eq、le、ge
    逻辑运算 and、or、not、|
    条件运算 ?:(ternary)、?:(Elvis)
    正则表达式 matches

    数值运算

    <!-- +运算符:两个数字相加 -->
    <property name="adjustedAmount" value="#{counter.total + 42}"/>
    <!-- +运算符:用于连接字符串 -->
    <property name="fullName" value="#{performer.firstName + ' ' + performer.lastName}"/>
    <!-- -运算符:两个数字相减 -->
    <property name="adjustedAmount" value="#{counter.total - 20}"/>
    <!-- *运算符:乘法运算 -->
    <property name="circumference" value="#{2 * T(java.lang.Math).PI * circle.radius}"/>
    <!-- /运算符:除法运算 -->
    <property name="average" value="#{counter.total / counter.count}"/>
    <!-- %运算符:求余运算 -->
    <property name="remainder" value="#{counter.total % counter.count}"/>
    <!-- ^运算符:乘方运算 -->
    <property name="area" value="#{T(java.lang.Math).PI * circle.radius ^ 2}"/>

    比较值

    比较两个值是否相等,可以使用”==”运算符:

    <!-- 假设equal属性为布尔属性 -->
    <property name="equal" value="#{counter.total == 100}"/>

     

    逻辑表达式

    运算符操作
    and 逻辑AND运算操作,只有运算符两边都是true,表达式才能是true
    or 逻辑OR运算操作,只要运算符的任意一边是true,表达式就会是true
    not或! 逻辑NOT运算操作,对运算结果求反
    <!-- and 运算符 -->
    <property name="largeCircle" value="#{shape.kind == 'circle' and shape.perimeter gt 10000}"/>
    <!-- ! 运算符 -->
    <property name="outOfStock" value="#{!product.availiable}"/>
    <!-- not 运算符 -->
    <property name="outOfStock" value="#{not product.availiable}"/>

    条件表达式

    <!-- ?:三元运算符 -->
    <property name="song" value="#{kenny.song != null ? kenny.song : 'Greensleeves'}"/>

    正则表达式

    <!-- 判断一个字符串是否是有效的邮件地址 -->
    <property name="validEmail" value="#{admin.email matches '[a-zA-Z0-9.-%+-]+@[a-zA-Z0-9.-]+\.com'}"/>

     

  • 相关阅读:
    PyTorch学习笔记之Variable_and_function_cat
    PyTorch学习笔记之DataLoaders
    PyTorch学习笔记之nn的简单实例
    PyTorch学习笔记之Tensors 2
    PyTorch学习笔记之Tensors
    webdriver定位元素
    Python面向对象、继承和多态
    python中json文件处理涉及的四个函数json.dumps()和json.loads()、json.dump()和json.load()的区分
    Python定义类时为什么要继承object类
    Python数据类型
  • 原文地址:https://www.cnblogs.com/socketqiang/p/11338011.html
Copyright © 2011-2022 走看看