关键代码:
<!-- SpEL赋值一个字面值(没多大意义) --> <bean id="user" class="com.shuoeasy.test.User" p:id="1001" p:name="#{'说易事'}"></bean> <!-- SpEL引用类里面的静态属性 --> <bean id="user2" class="com.shuoeasy.test.User" p:id="1001" p:name="#{T(com.shuoeasy.test.User).USER_NAME}"></bean> <!-- SpEL引用其它bean --> <bean id="order" class="com.shuoeasy.test.Order" > <property name="id" value="8888"></property> <!-- 引用bean --> <property name="user" value="#{user2}"></property> <!-- 引用bean的属性 --> <property name="message" value="备注:#{user2.name}"></property> </bean> <!-- SpEL使用运算符 --> <bean id="user3" class="com.shuoeasy.test.User" p:id="1003" p:name="#{user2.id>1000?'新账号':'旧账号'}"></bean>
详细:
User.java:
package com.shuoeasy.test; public class User { public static String USER_NAME = "老鹰开灰机"; private int id; private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "User [id=" + id + ", name=" + name + "]"; } }
Order.java:
package com.shuoeasy.test; public class Order { private int id; private User user; private String message; public int getId() { return id; } public void setId(int id) { this.id = id; } public User getUser() { return user; } public void setUser(User user) { this.user = user; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } @Override public String toString() { return "Order [id=" + id + ", user=" + user + ", message=" + message + "]"; } }
bean.xml:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- SpEL赋值一个字面值(没多大意义) --> <bean id="user" class="com.shuoeasy.test.User" p:id="1001" p:name="#{'说易事'}"></bean> <!-- SpEL引用类里面的静态属性 --> <bean id="user2" class="com.shuoeasy.test.User" p:id="1001" p:name="#{T(com.shuoeasy.test.User).USER_NAME}"></bean> <!-- SpEL引用其它bean --> <bean id="order" class="com.shuoeasy.test.Order" > <property name="id" value="8888"></property> <!-- 引用bean --> <property name="user" value="#{user2}"></property> <!-- 引用bean的属性 --> <property name="message" value="备注:#{user2.name}"></property> </bean> <!-- SpEL使用运算符 --> <bean id="user3" class="com.shuoeasy.test.User" p:id="1003" p:name="#{user2.id>1000?'新账号':'旧账号'}"></bean> </beans>
Main.java:
package com.shuoeasy.test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml"); // SpEL赋值一个字面值 User user = (User) ctx.getBean("user"); System.out.println("user->" + user); // SpEL引用类里面的静态属性 User user2 = (User) ctx.getBean("user2"); System.out.println("user2->" + user2); // SpEL引用其它bean的属性 Order order = (Order) ctx.getBean("order"); System.out.println("order->" + order); // SpEL使用运算符 User user3 = (User) ctx.getBean("user3"); System.out.println("user2->" + user3); } }
输出:
user->User [id=1001, name=说易事]
user2->User [id=1001, name=老鹰开灰机]
order->Order [id=8888, user=User [id=1001, name=老鹰开灰机], message=备注:老鹰开灰机]
user3->User [id=1003, name=新账号]