Spring对象的三种创建方式

  • 空参构造方式

实体类:

       

 1 package entity;
 2 
 3 public class User {
 4     private String name;
 5     private Integer age;
 6 
 7     @Override
 8     public String toString() {
 9         return "User{" +
10                 "name='" + name + '\'' +
11                 ", age=" + age +
12                 '}';
13     }
14 
15     public String getName() {
16         return name;
17     }
18 
19     public void setName(String name) {
20         this.name = name;
21     }
22 
23     public Integer getAge() {
24         return age;
25     }
26 
27     public void setAge(Integer age) {
28         this.age = age;
29     }
30 }

 

测试类:

public class DemoTest {
    @org.junit.Test
    public void fun1() throws Exception {

        //1、创建容器对象,从类路径下去加载xml的配置文件
        ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
        //想容器要对象
        User u=(User) ac.getBean("user");
        System.out.println(u);
    }

}

 applicationContext.xml配置文件

1 <?xml version= "1.0" encoding ="UTF-8" ?>
2 <beans xmlns="http://www.springframework.org/schema/beans"
3              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4              xsi:schemaLocation="http://www.springframework.org/schema/beans
5                 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
6              ">
7     <bean name="user" class="entity.User"></bean>
8 </beans>
  • 静态工厂方式

        1、 添加一个静态工厂类,用于手动创建对象

package test.test;

import entity.User;

public class UserFactory {
    //自己手动在下面的方法里进行User对象的创建,然后交给spring来管理
    public static User createUser(){
        System.out.println("静态工厂方式创建对象");
        return new User();
    }
}

    2、配置文件中bean元素的配置方式

意思为调用UserFactory的createUser方法创建名为user2的对象。

 

测试类中测试方法:

@org.junit.Test
    public void fun2() throws Exception {

        //1、创建容器对象,从类路径下去加载xml的配置文件
        ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
        //想容器要对象
        User u=(User) ac.getBean("user2");
        System.out.println(u);
    }

  输出为:  

 

  • 实例工厂方式创建对象

      工厂类:实例工厂方式与静态工厂方式的不同之处在于:创建对象的方法不是static方法,所以需要先创建出工厂实例类再使用该实例类创建所需对象。

 

 

 

 

 1 package test.test;
 2 
 3 import entity.User;
 4 
 5 public class UserFactory {
 6     //自己手动在下面的方法里进行User对象的创建,然后交给spring来管理
 7     
 8     //实例方式创建对象
 9     public User createUser3(){
10         System.out.println("实例工厂方式创建对象");
11         return new User();
12     }
13 }

 配置文件:首先需要对工厂实例类进行配置。

  <bean name="UserFactory"
           class="test.test.UserFactory"></bean>
 <bean name="user3"
           factory-bean="UserFactory"
           factory-method="createUser3"></bean>

通过UserFactory类创建实例对象userFactory

通过实例对象userFactory的createUser3方法创建名为user3的对象。

 

测试方法

1  @org.junit.Test
2     public void fun3() throws Exception {
3 
4         //1、创建容器对象,从类路径下去加载xml的配置文件
5         ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
6         //想容器要对象
7         User u=(User) ac.getBean("user3");
8         System.out.println(u);
9 
posted @ 2017-12-04 14:10  Garcia11  阅读(282)  评论(0)    收藏  举报