<?xml version="1.0" encoding="UTF-8"?>
<!-- 使用Spring的命名空间p装配属性,配置p -->
<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" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"
default-init-method="turnonLights" default-destroy-method="turnOffLights">
<!-- 如上两行,为容器中所有的Bean设置初始化和销毁方法 -->
<!-- Bean declarations go here -->
<bean id="duke" class="com.bean.PoeticJuggler">
<!-- 构造函数参数 -->
<constructor-arg value="15" />
<constructor-arg value="sonnet29" />
</bean>
<bean id="sonnet29" class="com.bean.Sonnet29" />
<!-- factory-method==>对于没有公开构造方法的Bean,配置静态工厂方法 -->
<bean id="theStage" class="com.bean.Stage" factory-method="getInstance" />
<!-- bean初始化调用方法设置init-method bean销毁时调用方法设置destroy-method -->
<bean id="auditorium" class="com.bean.Auditorium" init-method="turnOnLights"
destroy-method="turnOffLights" />
<bean id="kenny" class="com.bean.Instrumentalist">
<property name="song" value="Jingle Bells"/>
<!-- 引用其他的bean -->
<property name="instrument" ref="saxophone"/>
<!-- 定义内部Bean,仅该bean可用
内部bean还可以作为构造方法参数传入
<constructor-arg>
<bean class="com.bean.SaxoPhone"/>
</constructor-arg>
-->
<property name="instrument">
<bean class="com.bean.SaxoPhone"/>
</property>
</bean>
<!-- 使用命名空间p来配置 -->
<bean id="kenny2" class="com.bean.Instrumentalist"
p:song="Jingle Bells"
p:instrument-ref="saxophone"/>
<bean id="saxophone" class="com.bean.SaxoPhone"/>
<!--
Collection<Instrument>
List<Instrument>
Instrument[]
集合元素配置,数组和Collection的实现类的集合均可用list或者set来描述
list中元素允许重复
set中元素不允许重复
map中名称和值可以是任意类型
props中名称和值必须是String
-->
<bean id="hank" class="com.bean.OneManBand">
<property name="instruments">
<list>
<ref bean="guitar"/>
<ref bean="cymabal"/>
<ref bean="harmonica"/>
</list>
</property>
</bean>
<!--
map装配key-value形式集合,内容可以是任何类型
<entry>中的键和值:
key:指定map中entry的键为String
key-ref:指定map中的entry的键为Spring上下文中其他的Bean引用
value:指定map中的entry的值为String
value-ref:定map中的entry的键为Spring上下文中其他的Bean引用
-->
<bean id="hank2" class="com.bean.OneManBand">
<property name="instrments">
<map>
<entry key="GUITAR" value-ref="guitar"></entry>
<entry key="CYMABAL" value-ref="cymabal"></entry>
<entry key="HARMONICA" value-ref="harmonica"></entry>
</map>
</property>
</bean>
<!--
<props>装配properties类型
<property>元素用于把值或者Bean引用注入到Bean的属性中
<props>元素用于定义一个java.util.Properties类型的集合值
<prop>元素用于定义<props>集合的一个成员。
-->
<bean id="hank3" class="com.bean.OneManBand">
<property name="instruments">
<props>
<prop key="GUITAR">STRUM STRUM STRUM</prop>
<prop key="CYMBAL">CRASH CRASH CRASH</prop>
<prop key="HARMONICA">HUM HUM HUM</prop>
</props>
</property>
</bean>
<!--设置null元素 -->
<bean id="someNonNullProperty" class="com.bean.null">
<property name="someNonNullProperty"><null/></property>
</bean>
<!--
Spring表达式语言:SpEL,他通过运行期执行的表达式将值装配到Bean的属性或构造器的参数中
SpEL特性:
使用Bean的ID来引用Bean
调用方法和访问对象的属性
对值进行算术、关系和逻辑运算
正则表示匹配
集合操作
-->
<bean id="SpEL" class="SpEL">
<!-- 字面值引用 -->
<property name="count" value="#{5}"/>
<property name="message" value="The value is #{5}"/>
<property name="frequency" value="#{89.7}"/>
<property name="capacity" value="#{1e4}"/>
<property name="name" value="#{'Chuck'}"/>
<property name="name" value='#{"Chuck"}'/>
<property name="enabled" value="#{false}"/>
<!-- 引用Bean、Properties和方法
Bean:#{Bean的ID}
-->
<property name="instruments" value="#{saxophone}"/>
<!-- 引用Bean的属性的值 -->
<property name="instruments" value="#{saxophone.name}"/>
<!-- 调用方法 -->
<property name="song" value="#{songSelector.selectSong().toUpperCase()}"/>
<!--
调用方法中如果返回值是一个null值,将出现NullPointerException,
避免的方法:使用null-safe
如果selectSong()方法返回的是null则不再调用toUpperCase()
-->
<property name="song" value="#{songSelector.selectSong()?.toUpperCase()}"/>
<!-- 操作类:在SpEL中,使用T()运算符会调用类作用域的方法和常量 -->
<property name="multiplier" value="#{T(java.lang.Math).PI}"/>
</bean>
<!-- SpEL执行操作 -->
<!--
-------------------------------------------------------------
运算符类型 |运算符
-------------------------------------------------------------
算术运算 |+、-、×、/、%、^
-------------------------------------------------------------
关系运算 |<、>、==、<=、>=、lt、gt、eq、le、ge
-------------------------------------------------------------
逻辑运算 |and、or、not、|
-------------------------------------------------------------
条件运算 |?:(ternary)、?:(Elvis)
--------------------------------------------------------------
正则表达式 |matches
--------------------------------------------------------------
-->
<bean id="cacl" class="com.bean.clas">
<property name="adjustedAmount" value="#{counter.total+42}"/>
<property name="adjustedAmount" value="#{counter.total-42}"/>
<property name="circumference" value="#{2*T(java.lang.Math).PI*circle.radius}"/>
<property name="average" value="#{counter.total/counter.count}"/>
<property name="remainder" value="#{counter.total%counter.count}"/>
<property name="area" value="#{T(java.lang.Math).PI*circle.radius^2}"/>
<property name="fullName" value="#{performer.firstName+' ' +performer.lastName}"/>
<property name="equal" value="#{counter.total==100}"/>
<property name="hasCapacity" value="#{counter.total le 10000}"/>
<property name="largeCircle" value="shape.kind == 'circle' and shape.perimeter gt 10000"/>
<property name="outOfStock" value="#{!product.available}"/>
<property name="outOfStock" value="#{not product.available}"/>
<property name="instrument" value="#{songSelector.selectSong()=='Jingle Bells'?piano:saxophone}"/>
<property name="validEmail" value="#{admin.email matches '[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.com'}"/>
<!-- 用<util:list>元素定义一个List集合 -->
</bean>
</beans>
相关推荐
java Spring DI依赖注入.rar
SPRING DI注入例子 jar包没有包含
Spring依赖注入(DI)的例子,包括接口注入、构造注入、set注入的简单类型和复杂类型注入的例子。
spring di 的myeclipse完整代码
一步一步手绘spring DI运行时序图.rar
NULL 博文链接:https://zwhc.iteye.com/blog/665932
笔记整理
Spring Ioc DI 时序图分享给大家,希望对大家看源码有所帮助,不足之处欢迎批评指正,可以在下方留言
DI(Dependency Injection)依赖注入:就是指对象是被动接受依赖类而不是自己主动去找,换句话说就是指对象不是从容器中查找它依赖的类,而是在容器实例化对象的时候主动将它依赖的类注入给它。
NULL 博文链接:https://bepatient.iteye.com/blog/1036402
SpringDI
今天有空,写了基于C#使用Spring.Net的演示实例,希望能给有需要的人带来帮助,其中演示了配置下的IOC、AOP、属性注入、构造函数注入、通知过滤器、以及不使用配置直接代码硬编的AOP动态代码过程,另外还增加了...
一个 Maven 原型,用于快速设置由 Jetty + Jersey + Spring DI 提供支持的 RESTful web 服务 为什么是 Maven 原型? 因为我很懒,我想有一个简单的方法来快速启动 REST 服务器并开始原型设计。 原型创造了什么? ...
什么是spring,spring核心,spring优点,spring体系结构, 入门案例,DI基础,核心API,文档内附代码
spring-storm:将Spring DI与Apache Storm结合使用的示例模式
该资源为本人博客文章“什么是SpringIOC?如何理解SpringIOC?”和“解析Spring DI 依赖注入的5种方式”中SpringOIC、DI演示案列文章工程代码,可下载学习!
spring-context.xsd用于spring DI开发代码提示。用于Java开发。(包含2.5/3.1/3.2版本)
弹簧DI 使用Spring框架进行依赖注入测试这是我第一次学习使用spring依赖注入。 我在实习期间将这个框架应用于正在工作的项目。
spring-context-3.1.xsd用于spring DI开发代码提示。用于Java开发。
spring ioc di aop详解