临海企业网站建设公司,郑州市中标公示网,分销系统平台有哪些,办公室装修设计理念简短范文之前学习basic的时候有个疑问就是不知道如何实现bean中引用其他的bean的属性#xff0c;当时是用ref来实现对其他bean的引用#xff0c;但是ref必需引用的是一个常量。所以这种方式来实现对其他bean中的属性的引用是不合理的。 当我看到Spring Expression Language时发现原来…之前学习basic的时候有个疑问就是不知道如何实现bean中引用其他的bean的属性当时是用ref来实现对其他bean的引用但是ref必需引用的是一个常量。所以这种方式来实现对其他bean中的属性的引用是不合理的。 当我看到Spring Expression Language时发现原来我想实现的ref的效果就是使用Spring EL这种表达式就可以完成了。 而Spring EL有两种配置形式一种是Spring EL 写在xml中和zai xml定义bean一样通过在定义bean的xml标签内使用#{otherBean.property} 或#{otherBean}的形式来完成。另一种是基于注解的方式。一下稍作说明 第一种和第二种都是通过xml来加载spring context的但是xml里的内容不一样 第一种直接在xml中定义spring 的上下文并在bean中的属性字段直接使用SpringEL来引用其他bean或其他bean的属性 第二种: 在xml中定义Component的scan-package的包名这样spring会在这个包中扫描并配置对应的bean然后我们在对应的类中加上注解就可以了. 第一种 ?xml version1.0 encodingUTF-8?beans xmlnshttp://www.springframework.org/schema/beans xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd bean iditemBean classcom.ric.demo.Item property namename valueitemA/ property nameqty value10/ /bean bean idcustomerBean classcom.ric.demo.Customer property nameitem value#{itemBean} /// 直接在这里使用Spring EL设置值 property nameitemName value#{itemBean.name}/// 直接在这里使用Spring EL设置值 /bean/beans 第二种 定义xml: ?xml version1.0 encodingUTF-8?beans xmlnshttp://www.springframework.org/schema/beans xmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xmlns:contexthttp://www.springframework.org/schema/context xsi:schemaLocationhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd context:component-scan base-packagecom.ric.demo //beans配置对应的bean Component(customerBean)public class Customer { private Item item; private String itemName; Override public String toString() { return Customer{ item item , itemName itemName \ }; } public Item getItem() { return item; } // 使用Spring EL 来取到对应的value Value(#{itemBean}) public void setItem(Item item) { this.item item; } public String getItemName() { return itemName; }// 使用Spring EL 来取到对应的value Value(#{itemBean.name}) public void setItemName(String itemName) { this.itemName itemName; }} 需要注意的是如果这里的 Spring EL里面引用的是其他的对象有一下这种效果 ApplicationContext context new ClassPathXmlApplicationContext(BeansAutoScan.xml);Item item (Item)context.getBean(itemBean);System.out.println(item);Customer customer (Customer)context.getBean(customerBean);System.out.println(customer);item.setName(geek);System.out.println(item);System.out.println(customer); Item{nameitem, qty10}Customer{itemItem{nameitem, qty10}, itemNameitem}Item{namegeek, qty10}Customer{itemItem{namegeek, qty10}, itemNameitem} Process finished with exit code 0 也就是说如果引用的是item bean 我们的customer的itemBean属性的确是对item这个bean的一个引用。 但是Customer的itemName这个属性的值没有变因为bean扫描的时候也是为上下文中的bean做一个初始化的工作初始化完后item虽然对name做了一个更新的操作但是customer的itemName的指针不会因为item的name做了变化而同步而是依旧指向item原先name值的那个指针 转载于:https://www.cnblogs.com/codetime/p/6293489.html