博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring表达式语言SpEL
阅读量:6214 次
发布时间:2019-06-21

本文共 3431 字,大约阅读时间需要 11 分钟。

hot3.png

1.SpEL为bean动态赋值

2.Address.java

package com.huangliusong.spring.autowire.spel;public class Address {	private String city;	public String getCity() {		return city;	}	@Override	public String toString() {		return "Address [city=" + city + ", street=" + street + "]";	}	public Address() {		super();		// TODO Auto-generated constructor stub	}	public void setCity(String city) {		this.city = city;	}	public Address(String city, String street) {		super();		this.city = city;		this.street = street;	}	public String getStreet() {		return street;	}	public void setStreet(String street) {		this.street = street;	}	private String street;}

3.Car.java

package com.huangliusong.spring.autowire.spel;public class Car {	private String brand;	private double price;	//轮胎的周长	private double tyrePerimeter;	public double getTyrePerimeter() {		return tyrePerimeter;	}	public void setTyrePerimeter(double tyrePerimeter) {		this.tyrePerimeter = tyrePerimeter;	}	public String getBrand() {		return brand;	}	public Car(String brand, double price) {		super();		this.brand = brand;		this.price = price;	}	@Override	public String toString() {		return "Car [brand=" + brand + ", price=" + price + ", tyrePerimeter="				+ tyrePerimeter + "]";	}	public void setBrand(String brand) {		this.brand = brand;	}	public Car() {		System.err.println("car 构造方法");	}	public double getPrice() {		return price;	}	public void setPrice(double price) {		this.price = price;	}	}

4.Person.java

package com.huangliusong.spring.autowire.spel;public class Person {	private String name;	private String info;	// 引用address的city属性	private String city;	public String getInfo() {		return info;	}	public void setInfo(String info) {		this.info = info;	}	// 根据car的price确定info car的price>=300000; 金领	// 否则为白领	public String getName() {		return name;	}	public String getCity() {		return city;	}	public void setCity(String city) {		this.city = city;	}	public void setName(String name) {		this.name = name;	}	public Person() {		super();		// TODO Auto-generated constructor stub	}	@Override	public String toString() {		return "Person [name=" + name + ", info=" + info + ", city=" + city				+ ", car=" + car + "]";	}	public Person(String name, String info, String city, Car car) {		super();		this.name = name;		this.info = info;		this.city = city;		this.car = car;	}	public Car getCar() {		return car;	}	public void setCar(Car car) {		this.car = car;	}	private Car car;}

5.TestAutowire.java

package com.huangliusong.spring.autowire.spel;import org.junit.Test;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class TestAutowire {	@Test	public void test1() {		ApplicationContext ctx = new ClassPathXmlApplicationContext(				"bean_spel.xml");		Address address = (Address) ctx.getBean("address");		System.out.println(address);	}	@Test	public void test2() {		ApplicationContext ctx = new ClassPathXmlApplicationContext(				"bean_spel.xml");		Car car = (Car) ctx.getBean("car");		System.out.println(car);	}	@Test	public void test3() {		ApplicationContext ctx = new ClassPathXmlApplicationContext(				"bean_spel.xml");		Person car = (Person) ctx.getBean("person");		System.out.println(car);	}}

6.bean_spel.xml

 

转载于:https://my.oschina.net/liusonghuang/blog/817211

你可能感兴趣的文章
Cocos2d-html5循序渐进(1)-开发环境配置
查看>>
CP大于指定大小的文件到指定位置
查看>>
.net mvc Html.DropDownListFor 设置默认值无效
查看>>
部署RODC和RODNS加强分支办公处的信息和网络安全
查看>>
《Thinking in Java》笔记
查看>>
django开发环境部署(四)——uwsgi,nginx
查看>>
斯坦福NLP笔记3 —— Word tokenization
查看>>
leetcode----Longest Substring Without Repeating Characters
查看>>
UIBezierPath
查看>>
Kotlin教程学习-字符串拼接,数组,List
查看>>
redis cluster水平扩容
查看>>
IT高薪者所具备的人格魅力
查看>>
我的友情链接
查看>>
MyEclipse+Tomcat+MAVEN+SVN项目完整环境搭建
查看>>
Git使用详细教程
查看>>
给AIX5系统做克隆
查看>>
linux监控平台搭建-cpu
查看>>
Maven学习总结(四)——Maven核心概念
查看>>
Spring学习详解(1)——Spring入门详解
查看>>
MyEclipse+Tomcat+MAVEN+SVN项目完整环境搭建
查看>>