标签:
Spring创建对象的几种方式
一 :默认构造方法创建对象
1.applicationContext.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
           
    <bean id="helloWorld" class="com.spring.createobject.method.HelloWord"></bean>
    
</beans>
2.测试类
package com.spring.createobject.method;
public class HelloWord {
    
    public HelloWord(){
        System.out.println("create object");
    }
    
    public void hello(){
        System.out.println("hello word!");
    }
}
3,测试
    /**
     * 在Spring容器中,默认情况下调用了一个类的默认构造函数创建对象
     */
    @Test
    public void testCreateObject_Default(){
        
        //启动Spring容器
        ApplicationContext context=
                new ClassPathXmlApplicationContext("applicationContext.xml");
        
        //根据id把Spring容器中的bean提取出来
        HelloWord helloWord = (HelloWord) context.getBean("helloWorld");
        
        helloWord.hello();
        
        
    }
输出:
create object
hello word!
二:静态工厂创建对象
1.applicationContext.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
    <!--  factory-method 指的是静态工厂方法-->
    <bean id="helloWord2" class="com.spring.createobject.method.factory.HelloWordFactory"
        factory-method="getInstance"></bean>
        
</beans>
2.测试类
package com.spring.createobject.method;
public class HelloWord {
    
    public HelloWord(){
        System.out.println("create object");
    }
    
    public void hello(){
        System.out.println("hello word!");
    }
}
package com.spring.createobject.method.factory;
import com.spring.createobject.method.HelloWord;
//静态工厂方法
public class HelloWordFactory {
    
    public static HelloWord getInstance(){
        
        return new HelloWord();
    }
}
3.测试
/**
     * 
     *  利用静态工厂模式创建对象 
     *    <bean id="helloWord2" class="com.spring.createobject.method.factory.HelloWordFactory"
        factory-method="getInstance"></bean>
        Spring容器内部调用了HelloWordFactory中的getInstance方法创建对象
        而具体的new对象的过程由程序员完成的。
     */
    @Test
    public void testCreateObject_StaticFactory(){
        
        //启动Spring容器
        ApplicationContext context=
                new ClassPathXmlApplicationContext("applicationContext.xml");
        
        //根据id把Spring容器中的bean提取出来
        HelloWord helloWord = (HelloWord) context.getBean("helloWord2");
        helloWord.hello();
    }
输出:
create object
hello word!
三:实例工厂创建对象
1.applicationContext.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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
          
        
    <bean id="hellowordFactory" 
    class="com.spring.createobject.method.factory.HelloWordFactory2"></bean>
    
    <!-- factory-bean 是一个工厂bean,会调用hellowordFactory所指向的对象,再调用getInstance创建对象
         factory-method 是一个工厂方法
     -->
    
    <bean id="helloword3" 
        factory-bean="hellowordFactory"
        factory-method="getInstance"></bean>
    
    
        
</beans>
2.测试类
package com.spring.createobject.method;
public class HelloWord {
    
    public HelloWord(){
        System.out.println("create object");
    }
    
    public void hello(){
        System.out.println("hello word!");
    }
}
package com.spring.createobject.method.factory;
import com.spring.createobject.method.HelloWord;
public class HelloWordFactory2 {
    
    public  HelloWord getInstance(){
        
        return new HelloWord();
    }
}
3.测试
/**
     * 实例工厂方法
     */
    @Test
    public void testCreateObject_instancecFactory(){
        
        //启动Spring容器
        ApplicationContext context=
                new ClassPathXmlApplicationContext("applicationContext.xml");
        
        //根据id把Spring容器中的bean提取出来
        HelloWord helloWord = (HelloWord) context.getBean("helloword3");
        helloWord.hello();
    }
输出:
create object
hello word!
标签:
原文地址:http://www.cnblogs.com/thinkpad/p/4928274.html