现在的位置: 首页 > 综合 > 正文

Spring进阶(一)

2013年10月10日 ⁄ 综合 ⁄ 共 3063字 ⁄ 字号 评论关闭

 

了解了Spring的大致功能后,需要进一步了解细节。

今天我们来看看对于一个bean,我们可以用Spring做哪些事情。

1.Container

首先我们需要了解一下container的概念。

The container is at the core of the Spring Framework. Spring’s container uses
dependency injection (DI) to manage the components that make up an application.
This includes creating associations between collaborating components.

container咋翻译啊。。。咱就叫它容器吧。

容器是Spring框架的核心。Spring容器使用dependency injectionDI管理构成程序的各个组件。

包括创建各组件间的相互关系。所有bean的创建,使用,销毁都在容器中进行。

Spring comes with several container implementations that can be categorized into two distinct types.

Spring有好几种容器,可以被分为两类:

Bean factories (defined by the org.springframework.beans.factory.BeanFactory interface)是最简单的容器,提供对DI的最基本的支持。

Application contexts (defined by theorg.springframework.context.ApplicationContext interface)

build on the notion of a bean factory by providing application framework services, such as the ability to
resolve textual messages from a properties file and the ability to publish application
events to interested event listeners.

看到这里一定还是不清晰到底容器是啥。其实,我们在前面看到的:

ApplicationContext ctx=new ClassPathXmlApplicationContext(
"com/springinaction/springidol/spring-idol.xml");

就是Application contexts容器。ApplicationContext即spring用xml构建出来的真正的程序内容。

所有里面包含的bean,以及bean之间的交互,都是在ApplicationContext 这个container内进行的。

2. 建立一个Bean的一些细节

  • 使用构造函数实例化对象。

用简单的语言解释一下下面的例子:

<bean id="poeticDuke" class="com.springinaction.springidol.PoeticJuggler">
    <constructor-arg value="15"/>
    <constructor-arg ref="sonnet29"/>
</bean>

<bean id="sonnet29" class="com.springinaction.springidol.Sonnet29"/>

创建一个id为poeticDuke的bean,class是指定的类。

如果没有constructor element,则Spring会使用默认constructor实例化对象。

而使用了:

    <constructor-arg value="15"/> //用于传递simple parameter to constructor
    <constructor-arg ref="sonnet29"/> //用于传递其他的bean id作为constructor的输入参数

就等价于下面的constructor:

public PoeticJuggler(int beanBags,Poem poem){
    super(beanBags);
    this.poem=poem;
}//sonnet29是poem的一个实例

  • 使用工厂实例化对象

如果不使用构造函数实例化对象,Spring可以使用factory方法来完成。不过需要代码中有getInstance的方法哦。

<bean id="theStage" class="com.springinaction.springidol.Stage" factory-method="getInstance" />

  • property

如果需要在实例化对象时,config 成员变量的值,则可以使用以下方法。但是在代码中必须使用get/set方法来设置成员变量的值。

<bean id="kenny" class="com.springinaction.springidol.Instrumentalist"> //此时会使用默认的构造函数来实例化对象哦~
    <property name="song" value="JingleBells"/>
</bean>

  • scope

By default, all Spring beans are singletons. When the container dispenses a bean
(either through wiring or as the result of a call to the container’s getBean() method)
it’ll always hand out the exact same instance of the bean.

这段话非常非常重要。即在默认情况下,spring创建的对象都是singleton模式的。对于一个bean来说,只会有一个实例,只会一直使用同一个实例。

<bean id="ticket" class="com.springinaction.springidol.Ticket"scope="prototype"/>

//如果使用了prototype,则spring每次都会提供新的实例。

3.创建和销毁一个bean

这部分要说的内容很简单。就是在创建和销毁bean前后,可以执行指定的逻辑。

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

<?xml version="1.0"encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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"
default-init-method="turnOnLights"
default-destroy-method="turnOffLights"> //则是对所有bean都有效
</beans>

抱歉!评论已关闭.