码迷,mamicode.com
首页 > 编程语言 > 详细

spring初级知识讲解(一)装配Bean

时间:2015-11-30 02:18:30      阅读:127      评论:0      收藏:0      [点我收藏+]

标签:

序,Spring的依赖注入是学习spring的基础。IOC为控制反转,意思是需要的时候就由spring生成一个,而不是先生成再使用。

写在前面

Spring提供面向接口编程,面向接口编程与依赖注入协作实现了松散耦合。

 

Spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    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   
       http://www.springframework.org/schema/tx/spring-tx-3.0.xsd   
       http://www.springframework.org/schema/context ">
       <!-- Beans 声明 -->
</beans>

从Spring获得Bean

package single.spring;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class testSpringXML {

    public static void main(String[] args) throws PerformanceException {
        //装配Spring.xml配置文件
        ApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");
        //从Spring容器获得声明的Bean
        Performer performer = (Performer) ctx.getBean("duke");
        //执行Bean的方法
        performer.perform();
    }
}

 

一、默认构造函数注入

要求此类具有默认构造函数

<bean id="duke" class="single.spring.Juggler"></bean>

 

二、通过构造器注入

1 含参构造函数,参数为基本数据类型

public Juggler(int beanBags) {
  this.beanBags = beanBags;
}

使用传参构造函数实例化Bean

<bean id="duke" 
            class="single.spring.Juggler">
    <!-- 通過含參構造函數實例化Bean -->
    <constructor-arg value="15"></constructor-arg>
    </bean>

2 含参数构造函数,参数为引用类型

Performer poeticDuke = (Performer)ctx.getBean("poeticDuke");
poeticDuke.perform();

<bean id="poeticDuke" class="single.spring.PoeticJuggler">
        <constructor-arg value="15"></constructor-arg>
        <!-- 参数类型为引用类型 用ref-->
        <constructor-arg ref="sonnet29"></constructor-arg>
    </bean>

    <bean id="sonnet29" class="single.spring.Sonnet29">
    </bean>

 

三、通过工厂方法创建Bean

有时候静态工厂方法是实例化对象的唯一方法,出于线程安全考虑,getInstance()使用了一种被称为“initialization on demand holder”的技术来创建单例类的实例。

Initialization on Demand Holder模式,这种方法使用内部类来做到延迟加载对象,在初始化这个内部类的时候,JLS(Java Language Sepcification)会保证这个类的线程安全(the class initialization phase is guaranteed by the JLS to be serial),这种写法最大的美在于,完全使用了Java虚拟机的机制进行同步保证,没有一个同步的关键字。

package single.spring;

public class Stage {
    private Stage() {
    }

    //延迟加载实例
    private static class StageSingletonHolder {
        static Stage instance = new Stage();
    }
    //返时实例
    public static Stage getInstance() {
        return StageSingletonHolder.instance;
    }
}

 

spring.xml配置,factory-method属性配置静态方法

<bean id="theStage" class="single.spring.Stage" factory-method="getInstance"></bean>

 

四、Bean的作用域

所有的Spring Bean默认都是单例,但是Spring提供了一个scope属性来每次成不同的实例

singleton:在每个Spring容器中,一个Bean定义只有一个对象实例(默认)

prototype:允许Bean的定义可以被实例化任意次(每次调用都创建一个实例)

request:在一次Http请求中,每个Bean定义对应一个实例,该作用域仅在基于Web的Spring的上下文(例如Spring MVC)中才有效。

session:在一个Http Session中,每个Bean定义对应一个实例。该作用域仅在基于Web的Spring上下文(例如Spring MVC)中才有效。

global-session:在一个全局Http Session中,每个Bean定义对应一个实例。该作用域仅在Portlet上下文中才有效。

五、初始化和销毁Bean

为了满足初始化和销毁Bean的需求,Spring提供了Bean声明周期的钩子方法。

init-method:提供Bean初始化时调用方法
destroy-method:提供Bean销毁时调用方法
<bean id="auditorium" class="single.spring.Auditorium" init-method="turnOnLights" destroy-method="turnOffLights"></bean>

 

六、注入Bean属性

 

1 setter方法注入

 

 

七、装配集合

spring初级知识讲解(一)装配Bean

标签:

原文地址:http://www.cnblogs.com/yaochc/p/5006081.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!