码迷,mamicode.com
首页 > 其他好文 > 详细

IOC装配Bean(2)

时间:2018-04-25 10:55:23      阅读:128      评论:0      收藏:0      [点我收藏+]

标签:IOC装配Bean

Spring框架Bean实例化的方式
提供了三种方式实例化Bean.
* 构造方法实例化:(默认无参数)
* 静态工厂实例化:
* 实例工厂实例化:

无参数构造方法的实例化

<!-- 默认情况下使用的就是无参数的构造方法. -->
    <bean id="bean1" class="cn.spring.demo2.Bean1"></bean>

package cn.spring.demo2;
/*
 * 使用无参数的构造方法实例化
 */
public class Bean1 {
    public Bean1(){};
}

静态工厂实例化

<!-- 静态工程实例化 -->
    <bean id="bean2" class="cn.spring.demo2.Bean2Factory"
        factory-method="getBean2"></bean>
package cn.spring.demo2;
/*
 * 使用静态工厂实例化
 */
public class Bean2 {

}
package cn.spring.demo2;

/*
 * Bean2的静态工程
 */
public class Bean2Factory {
    public static Bean2 getBean2() {
        System.out.println("静态工程或得bean2的方法……");
        return new Bean2();
    }
}

实例工厂实例化

<!-- 使用实例工程实例化 -->
    <bean id="bean3" factory-bean="bean3Factroy" factory-method="getBean3"></bean>
    <bean id="bean3Factroy" class="cn.spring.demo2.Bean3Factory"></bean>
package cn.spring.demo2;
/*
 * 使用实例工厂实例化
 */
public class Bean3 {

}
package cn.spring.demo2;

/**
 * 使用实例方法
 * 
 * @author NOP
 * 
 */
public class Bean3Factory {
    public Bean3 getBean3() {
        System.out.println("实例工厂得bean3的方法……");
        return new Bean3();
    }

}

测试类编写

package cn.spring.demo2;

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

public class SprintTest2 {

    @Test
    // 无参数的构造方法实例化
    public void demo1() {
        ApplicationContext applicaionContext = new ClassPathXmlApplicationContext(
                "applicationContext.xml");
        Bean1 bean1 = (Bean1) applicaionContext.getBean("bean1");
        System.out.println(bean1);
    }

    @Test
    // 静态工厂实例化
    public void demo2() {
        ApplicationContext applicaionContext = new ClassPathXmlApplicationContext(
                "applicationContext.xml");
        Bean2 bean2 = (Bean2) applicaionContext.getBean("bean2");
        System.out.println(bean2);
    }

    @Test
    // 实例工程实例化
    public void demo3() {
        ApplicationContext applicaionContext = new ClassPathXmlApplicationContext(
                "applicationContext.xml");
        Bean3 bean3 = (Bean3) applicaionContext.getBean("bean3");
        System.out.println(bean3);
    }

}

Bean的其他配置

id和name的区别

  • id遵守XML约束的id的约束.id约束保证这个属性的值是唯一的,而且必须以字母开始,可以使用字母、数字、连字符、下划线、句话、冒号
  • name没有这些要求
    ***** 如果bean标签上没有配置id,那么name可以作为id.
    ***** 开发中Spring和Struts1整合的时候, /login.
    <bean name=”/login” class=””>

现在的开发中都使用id属性即可.

类的作用范围

  • scope属性:
    • singleton:单例的.(默认的值.)
    • prototype:多例的.
    • request:web开发中.创建了一个对象,将这个对象存入request范围,request.setAttribute();
    • session: web开发中.创建了一个对象,将这个对象存入session范围,session.setAttribute();
    • globalSession:一般用于Porlet应用环境.指的是分布式开发.不是porlet环境,globalSession等同于session;

实际开发中主要使用singleton,prototype

package cn.spring.demo3;

public class Customer {
    public Customer(){
        super();
        System.out.println("Customer类实例化……");
    }
}
package cn.spring.demo3;

public class Product {
    private String name ;

    public void setName(String name) {
        this.name = name;
    }
    public void setup(){
        System.out.println("初始化……");
    }
    public void teardown(){
        System.out.println("销毁……");
    }
    @Override
    public String toString() {
        return "Product [name=" + name + "]";
    }

}
<!-- cn.spring.demo3 bean作用范围-->
    <!--默认是单实例--> 
    <bean id="customer" class="cn.spring.demo3.Customer"    scope="singleton"></bean>       
        <!--
        测试类结果:单实例对象一样
        Customer类实例化…… 
        cn.spring.demo3.Customer@19e215b 
        cn.spring.demo3.Customer@19e215b
    -->
    <!-- 多实例 -->
    <bean id="customer" class="cn.spring.demo3.Customer" scope="prototype"></bean>
    <!--
        测试类结果:多实例对象不一样
        Customer类实例化…… 
        cn.spring.demo3.Customer@19762f 
        Customer类实例化……
        cn.spring.demo3.Customer@19e215b
    -->
    <!-- bean的生命周期-->
    <bean id="Product" class="cn.spring.demo3.Product" init-method="setup" destroy-method="teardown">
    <property name="name" value="空调"/>
    </bean>

    <!-- cn.spring.demo3 bean作用范围-->
package cn.spring.demo3;

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

public class SprintTest3 {
    @Test
    // 测试单实例或多实例
    public void demo1() {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
                "applicationContext.xml");
        Customer c1 = (Customer) applicationContext.getBean("customer");
        System.out.println(c1);

        Customer c2 = (Customer) applicationContext.getBean("customer");
        System.out.println(c2);
    }

    @Test
    public void demo2() {
    //ApplicationContext只会执行 init-method="setup" 方法
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(
                "applicationContext.xml");
        Product p1 = (Product) applicationContext.getBean("Product");
    /*
    *结果:
    *初始化……
    */

        // 执行销毁的方法用ClassPathXmlApplicationContext有close的方法
        ClassPathXmlApplicationContext applicationContext1 = new ClassPathXmlApplicationContext(
                "applicationContext.xml");
        Product p2 = (Product) applicationContext.getBean("Product");
        System.out.println(p2);
        // 会执行destroy-method="teardown"
        applicationContext1.close();
            /*
    *结果:
    *初始化……
    *销毁......
    */

    }
}

IOC装配Bean(2)

标签:IOC装配Bean

原文地址:http://blog.51cto.com/4534309/2107567

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