6.Spring SpEL表达式

SpEL:spring表达式

  格式:<property name="" value="#{表达式}">

1.#{123}#{'jack'}:数字,字符串
2.#{beanId}:另一个bean引用
3.#{beanId.propName}:操作数据
4.#{beanId.toString()}:执行方法
5.#{T().字段|方法}:静态方法或字段


代码展示:
Customer.java
 1 package com.gyf.model;
 2 
 3 public class Customer {
 4     private String name;
 5     private String sex = "男";
 6     private double pi;
 7     private Address address;
 8 
 9     public String getName() {
10         return name;
11     }
12 
13     public void setName(String name) {
14         this.name = name;
15     }
16 
17     public String getSex() {
18         return sex;
19     }
20 
21     public void setSex(String sex) {
22         this.sex = sex;
23     }
24 
25     public double getPi() {
26         return pi;
27     }
28 
29     public void setPi(double pi) {
30         this.pi = pi;
31     }
32 
33     public Address getAddress() {
34         return address;
35     }
36 
37     public void setAddress(Address address) {
38         this.address = address;
39     }
40 
41     @Override
42     public String toString() {
43         return "Customer{" +
44                 "name='" + name + '\'' +
45                 ", sex='" + sex + '\'' +
46                 ", pi=" + pi +
47                 ", address=" + address +
48                 '}';
49     }
50 }
Address.java
 1 package com.gyf.model;
 2 
 3 public class Address {
 4     private String name;
 5 
 6     public String getName() {
 7         return name;
 8     }
 9 
10     public void setName(String name) {
11         this.name = name;
12     }
13 
14     @Override
15     public String toString() {
16         return "Address{" +
17                 "name='" + name + '\'' +
18                 '}';
19     }
20 }

beans.xml
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
 5     <!--spEL:spring表达式
 6         <property name="" value="#{表达式}">
 7         #{123},#{'jack'}:数字,字符串
 8         #{beanId}:另一个bean引用
 9         #{beanId.propName}:操作数据
10         #{beanId.toString()}:执行方法
11         #{T(类).字段|方法}:静态方法或字段
12     -->
13     <bean name="address" class="com.gyf.model.Address">
14         <property name="name" value="上海"></property>
15     </bean>
16     <bean id="customer" class="com.gyf.model.Customer">
17         <!--#{beanId.xxx()}:执行方法-->
18         <!--<property name="name" value="#{'alex'.toUpperCase()}"></property>-->
19         <!--{T(类).字段|方法}:静态方法或字段-->
20         <property name="pi" value="#{T(java.lang.Math).PI}"></property>
21         <!--
22         一个对象引用另一个对象的两种写法:
23         1.ref:引用 <property name="address" ref="address"></property>
24         2.SpEL:<property name="address" value="#{address}"></property>
25         -->
26         <property name="address" value="#{address}"></property>
27         <!--{beanId.propName}:操作数据-->
28         <property name="name" value="#{address.name}"></property>
29     </bean>
30 </beans>
main.java
 1 package com.gyf.test;
 2 
 3 import com.gyf.model.Customer;
 4 import org.junit.Test;
 5 import org.springframework.context.ApplicationContext;
 6 import org.springframework.context.support.ClassPathXmlApplicationContext;
 7 
 8 public class Run5 {
 9     @Test
10     public void test() {
11         ApplicationContext context = new ClassPathXmlApplicationContext("beans5.xml");
12         Customer customer = (Customer) context.getBean("customer");
13         System.out.println(customer);
14     }
15 }

 

 

 

posted @ 2020-08-18 22:33  违和感  阅读(115)  评论(0)    收藏  举报