Spring的SpEL的属性注入

Spring的SpEL的属性注入

SpEL(Spring Expression Language),即Spring表达式语言,是比JSP的EL更强大的一种表达式语言。在运行时查询和操作数据,尤其是数组列表型数据,因此可以缩减代码量,优化代码结构。

1、注入一般属性

#{SpEL}

大括号当中可以执行一些计算,调用其他类的方法

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd">
	<!-- Spring的属性注入的方式 -->
	<!-- 构造方法的方式 -->
	<!-- set方法方式符注入 -->
	<!-- SpEL的属性注入 -->
	<bean id="car2" class="com.itzheng.spring.demo4.Car2">
		<property name="name" value="#{'拖拉机' }"></property>
		<property name="price" value="#{30000}"></property>
	</bean>
</beans>
package com.itzheng.spring.demo4;

/*
 * set方法的属性注入
 */
public class Car2 {
	private String name;
	private Double price;
	public void setName(String name) {
		this.name = name;
	}
	public void setPrice(Double price) {
		this.price = price;
	}
	@Override
	public String toString() {
		return "Car2 [name=" + name + ", price=" + price + "]";
	}
}
@Test
	public void demo5() {
		// 创建工程
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
		// 获得到对应XML当中的对象
		// 获得到对应XML当中的对象
		Car2 car2 = (Car2) applicationContext.getBean("car2");
		System.out.println(car2);
	}

在这里插入图片描述

2、注入对象

(1)直接注入
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd">
	<!-- Spring的属性注入的方式 -->
	<!-- 构造方法的方式 -->
	<!-- SpEL的属性注入 -->
	<bean id="car2" class="com.itzheng.spring.demo4.Car2">
		<property name="name" value="#{'拖拉机' }"></property>
		<property name="price" value="#{30000}"></property>
	</bean>
	<bean id="employee" class="com.itzheng.spring.demo4.Employee">
		<property name="name" value="#{'武九' }"></property>
		<property name="car2" value="#{car2}"></property>
	</bean>
</beans>

在这里插入图片描述

package com.itzheng.spring.demo4;
public class Employee {
	private String name;
	private Car2 car2;
	public void setName(String name) {
		this.name = name;
	}
	public void setCar2(Car2 car2) {
		this.car2 = car2;
	}
	@Override
	public String toString() {
		return "Employee [name=" + name + ", car2=" + car2 + "]";
	}
}
@Test
	public void demo5() {
		// 创建工程
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
		// 获得到对应XML当中的对象
		// 获得到对应XML当中的对象
		Car2 car2 = (Car2) applicationContext.getBean("car2");
		System.out.println(car2);
	}

在这里插入图片描述

(2)通过方法获得值,注入到另外一个对象的属性
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd">
	<!-- Spring的属性注入的方式 -->
	<!-- 构造方法的方式 -->
	<!-- SpEL的属性注入 -->
	<bean id="carInfo" class="com.itzheng.spring.demo4.CarInfo">
	
	</bean>
	
	<bean id="car2" class="com.itzheng.spring.demo4.Car2">

		<property name="name" value="#{carInfo.name}"></property>
		<property name="price" value="#{carInfo.calculatorPrice()}"></property>

	</bean>
	
</beans>

在这里插入图片描述

package com.itzheng.spring.demo4;
public class CarInfo {
	private String name;
	public String getName() {
		return "摩托车";
	}
	public Double calculatorPrice() {
		return Math.random() * 3000;
	}
}
package com.itzheng.spring.demo4;
public class CarInfo {
	private String name;
	public String getName() {
		return "摩托车";
	}
	public Double calculatorPrice() {
		return Math.random() * 3000;
	}
}

测试
在这里插入图片描述
测试
在这里插入图片描述

  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

程序员猫爪

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

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

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

打赏作者

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

抵扣说明:

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

余额充值