`

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 .........................................................

    spring data jpa入门实例

    -- Spring Core --&gt; &lt;groupId&gt;org.springframework &lt;artifactId&gt;spring-core ${spring.version} &lt;!-- Spring MVC --&gt; &lt;groupId&gt;org.springframework &lt;artifactId&gt;spring-webmvc ${spring.version} ...

    Developing a Spring Framework MVC application step-by-step

    例如,添加Spring Web和Spring Core库: ```xml &lt;groupId&gt;org.springframework &lt;artifactId&gt;spring-webmvc &lt;version&gt;5.x.y.RELEASE &lt;groupId&gt;org.springframework &lt;artifactId&gt;spring-context ...

    SPRING BY EXAMPLE

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

    spring mvc step by step

    ### Spring MVC Step by Step知识点详解 #### 概览 本文档详细介绍了如何使用Spring Framework MVC框架逐步构建一个完整的Web应用程序。文档版本为2.5,由Thomas Risberg、Rick Evans和Portia Tung共同编著,版权...

    SpringBoot+Mybatis-Plus整合Sharding-JDBC5.1.1实现单库分表【全网最新】.doc

    Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userMapper' defined in file [D:\jiawayun\demo\target\classes\com\example\demo\mapper\UserMapper.class...

    SSH环境搭建

    - **Spring框架**:导入6个包,包括`spring-core-xx.jar`、`spring-beans-xx.jar`、`spring-context-xx.jar`、`spring-web-xx.jar`、`spring-webmvc-xx.jar`、`spring-tx-xx.jar`。 - **Struts框架**:导入2个包,...

    SPRING面试宝典

    import org.springframework.core.io.ClassPathResource; public class BeanFactoryExample { public static void main(String[] args) { // 创建一个XML配置文件的资源 ClassPathResource resource = new Class...

    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, ...

    SpringBoot默认日志配置说明

    2022-03-06 14:54:31.657 INFO [main] com.example.Application --- Starting Application on DESKTOP-XXXX with PID 1234 (D:\workspaces\myapp\target\classes started by user in D:\workspaces) ``` 如果你想...

    ebean插件压缩包(最终版)

    2. **查询语言**: Ebean提供了一种强大的查询语言(QBE,Query By Example),允许开发者通过实例化对象来构造查询条件,既直观又灵活。 3. **事务管理**: Ebean内置事务管理机制,能与Java的@Transactional注解...

    解决springboot yml配置 logging.level 报错问题

    在Spring Boot应用中,日志配置是至关重要的,它帮助我们跟踪应用程序的运行状态和调试问题。YAML(YAML Ain't Markup Language)是Spring Boot推荐的配置文件格式,但有时候我们在配置`logging.level`时可能会遇到...

    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**. 每个步骤...

Global site tag (gtag.js) - Google Analytics