装配表达式:
全部采用基于xml装配Bean:
学习笔记(05):轻松搞定Spring全家桶(初识篇)-控制反转IOC:基于XML装配Bean:
1.表达式注入格式:
#{表达式内容}
2.关于运算符:
1)比较运算符:
运算符 | 文本比较运算符 |
---|---|
== | eq |
< | lt |
<= | le |
> | gt |
>= | ge |
2)逻辑运算符:
运算符 | 操作 |
---|---|
and | 只有运算符两边都是true,表达式才能使true |
or | 只要运算符的任意一一边是true, 表达式就会是true |
not或者! | 对运算结果求反 |
3)正则表达式:
匹配正则表达式:matches
正则表达式规则此处不赘述:
3.例子:
- /IocExpressionProject/src/com/ioc/AnonymousUserModel.java
package com.ioc;
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;
}
}
- /IocExpressionProject/src/com/ioc/UserModel.java
package com.ioc;
public class UserModel extends AnonymousUserModel {
private String osName;
public String getOsName() {
return osName;
}
public void setOsName(String osName) {
this.osName = osName;
}
}
- /IocExpressionProject/src/com/ioc/UserDao.java
package com.ioc;
public class UserDao {
private UserModel userModel;
private boolean isman;
private String ageType;
private boolean isPhone;
public void show() {
System.out.println("is man:"+this.isman);
System.out.println("age type:"+this.ageType);
System.out.println("is phone:"+this.isPhone);
System.out.println("id:"+this.userModel.getId());
System.out.println("name:"+this.userModel.getName());
System.out.println("age:"+this.userModel.getAge());
System.out.println("os name:"+this.userModel.getOsName());
}
public UserModel getUserModel() {
return userModel;
}
public void setUserModel(UserModel userModel) {
this.userModel = userModel;
}
public boolean isIsman() {
return isman;
}
public void setIsman(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;
}
}
- /IocExpressionProject/resource/applicationContext.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-4.3.xsd">
<bean id="anonymous" class="com.ioc.AnonymousUserModel"
p:id="anonymous" p:age="6" p:name="Tom" >
</bean>
<bean id="userModel" class="com.ioc.UserModel">
<!-- id使用随机数 -->
<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="13144356132"></property>
<!-- 获取本机操作系统 -->
<property name="osName" value="#{systemProperties['os.name']}"></property>
</bean>
<bean id="userDao" class="com.ioc.UserDao">
<property name="userModel" ref="userModel"></property>
<property name="isman" value="#{!(userModel.sex eq 'woman')}"></property>
<property name="ageType" value="#{userModel.age>=18?'adult':'juveniles'}"></property>
<!-- 判断是否为手机号:以一开头,第二位是3578中的一个,一共是11位数 -->
<property name="phone" value="#{userModel.phone matches '^1[3578]\d{9}$'}"></property>
</bean>
</beans>
- /IocExpressionProject/src/com/ioc/TestUserDao.java
package com.ioc;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
class TestUserDao {
@Test
void testShow() {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserDao userDao = context.getBean("userDao", UserDao.class);
userDao.show();
}
}