Introduction to the use of Spring (4) - SpEL

1. Introduction to SpEL

Spring Expression Language is called "Spring Expression Language", abbreviated as "SpEL", which can build complex expressions at runtime

Steps for usage:

1) Create a parser: The ExpressionParser interface represents the parser, and SpelExpressionParser provides a default implementation

2) Parse the expression: Use the ExpressionParser.parseExpression() method to parse the expression into an Expression object

3) Construction context: used to define variables, represented by the EvaluationContext interface, StandardEvaluationContext provides a default implementation

4) Evaluation: Use the Expression.getValue() method to obtain the expression value according to the context

@Test
 public  void testGames () {
     // games 解析器
    ExpressionParser parser = new GamesExpressionParser ();
    
    // spel语句
    Expression expression1 = parser.parseExpression("895");
    Assert.assertTrue(895 == expression1.getValue(int.class));
    
    // spel语句
    Expression expression2 = parser.parseExpression("'hello'");
    Assert.assertTrue(StringUtils.equals("hello", expression2.getValue(String.class)));
    
    // spel context 
    EvaluationContext context = new StandardEvaluationContext();
    context.setVariable("end", "zz");
    Expression expression3 = parser.parseExpression("('yy' + 'pp').concat(#end)");
    Assert.assertTrue(StringUtils.equals("yyppzz", expression3.getValue(context, String.class)));
}

 

2. Using SpEL in bean definitions

The ApplicationContext implementation supports SpEL by default. When injected at the time of bean definition, use "#{SpEL expression}" to indicate

1) xml method

<bean id="str1" class="java.lang.String">
    <constructor-arg index="0" value="uouu"/>  
</bean>

<bean id="propBean" class="java.lang.String">
    <constructor-arg index="0" value="#{str1}"/>
</bean>
@Test
 public  void testSpel2() {
    ApplicationContext context = new ClassPathXmlApplicationContext("spring-context.xml");
    String prop = context.getBean("propBean", String.class);
    Assert.assertTrue(StringUtils.equals("uouu", prop));
}

2) Annotation method

public class Hello {
    @Value("#{str1}")
    private String password;

    public String getPassword() {
        return password;
    }
}
<!-- 定义bean -->
<bean class="cn.matt.spel.Hello"></bean>

<!-- Open annotation --> 
< context:annotation-config />
@Test
 public  void testSpel3() {
    ApplicationContext context = new ClassPathXmlApplicationContext("spring-context.xml");
    Hello hello = context.getBean(Hello.class);
    Assert.assertTrue(StringUtils.equals("uouu", hello.getPassword()));
}

 

Third, the use of property files

Spring property file configuration method:

<!-- 全写方式 -->
<bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <list>
            <value>jdbc1.properties</value>
            <value>jdbc2.properties</value>
        </list>
    </property>
</bean>

Shorthand for the above configuration:

<!-- 简写方式 -->
<context:property-placeholder location="classpath:jdbc1.properties,classpath:jdbc2.properties"/>

Examples of use are as follows:

# jdbc1.properties file
username=root
password=root
# jdbc2.properties file
username=admin
password=admin
<bean id="propBean" class="java.lang.String">
    <constructor-arg index="0" value="${password}"/>  
</bean>
@Test
 public  void testSpel2() {
    ApplicationContext context = new ClassPathXmlApplicationContext("spring-context.xml");
    String prop = context.getBean("propBean", String.class);
    Assert.assertTrue(StringUtils.equals("admin", prop));
}

Note:

1) Spring uses "${property name}" to represent property values, which is different from SpEL's use of "#{SpEL expression}"

2) When the same property value exists in multiple property files, the latter overrides the former

3) Support annotation method, the usage method is similar to SpEL annotation, such as: @Value("${password}")

 

refer to:

Chapter 5 Spring Expression Language 5.1 Overview 5.2 SpEL Basics - Follow me spring3

Chapter 5 Spring Expression Language 5.3 SpEL syntax - follow me spring3

Chapter 5 Spring Expression Language 5.4 Using EL in Bean Definitions - Follow me spring3

Spring <context:property-placeholder> Instructions for use

Import multiple properties files using the <context:property-placeholder> tag

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324613367&siteId=291194637