spring-ioc

实例化bean的三种方式:

<!-- 默认的 
          把类添加到spring容器中
            -->

           <bean id="helloWorld" class="com.example.vic.test_spring.ioc_di_setter_HelloWorld.HelloWorld">

            <!-- 静态工厂    工厂方法是静态的-->

          <bean  id="helloWorld1" class="com.example.vic.test_spring.ioc_di_setter_HelloWorld.FactoryClassHelloWorld1" factory-method="getInstance">
           </bean>  


           <!-- 实例工厂 -->
           <bean id="factoryHelloWorld" class="com.example.vic.test_spring.ioc_di_setter_HelloWorld.FactoryClassHelloWorld2">
           </bean>

           <bean id="helloWorld2" factory-bean="factoryHelloWorld" factory-method="getInstance">
           </bean>

注意

 <bean id="helloWorld" class="com.example.vic.test_spring.ioc_di_setter_HelloWorld.HelloWorld" lazy-init="true" scope="prototype" init-method="">
            <!--注入属性-->
    <property name="" value=""></property> 
 </bean> 

第一点– 因为ApplicationContext context =
new ClassPathXmlApplicationContext(“applicationContext.xml”);
会加载所有中的类并生成id类的对象。

第二点 lazy-init=”default” 是默认false,不是延迟加载(加载xml同时生成对象), lazy-init=”true” ,会延迟加载,不会加载xml同时生成对象。

第三点–还有个属性scope=“prototype” 他会创造多个bean对象,因为spring容器默认为单类模式,只会创造一个对象。

第四点–执行顺序,
0.启动spring容器
1.创建helloWorld对象
2.setter 方法注入
3.init-method
4. destroy-method 注销时

public class HelloWorld {
    public HelloWorld() {
        System.out.println("new instance");
    }
    public void printHelloWorld(){
        System.out.println("调用方法");
    }
}
public class FactoryClassHelloWorld1 {
    //工厂类静态方法
    public static HelloWorld getInstance(){
        return new HelloWorld();

    }
}
public class FactoryClassHelloWorld2 {
    //工厂类方法
    public  HelloWorld getInstance(){
        return new HelloWorld();

    }
}
public class Test_mothed {
    /**
     * spring容器在默认的情况下使用默认的构造函数创建对象(默认构造函数可写可不写)
     */
    @Test
    public void testMothed(){
        ApplicationContext context = 
            new ClassPathXmlApplicationContext("applicationContext.xml");
    HelloWorld helloWorld = (HelloWorld) context.getBean("helloWorld");
    helloWorld.printHelloWorld();
    }
    /*
     *  在spring容器 内部,调用了HelloWorldFactory中的getInstance静态方法
     * 而该方法的内容就是创建对象的过程,是由程序员来完成
     * */
    @Test
    public void testFactory1(){
        ApplicationContext context = 
            new ClassPathXmlApplicationContext("applicationContext.xml");
        HelloWorld helloWorld = (HelloWorld) context.getBean("helloWorld1");
        helloWorld.printHelloWorld();
    }

    /**
     * 实例工厂
     *   1、spring容器创建一个实例工厂bean
     *   2、该bean调用了工厂方法getInstance产生对象
     */
    @Test
    public void testFactory2(){
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        HelloWorld helloWorld = (HelloWorld) context.getBean("helloWorld2");
        helloWorld.printHelloWorld();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值