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

spring bean

时间:2017-07-18 20:10:20      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:star   import   舞台   单例对象   copy   tag   结果   process   from   

在Spring中有许多的容器,在以前都是使用Bean工厂,但是现在基本都是使用应用上下文。

  下面是几种常用的应用上下文加载方式:

  ClassPathXmlApplicationContext:通过加载类路径下的xml文件,最常使用的方式,即加载src目录下的bean.xml文件。

  FileSystemXmlApplicationContext:通过指定绝对路径加载配置文件。

  XmlWebApplicationContext:读取web应用下的配置文件加载。

  

  Bean的生命周期:

 
1 实例化
2 注入属性
3 BeanNameAware
4 BeanFactoryAware
5 ApplicationContextAware
6 BeanPostProcessor,ProcessBeforeInitialization
7 Initilalization
8 BeanPostProcessor,ProcessAfterInitialization
9 可以使用
10 DisposableBean destroy
 

 

可以通过constructor-arg 来传递构造函数的参数

 
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.springframework.org/schema/beans"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
     <bean id="duke" class="com.spring.test.action1.Juggler">
         <constructor-arg value="15"/>
     </bean>
</beans>
 

  4 应用上下文获

 

 

如果有这样的需求

  1 不想再bean.xml加载的时候实例化bean,而是想把加载bean.xml与实例化对象分离。

  2 实现单例的bean

以上的情况,都可以通过工厂方法factory-method来创建bean

这样再加载bean.xml时,不会直接实例化bean,而是当调用factory-method所指的方法时,才开始真正的实例化。

  首先看一下传统的单例模式的实现方式:

  1 最原始的实现单例模式的方法(存在线程不安全):

 
public class SingletonOne {
    private static SingletonOne instance = null;
    private SingletonOne() {}
    public static SingletonOne getInstance() {
        if (instance == null) {
            instance = new SingletonOne(); 
        }
        return instance;
    }
}    
 

  但是这种方法有一个弊端,就是存在线程的不安全!

  比如当两个线程同时进入if(instance == null)时,一个线程判断了当前为空,然后切换到另一个线程,这个线程也判断为空。然后切换回第一个线程,进行实例化,再切换到第二个线程,进行实例化。这样就存在了两个实例。

  2 通过关键字Synchronized强制线程同步 

 
package com.something.singleton;
public class SingletonTwo {
    private static SingletonTwo instance = null;
    private SingletonTwo() {}
    public static synchronized SingletonTwo getInstance() {
        if (instance == null) {
            instance = new SingletonTwo();
        }
        return instance;
    }
}
 

  这样当线程进行到getInstance会同步的进行,不会有线程安全问题,但是不仅仅是实例化,每次调用也需要同步,这样就会造成很多资源的浪费。

  3 通过静态内部类进行单例

 
public class SingletonThree {
    private static class SingletonHolder{
        static SingletonThree instance = new SingletonThree();
    }
    
  private SingletonThree() {} public static SingletonThree getInstance() { return SingletonHolder.instance; } }
 

  这种方法时最推荐的一种方法,由于Java的调用机制,SingletonHolder只有在调用getInstance的时候才会加载,而内部的静态类只会被加载一次,因此又是线程安全的。

  总结起来

  第一种方法,是存在线程安全问题的。

  第二种方法,则消耗了一定的资源。

  第三种方法,比较推荐。

 

通过spring的factory-method来创建单例的bean

  首先通过静态内部类创建一个单例对象

package com.spring.test.factorymethod;

public class Stage {
    public void perform(){
        System.out.println("演出开始...");
    }
    private Stage(){
        
    }
    private static class StageSingletonHolder{
        static Stage instance = new Stage();
    }
    public static Stage getInstance(){
        return StageSingletonHolder.instance;
    }
}

  在spring配置文件中指定加载的方法getInstance

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.springframework.org/schema/beans"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
     <bean id="theStage" class="com.spring.test.factorymethod.Stage"
         factory-method="getInstance"></bean>
</beans>

  通过应用上下文调用bean获取实例

package com.spring.test.factorymethod;

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

public class test {
    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml");
        Stage stage = ((Stage)ctx.getBean("theStage"));//.getInstance();
        stage.perform();
    }
}

  执行结果

一月 24, 2015 6:38:18 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@512dbd1a: startup date [Sat Jan 24 18:38:18 CST 2015]; root of context hierarchy
一月 24, 2015 6:38:19 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [bean.xml]
一月 24, 2015 6:38:19 下午 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@2d1879ea: defining beans [duke,sonnet29,poeticDuke,theStage]; root of factory hierarchy
演出开始...

 

Singleton:在一个应用上下文容器中,所有的线程或对象通过getBean获得指定id的Bean,得到的都是同一个实例。

      这种的Bean实例完全由应用上下文容器来控制声明周期,用户无论何时何地得到的实例都是同一个

Prototype:  这种类型的Bean会在每一次都创建一个新的实例,而实例的生命周期仅仅由应用上下文控制其初始化和装配,一旦初始化成功,控制权就会交给用户

 

通过上面对生命周期的讲解,可以了解到,我们在spring中使用init-method和destroy-method方法时,仅仅在默认情况即singleton模式下,destroy-method才会起作用。

  下面做个小例子,做一下验证:

  举个例子,一个舞台Bean,我们想在使用舞台前打开灯光,在舞台使用后关闭灯光再拆掉舞台。就可以通过init-method和destroy-method来指定方法。

技术分享
package com.spring.test.initdesotry;

public class Stage {
    public void perform(){
        System.out.println("演出开始...");
    }
    public void turnOnLight(){
        System.out.println("演出开始前,开灯...");
    }
    public void turnOffLight(){
        System.out.println("演出结束前,关灯...");
    }
}
技术分享

  配置bean.xml

技术分享
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.springframework.org/schema/beans"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">    
     <bean id="stage" class="com.spring.test.initdesotry.Stage"
         scope="prototype" //注意这句话,如果是singleton或者没有该句(默认情况)时,才会执行destroy-method指定的方法,如果是当前的prototype不会触发destroymethod的执行
         init-method="turnOnLight" 
         destroy-method="turnOffLight"/>
</beans>
技术分享

  在主函数中,不要忘记应用上下文容器的关闭,只有这样,才会出发destroy-method的执行。

技术分享
package com.spring.test.initdesotry;

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

public class test {
    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("bean.xml");
        Stage stage = (Stage)ctx.getBean("stage");
        stage.perform();
        ((ClassPathXmlApplicationContext) ctx).close();//关闭应用上下文容器,不要忘记这句话
    }
}
技术分享

  当Bean是singleton模式或者默认时,会得到如下的结果:

技术分享
一月 25, 2015 1:30:42 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@4f5cb20e: startup date [Sun Jan 25 13:30:42 CST 2015]; root of context hierarchy
一月 25, 2015 1:30:42 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [bean.xml]
一月 25, 2015 1:30:42 下午 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@68df6513: defining beans [duke,sonnet29,poeticDuke,theStage,stage]; root of factory hierarchy
演出开始前,开灯...
演出开始...
一月 25, 2015 1:30:42 下午 org.springframework.context.support.AbstractApplicationContext doClose
信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@4f5cb20e: startup date [Sun Jan 25 13:30:42 CST 2015]; root of context hierarchy
一月 25, 2015 1:30:42 下午 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry destroySingletons
信息: Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@68df6513: defining beans [duke,sonnet29,poeticDuke,theStage,stage]; root of factory hierarchy
演出结束前,关灯...
技术分享

 

  当是prototype模式时,得到如下的结果:

技术分享
一月 25, 2015 1:05:40 下午 org.springframework.context.support.AbstractApplicationContext prepareRefresh
信息: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@4eadddd6: startup date [Sun Jan 25 13:05:40 CST 2015]; root of context hierarchy
一月 25, 2015 1:05:40 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
信息: Loading XML bean definitions from class path resource [bean.xml]
一月 25, 2015 1:05:40 下午 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
信息: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@2264f82f: defining beans [duke,sonnet29,poeticDuke,theStage,stage]; root of factory hierarchy
演出开始前,开灯...
演出开始...
一月 25, 2015 1:05:40 下午 org.springframework.context.support.AbstractApplicationContext doClose
信息: Closing org.springframework.context.support.ClassPathXmlApplicationContext@4eadddd6: startup date [Sun Jan 25 13:05:40 CST 2015]; root of context hierarchy
一月 25, 2015 1:05:40 下午 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry destroySingletons
信息: Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@2264f82f: defining beans [duke,sonnet29,poeticDuke,theStage,stage]; root of factory hierarchy
技术分享

  

 

spring bean

标签:star   import   舞台   单例对象   copy   tag   结果   process   from   

原文地址:http://www.cnblogs.com/diegodu/p/7202374.html

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