admin 管理员组

文章数量: 888297

跟杨春娟学Spring笔记:表达式装配

跟杨春娟学Spring笔记:表达式装配

完成:第一遍

1.使用表达式注入有哪几种?

如果是表达式注入,必须在value里写#{表达式}

运算符:等于
符号: ==
文本类型运算符: eq

运算符:小于
符号: <
文本类型运算符:lt

运算符:小于等于
符号: <=
文本类型运算符:le

运算符:大于
符号: >
文本类型运算符: gt

运算符:大于等于
符号: >=
文本类型运算符: ge

运算符:and
操作:逻辑AND运算操作,只有运算符两边都是true,表达式才能使true

运算符:or
操作:逻辑OR运算操作,只要运算符的任意一边是true,表达式就会是true

运算符:not或!
操作:逻辑NOT运算操作,对运算结果求反

运算符:正则表达式
操作:matches

AnonymousUserModel

package com.adbycool.expression;public class AnonymousUserModel {private String id;private String name;private String sex;private int age;private String phone;public String getId() {return id;}public void setId(String id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getSex() {return sex;}public void setSex(String sex) {this.sex = sex;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}public String getPhone() {return phone;}public void setPhone(String phone) {this.phone = phone;}@Overridepublic String toString() {return "AnonymousUserModel [id=" + id + ", name=" + name + ", sex=" + sex + ", age=" + age + ", phone=" + phone+ "]";}}

UserModel

package com.adbycool.expression;public class UserModel extends AnonymousUserModel {private String osName;public String getOsName() {return osName;}public void setOsName(String osName) {this.osName = osName;}@Overridepublic String toString() {return "UserModel [osName=" + osName + "]";}}

UserDao

package com.adbycool.expression;public class UserDao {private UserModel userModel;private boolean isMan;private String ageType;private boolean isPhone;public void show() {System.out.println("userDao:"+this);}public UserModel getUserModel() {return userModel;}public void setUserModel(UserModel userModel) {this.userModel = userModel;}public boolean isMan() {return isMan;}public void setMan(boolean isMan) {this.isMan = isMan;}public String getAgeType() {return ageType;}public void setAgeType(String ageType) {this.ageType = ageType;}public boolean isPhone() {return isPhone;}public void setPhone(boolean isPhone) {this.isPhone = isPhone;}@Overridepublic String toString() {return "UserDao [userModel=" + userModel + ", isMan=" + isMan + ", ageType=" + ageType + ", isPhone=" + isPhone+ "]";}}

TestExcepression

package com.adbycool.expression;import static org.junit.jupiter.api.Assertions.*;import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;class TestExcepression {@Testvoid test() {ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");UserDao userDao = context.getBean("userDao",UserDao.class);userDao.show();}}

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?><beans xmlns=""xmlns:xsi=""xmlns:p=""xmlns:c=""xsi:schemaLocation="://www.springframework/schema/beans/spring-beans-4.3.xsd"><!--p标签注入  --><bean id="anonymous" class="com.adbycool.expression.AnonymousUserModel"p:id="anonymous" p:age="5" p:name="X man" >   </bean><bean id="userModel" class="com.adbycool.expression.UserModel"><!--Math取随机数  --><property name="id" value="#{T(java.lang.Math).random()}"></property><property name="name" value="#{anonymous.name}"></property><property name="sex" value="woman"></property><property name="age" value="#{13+5}"></property><property name="phone" value="15999993333"></property><property name="osName" value="#{systemProperties['os.name']}"></property></bean><bean id="userDao" class="com.adbycool.expression.UserDao"><property name="userModel" ref="userModel"></property><property name="ageType" value="#{(userModel.age ge 18)?'Adult':'NotAdult'}"></property><property name="man"   value="#{!(userModel.sex eq 'woman')}"></property><property name="phone" value="#{userModel.phone matches '^1[3578]\d{9}$'}"></property></bean></beans>

本文标签: 跟杨春娟学Spring笔记表达式装配