前面介绍的都是关于普通的单个Bean的注入,现在讲解关于容器类的输入,包括实现Collection接口的容器,和
Properties还有Map的这类容器的注入。
1.Wiring collections
package com.bird.springidol;
import java.util.Collection;
public class OneManBand {
private Collection<Instrument> instruments;
public OneManBand() {
}
public void perform() {
for(Instrument instrument: instruments){
instrument.play();
}
}
public Collection<Instrument> getInstruments() {
return instruments;
}
public void setInstruments(Collection<Instrument> instruments) {
this.instruments = instruments;
}
}
配置代码
<bean id="hank" class="com.bird.springidol.OneManBand">
<property name="instruments">
<list>
<ref bean="saxophone"/>
<ref bean="guitar"/>
</list>
</property>
</bean>
2.Wiring Map Collections
package com.bird.springidol;
import java.util.Map;
public class CopyOfOneManBand2 {
private Map<String, Instrument> instruments;
public CopyOfOneManBand2() {
}
public void perform() {
for (String key : instruments.keySet()) {
System.out.println(key + " : ");
Instrument instrument = instruments.get(key);
instrument.play();
}
}
public Map<String, Instrument> getInstruments() {
return instruments;
}
public void setInstruments(Map<String, Instrument> instruments) {
this.instruments = instruments;
}
}
<bean id="hank1" class="com.bird.springidol.CopyOfOneManBand2">
<property name="instruments">
<map>
<entry key="guitar" value-ref="guitar"></entry>
<entry key="saxophone" value-ref="saxophone"></entry>
</map>
</property>
</bean>
3.Wiring nothing
在Spring里面,可以给一个属性显示的注入空值
<property name="someNonNullProperty"><null/></property>
4.Wiring with expressions
Spring提供了一个强大的动态表示语言,Spring Expression Language。
<bean id="car1" class="com.bird.springidol.Instrumentalist">
<property name="song" value="#{kenny.song}"></property>
<property name="instrument" value="#{kenny.instrument}"></property>
</bean>
还可以使用各种静态方法和获得方法的返回值等等
<property name="song" value="#{songSelector.selectSong()}"/>获得一个类的方法的返回值
使用静态方法
<property name="multiplier" value="#{T(java.lang.Math).PI}"/>
也可以使用逻辑运算符
<property name="adjust" value="#{T(java.lang.Math).PI * 2}"/>
分享到:
相关推荐
SpringCloud Function SpEL注入漏洞分析(CVE-2022-22963).doc
SSH笔记-Spring表达式语言:SpEL,关于SpEl的字面量、引用 Bean、属性和方法、支持的运算符号
SpringBoot 自定义注解,属性支持SPEL表达式。介绍了SPEL表达式的解析
支持每个数据库独立初始化表结构schema和数据库database。 支持无数据源启动,支持懒加载数据源(需要的时候再创建连接)。 支持 自定义注解 ,需继承DS(3.2.0+)。 提供并简化对Druid,HikariCp,BeeCp,Dbcp2的快速...
Spring框架中的spring-messaging模块提供了一种基于WebSocket的STOMP协议实现,STOMP消息代理在处理客户端消息时存在SpEL表达式注入漏洞,因此攻击者可以通过构造恶意的消息来实现远程代码执行。 Windows平台...
NULL 博文链接:https://364232252.iteye.com/blog/2369770
spring spEL 表达式详解 运行环境:eclipse 构建工具:maven 不提供maven构建,maven用来解决jar包的依赖
Spring之旅 依赖注入 AOP bean的初始化过程 spring容器 2. 装配Bean “initialization on demand holder”创建单例模式的理解,参考 Spring中单例的概念限于Spring上下文中,遵守约定 内部bean适用于setter注入和构造...
Spring 3.0是Spring框架的一次重大更新,而根据SpringSource副总裁Shaun Connolly的描述,Spring框架是当下最流行的Java开发框架。 全新的Spring 3.0提供了全面的RESTful Web服务支持,以及一个新的表达式语言。其...
2D--Spel-Absamoze 2D--Spel-Absamoze
spring-framework.zip,spring表达式语言(spel)spring框架
Getting started with Spring Framework is a hands-on guide to begin developing applications using Spring Framework. This book is meant for Java developers with little or no knowledge of Spring ...
Spring_SpEl表达式使用用例 只是一个简单的demo,有需要的可以看看
支持每个数据库独立初始化表结构schema和数据库database。 支持无数据源启动,支持懒加载数据源(需要的时候再创建连接)。 支持 自定义注解 ,需继承DS(3.2.0+)。 提供并简化对Druid,HikariCp,BeeCp,Dbcp2的快速...
CVE-2022-22947 SpringCloud GateWay SpEL RCE.doc
Exposing YAML as Properties in the Spring Environment 24.6.3. Multi-profile YAML Documents 24.6.4. YAML Shortcomings 24.7. Type-safe Configuration Properties 24.7.1. Third-party Configuration 24.7.2....
Bug Fixes 'default-lazy-init' attribute is not processed when XSD validation is disabled [SPR-8335] #12983 Spring JavaMailSenderImpl does not ...Typo in SpEL Evaluation Context Sample [SPR-17581] #22113
用于Spring Cloud Function SPEL表达式注入漏洞测试环境搭建,是编译好的服务端程序,命令号java -jar *.jar运行即可,服务端运行在127.0.0.1:8080端口
spring-framework.zip,spring表达式语言(spel)spring框架
1、为什么要使用Spring? 2、Bean 3、Spring事务 4、基本框架 5、面向切面编程 6、为Spring添加REST功能 7、spring计划任务 8、spel 9、面试