需要的springFrameWork的jar包
写一个通用的父类:
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;
}
}
用子类对象去继承该父类: 并且单独拥有一个属性osName
package com.ioc;
public class UserModel extends AnonymousUserModel {
private String osName;
public String getOsName() {
return osName;
}
public void setOsName(String osName) {
this.osName = osName;
}
}
DAO层调用对象
package com.ioc;
public class UserDao {
private UserModel userModel;
private boolean isMan;
private String ageType;
private boolean isPhone;
public void show() {
System.out.println("isMan:"+this.isMan);
System.out.println("ageType:"+this.ageType);
System.out.println("isPhone:"+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("osName:"+this.userModel.getOsName());
}
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;
}
}
如果是正常需要调用对象:
通过构造器给userModel对象进行设置 属性。
然后在通过dao调用测试对象:
userDao userDao = new UserDao();
userDao.show();
但是如果使用spring的xml配置方法,我们可以通过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="5" p:name="陌生人"/>
<bean id="userModel" class="com.ioc.UserModel">
<property name="id" value="#{T(java.lang.Math).random()}"/>
<property name="name" value="#{anonymous.name}"/>
<property name="sex" value="女"/>
<property name="age" value="#{13+2}"/>
<property name="phone" value="15821302132"/>
<property name="osName" value="#{systemProperties['os.name']}"/>
</bean>
<bean id="userDao" class="com.ioc.UserDao">
<property name="userModel" ref="userModel"/>
<property name="man" value="#{!(userModel.sex eq '女')}"/>
<property name="ageType" value="#{userModel.age>18?'成年人':'未成年人'}"/>
<property name="phone" value="#{userModel.phone matches '^1[3578]\d{9}$'}"/>
</bean>
</beans>
比如:
可以通过表达式,应用对象的属性值,并且还可以通过表达式进一步的进行判断,或者通过表达式进行设置初始值。