`

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同时定义了相同的初始化和销毁方法

分享到:
评论

相关推荐

    spring-framework-reference-4.1.2

    3. New Features and Enhancements in Spring Framework 4.0 ............................................ 17 3.1. Improved Getting Started Experience .........................................................

    spring-framework-reference4.1.4

    3. New Features and Enhancements in Spring Framework 4.0 ............................................ 17 3.1. Improved Getting Started Experience .........................................................

    Pro Spring 5 An In-Depth Guide -Apress(2017)

    This edition covers core Spring and its integration with other leading Java technologies, such as Hibernate, JPA 2, Tiles, Thymeleaf, and WebSocket. The focus of the book is on using Java ...

    SPRING BY EXAMPLE

    Spring In Context: Core Concepts ........................................................................................ 15 1. Spring and Inversion of Control ...........................................

    pro spring dm server

    this book, each development topic is introduced by a complete and real-world code example that you can follow step by step. Also, instead of abstract descriptions of complex concepts, you will find ...

    pro spring batch

    What you'll learn * Batch concepts and how they relate to the Spring Batch framework * How to use declarative I/O using the Spring Batch readers/writers * Data integrity techniques used by Spring ...

    ActiveMQ in Action

    Co-authored by one of the leading ActiveMQ developers, Bruce Snyder, the book starts with the anatomy of a core Java message, then moves quickly through fundamentals including data persistence, ...

    iuhyiuhkjh908u0980

    如果你使用ant 1.6.0或者更高,你可以简单的到src/example/hello-ivy 目录并运行ant: 如果构建成功,你就成功的安装了ivy! 如 ... by skydream 2009-09-02 回复 (0) ivy教程(8 ... 这个教程介绍ivy文件中的模块配置...

    restful restful所需要的jar包

    * Core REST concepts have equivalent Java classes (UniformInterface, Resource, Representation, Connector for example). * Suitable for both client-side and server-side web applications. The ...

    k8s-grpc-dotntecore:此存储库包含在.net cre 3.0中创建测试和部署grpc服务器的分步指南。 它还包括示例应用程序和yaml文件

    This example demonstrates step by step to create, run, deploy and consume a gRPC service in .NetCore 3.0. Whole process is has been categorized in 3 main section **Create, Test and Deploy**. 每个步骤...

    Sosoo 1.0网络爬虫程序.doc

    通过上述对功能的定制,我们可以看到在应用中我们对sosoo的编程接口并不多,而且目前系统都是基于set的方式注入aop注入对象,这样很容易和spring等基于set方式的依赖注入(IOC)框架集成。 1.Roboter类,spider...

Global site tag (gtag.js) - Google Analytics