2.SpringEL bean引用实例

本文详细介绍了如何在SpringEL中使用注解和XML形式对bean进行属性引用,包括嵌套属性和方法调用的示例,展示了如何在`Customer`和`Address`类中实现bean的注入和获取。
摘要由CSDN通过智能技术生成

SpringEL bean引用实例

介绍

在Spring EL,可以使用点(.)符号嵌套属性参考一个bean。例如,“bean.property_name”

public class Customer {
	
	@Value("#{addressBean.country}")
	private String country;
}

在上面的代码片段,它从"addressBean""bean注入了"country"属性到现在的"customer"类的"country"属性的值

Spring EL以注解的形式

使用 SpEL 引用一个bean,bean属性也它的方法

package com.yiibai.core;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component("customerBean")
public class Customer {

	@Value("#{addressBean}")
	private Address address;

	@Value("#{addressBean.country}")
	private String country;
	
	@Value("#{addressBean.getFullAddress('yiibai')}")
	private String fullAddress;

	//getter and setter methods
	
	@Override
	public String toString() {
		return "Customer [address=" + address + "\n, country=" + country
				+ "\n, fullAddress=" + fullAddress + "]";
	}

}
package com.yiibai.core;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component("addressBean")
public class Address {

	@Value("GaoDeng, QiongShang")
	private String street;

	@Value("571100")
	private int postcode;

	@Value("CN")
	private String country;

	public String getFullAddress(String prefix) {

		return prefix + " : " + street + " " + postcode + " " + country;
	}

	//getter and setter methods

	public void setCountry(String country) {
		this.country = country;
	}

	@Override
	public String toString() {
		return "Address [street=" + street + ", postcode=" + postcode
				+ ", country=" + country + "]";
	}

}

执行结果

Customer obj = (Customer) context.getBean("customerBean");
System.out.println(obj);

输出结果

Customer [address=Address [street=GaoDeng, QiongShang, postcode=571100, country=CN]
, country=CN
, fullAddress=yiibai : GaoDeng, QiongShang 571100 CN]

Spring EL以XML的形式

<beans xmlns="http://www.springframework.org/schema/beans"
	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-3.0.xsd">

	<bean id="customerBean" class="com.yiibai.core.Customer">
		<property name="address" value="#{addressBean}" />
		<property name="country" value="#{addressBean.country}" />
		<property name="fullAddress" value="#{addressBean.getFullAddress('yiibai')}" />
	</bean>

	<bean id="addressBean" class="com.yiibai.core.Address">
		<property name="street" value="GaoDeng, QiongShang" />
		<property name="postcode" value="571100" />
		<property name="country" value="CN" />
	</bean>

</beans>
SpringEL(Expression Language)是一种表达式语言,它提供了在运行时访问对象的属性和方法的能力。EL可以用于访问JavaBean的属性、访问数组、集合和映射、执行算术和逻辑运算、调用方法等。 在Spring中,EL通常用于配置文件中的占位符和SpELSpring表达式语言注解中的表达式。例如,在Spring的XML配置文件中,可以使用EL表达式来引用其他bean的属性值,如下所示: ```xml <bean id="myBean" class="com.example.MyBean"> <property name="name" value="#{otherBean.name}" /> </bean> ``` 在这个例子中,EL表达式`#{otherBean.name}`用于引用另一个bean的属性值,这个属性值将被注入到`MyBean`实例中。 除了XML配置文件中的EL表达式外,Spring还提供了SpEL注解,它可以用于Java类中的方法、字段和构造函数参数上。例如,可以使用SpEL注解来计算方法返回值: ```java @Service public class MyService { private List<String> myList = new ArrayList<>(); @Value("#{myList.size()}") private int listSize; public void addToList(String item) { myList.add(item); } @Value("#{myList}") public List<String> getMyList() { return myList; } @Value("#{myList[0]}") public String getFirstItem() { return myList.get(0); } @Value("#{myList.size()}") public int getListSize() { return listSize; } } ``` 在这个例子中,SpEL注解`@Value`用于注入`listSize`字段的值,并计算`getMyList`方法、`getFirstItem`方法和`getListSize`方法的返回值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值