springFrameWork 第四章:IOC注入器_表达式装配

14 篇文章 0 订阅

需要的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>

比如:

可以通过表达式,应用对象的属性值,并且还可以通过表达式进一步的进行判断,或者通过表达式进行设置初始值。

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

逼哥很疯狂

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值