spring概述(二)--bean简介

bean的实例化方式:

①.构造器实例化(无参数构造器),最标准,使用最多
②.静态工厂方法实例化:解决系统遗留问题

 <!-- Ioc: 控制反转  将class交给spring容器管理-->
    <!-- 使用静态工厂 -->
    <bean id="someBean1" class="gzcPro.spring._02instance.static_factory.SomeBeanFactory"
     factory-method="getObject">
    </bean>
//静态工厂
public class SomeBeanFactory {
    public static SomeBean getObject(){
        return new SomeBean();
    }

}

    //自动从spring容器中获取这个bean
    @Autowired
    private ApplicationContext context;
    
    @Test
    public void testAppCtx() throws Exception{
        context.getBean("someBean1");
    }

    @Test
    public void testOld() throws Exception{
        SomeBean someBean=SomeBeanFactory.getObject();
    }

③.实例工厂方法实例化:解决系统遗留问题

 <!-- 使用实例工厂-->
    <bean id="factory"  class="gzcPro.spring._02instance.factory.SomeBeanFactory" />
    <bean id="someBean" factory-bean="factory" factory-method="getObject" />
public class SomeBeanFactory implements FactoryBean<SomeBean>{
    public  SomeBean getObject(){
        return new SomeBean();
    }
}    
//自动从spring容器中获取这个bean
    @Autowired
    private ApplicationContext context;

    @Test
    public void testAppCtx() throws Exception{
        System.out.println(context.getBean("someBean"));
    }

    @Test
    public void testOld() throws Exception{
        SomeBeanFactory factory=new SomeBeanFactory();
        SomeBean someBean= factory.getObject();
    }

④.实现FactoryBean接口实例化:实例工厂变种:
集成其他框架使用:SqlSessionFactoryBean和MapperFactoryBean

<!-- FactroyBean创建 -->
<bean id="someBean"  class="gzcPro.spring._02instance.instance_factory.SomeBeanFactory" />
public class SomeBeanFactory implements FactoryBean<SomeBean>{

    public  SomeBean getObject(){
        return new SomeBean();
    }

    @Override
    public Class<?> getObjectType() {
        return null;
    }

    //bean是否单例
    @Override
    public boolean isSingleton() {
        return false;
    }
}

bean作用域

1.单例
2.多例

bean对象的作用域(bean对象可以存活的时间):
<bean id="" class="" scope="作用域"/>
singleton: 单例 ,在Spring IoC容器中仅存在一个Bean实例 (默认的scope)
prototype: 多例 ,每次从容器中调用Bean时,都返回一个新的实例,即每次调用getBean()时 ,相当于执行new XxxBean():不会在容器启动时创建对象
request: 用于web开发,将Bean放入request范围 ,request.setAttribute(“xxx”) , 在同一个request 获得同一个Bean
session: 用于web开发,将Bean 放入Session范围,在同一个Session 获得同一个Bean
globalSession: 一般用于Porlet应用环境 , 分布式系统存在全局session概念(单点登录),如果不是porlet环境,globalSession 等同于Session
在开发中主要使用 scope=“singleton”、 scope=“prototype”
对于Struts2中的Action使用prototype类型,其他使用singleton
Spring容器会管理Action对象的创建,此时把Action的作用域设置为prototype.

bean的生命周期 ( init destroy )

<?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.xsd"
       default-init-method="init"
       default-destroy-method="destroy">

    <!-- Ioc: 控制反转  将class交给spring容_03scopecope="prototype"多例-->
    <!-- Ioc: 控制反转  将class交给spring容器管理 
    init-method="init" 
    destroy-method="destroy"-->
    <bean id="someBean" class="gzcPro.spring._03scope.SomeBean">
    </bean>
</beans>
public class SomeBean {
    public SomeBean(){
        System.out.println("bean被创建了");
    }

    public void init() {
        System.out.println("初始化");
    }

    public void destroy() {
        System.out.println("被销毁");
    }

    public void doWorlk() {
        System.out.println("工作");
    }
}
@RunWith(SpringJUnit4ClassRunner.class)//引入单元测试
@ContextConfiguration("App_context.xml")//引入配置文件
public class App {

    //自动从spring容器中获取这个bean
    @Autowired
    private ApplicationContext context;

    @Test
    public void testAppCtx(){
        SomeBean someBean1=context.getBean("someBean",SomeBean.class);
        SomeBean someBean2=context.getBean("someBean",SomeBean.class);
        System.out.println(someBean1==someBean2);
        someBean1.doWorlk();
    }
}

bean被创建了
初始化
true
工作
被销毁

发布了6 篇原创文章 · 获赞 0 · 访问量 183

猜你喜欢

转载自blog.csdn.net/hero_chao/article/details/103960126
今日推荐