Spring By example -- core

Spring IOC 容器的两种实现  BeanFactory 和ApplicationContext

DefaultListableBeanFactory作为一个功能完备的默认的IOC容器

 

ApplicationContext 除了基本的功能外,还支持不同的信息源,访问资源,支持应用事件,提供附加服务;比基本的BeanFactory提供了更高级的形态

 

IOC容器的初始化过程

1. BeanDefinitioin的Resource定位

    对于不同的ApplicationContext,会对应不同的Rsource,例如ClassPathResource,ServletContextResource等

2. BeanDeifinition的载入和解析

3. BeanDefinition在容器中的注册

 

IOC容器的依赖注入

触发于getBean,具体过程可以参考AbstractBeanFactory的doGetBean

1.从缓存中取得,如果已创建,则返回否则转2

2. 在双亲BeanFactory中寻找,如果找不到,则递归调用寻找,如果找到返回,否则转3

3. 如果有依赖Bean则递归查找,一直找到没有任何依赖Bean,

    BeanWrapper完成Bean的属性注入

 

Spring comes with several XML namespaces through which you can configure the Spring

container

aop  Provides elements for declaring aspects and for automatically proxying @AspectJannotated classes as Spring aspects.

beans The core primitive Spring namespace, enabling declaration of beans and how they

should be wired.

context Comes with elements for configuring the Spring application context, including the abil-

ity to autodetect and autowire beans and injection of objects not directly managed by

Spring.

jee Offers integration with Java EE APIs such as JNDI and EJB.

jms Provides configuration elements for declaring message-driven POJOs.

lang Enables declaration of beans that are implemented as Groovy, JRuby, or BeanShell

scripts.

mvc Enables Spring MVC capabilities such as annotation-oriented controllers, view control-

lers, and interceptors.

oxm Supports configuration of Spring’s object-to-XML mapping facilities.

tx Provides for declarative transaction configuration.

util A miscellaneous selection of utility elements. Includes the ability to declare collec-

tions as beans and support for property placeholder elements.

Spring注入方式

接口注入,setter注入,构造器注入

Injecting simple values

<bean id="kenny"
class="com.springinaction.springidol.Instrumentalist">
<propertyname="song"value="JingleBells"/>
</bean>

 Referencing other beans

<bean id="saxophone"
class="com.springinaction.springidol.Saxophone"/>

<bean id="kenny2"
class="com.springinaction.springidol.Instrumentalist">
<propertyname="song"value="JingleBells"/>
<propertyname="instrument"ref="saxophone"/>
</bean>

 INJECTING INNER BEANS

<bean id="kenny"
class="com.springinaction.springidol.Instrumentalist">
<propertyname="song"value="JingleBells"/>
<propertyname="instrument">
<beanclass="org.springinaction.springidol.Saxophone"/>
</property>
</bean>


--------------------------------

<bean id="duke"
class="com.springinaction.springidol.PoeticJuggler">
<constructor-argvalue="15"/>
<constructor-arg>
<beanclass="com.springinaction.springidol.Sonnet29"/>
</constructor-arg>
</bean>

Wiring properties with Spring’s p namespace

<?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-3.0.xsd">
<bean id="kenny"class="com.springinaction.springidol.Instrumentalist"
p:song="JingleBells"
p:instrument-ref="saxophone"/>

Wiring collections

<list> Wiring a list of values, allowing duplicates

<set> Wiring a set of values, ensuring no duplicates

<map> Wiring a collection of name-value pairs where name and value

can be of any type

<props> Wiring a collection of name-value pairs where the name and

value are both Strings

<bean id="hank"
class="com.springinaction.springidol.OneManBand">
<propertyname="instruments">
<list>
<refbean="guitar"/>
<refbean="cymbal"/>
<refbean="harmonica"/>
</list>
</property>
</bean>
<bean id="hank"
class="com.springinaction.springidol.OneManBand">
<propertyname="instruments">
<set>
<ref bean="guitar"/>
<ref bean="cymbal"/>
<ref bean="harmonica"/>
<ref bean="harmonica"/>
</set>
</property>
</bean>
<bean id="hank"class="com.springinaction.springidol.OneManBand">
<propertyname="instruments">
<map>
<entry key="GUITAR" value-ref="guitar"/>
<entry key="CYMBAL" value-ref="cymbal"/>
<entry key="HARMONICA" value-ref="harmonica"/>
</map>
</property>
</bean>

 map wiring property

key Specifies the key of the map entry as a String

key-ref Specifies the key of the map entry as a reference to a bean in the Spring context

value Specifies the value of the map entry as a String

value-ref Specifies the value of the map entry as a reference to a bean in the Spring context

private Propertiesinstruments;
public voidsetInstruments(Propertiesinstruments){
this.instruments=instruments;
}
<bean id="hank"class="com.springinaction.springidol.OneManBand">
<propertyname="instruments">
<props>
<propkey="GUITAR">STRUMSTRUMSTRUM</prop>
<propkey="CYMBAL">CRASHCRASHCRASH</prop>
<propkey="HARMONICA">HUMHUMHUM</prop>
</props>
</property>
</bean>

bean scopes

singleton Scopes the bean definition to a single instance per Spring container (default).

prototype Allows a bean to be instantiated any number of times (once per use).

request Scopes a bean definition to an HTTP request. Only valid when used with a

web-capable Spring context (such as with Spring MVC).

session Scopes a bean definition to an HTTP session. Only valid when used with a

web-capable Spring context (such as with Spring MVC).

global-session Scopes a bean definition to a global HTTP session. Only valid when used in a portlet context.

Initializing and destroying beans

第一种 定义 init-method   destroy-method

<bean id="auditorium"
class="com.springinaction.springidol.Auditorium"
init-method="turnOnLights"
destroy-method="turnOffLights"/>

 第二种  实现InitializingBean和DisposableBean

            如果用第二种方式,你的代码是和sping耦合在一起的,建议不使用

第三种 default-init-method default-destroy-method  

         这种情况适合于有多个bean同时定义了相同的初始化和销毁方法

猜你喜欢

转载自ilnba.iteye.com/blog/1836221