← 返回首页
Spring基础教程(十一)
发表时间:2020-05-14 16:07:49
讲解getBean四种用法

我们知道可以通过 ApplicationContext的getBean方法来获取Spring容器中已初始化的bean。getBean一共有以下四种方法原型:

方法 说明
getBean(String name) 参数name表示IOC容器中已经实例化的bean的id或者name,且无论是id还是name都要求在IOC容器中是唯一的不能重名。那么这种方法就是通过id或name去查找获取bean。获得的对象需要类型转换。
getBean(Class type) 参数Class type表示要加载的Bean的类型。如果该类型没有继承任何父类(Object类除外)和实现接口的话,那么要求该类型的bean在IOC容器中也必须是唯一的。获得的对象不需要类型转换。
getBean(String name,Class type) 这种方式比较适合当类型不唯一时,再通过id或者name来获取bean,获得的对象不需要类型转换。
getBean(String name,Object[] args) 这种方式本质还是通过bean的id或者name来获取bean,通过第二个参数Object[] args可以给bean的属性赋值,赋值的方式有两种:构造方法和工厂方法。但是通过这种方式获取的bean必须把scope属性设置为prototype,也就是非单例模式。

实例 Person实体类如下:

package com.entity;

public class Person {
    private String name;
    private String gender;
    private int age;

    public Person() {
    }

    public Person(String name, String gender, int age) {
        this.name = name;
        this.gender = gender;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", gender='" + gender + '\'' +
                ", age=" + age +
                '}';
    }
}

在SpringConfiguration配置有一个Person类型的bean.代码如下:

package com.config;


import com.entity.Person;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

@Configuration
@ComponentScan(basePackages = "com")
public class SpringConfiguration {


    @Bean(name = {"p1","person"})
    @Scope
    public Person getPerson(){
        return new Person("zhangsan","男",20);
    }

}

在测试类中使用getBean(String name)和getBean(Class type)来获取Person类型bean对象。

package com.test;

import com.config.SpringConfiguration;
import com.entity.Person;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringDemo {
    public static void main(String[] args) {
        //ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringConfiguration.class);
        Person person1 = (Person) context.getBean("p1"); //必须类型转换
        Person person2 = context.getBean(Person.class); //无需类型转换
        System.out.println(person1);
        System.out.println(person1==person2);
    }
}

运行结果:
Person{name='zhangsan', gender='男', age=20}
true

如果在SpringConfiguration配置有多个Person类型的bean,则不能使用getBean(Class type)来获取Person类型bean对象,因为类型不唯一。可以使用getBean(String name)或者getBean(String name,Class type)。

例如: 在SpringConfiguration配置有多个Person类型的bean.

package com.config;

import com.entity.Person;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

@Configuration
@ComponentScan(basePackages = "com")
public class SpringConfiguration {


    @Bean(name = {"p1","person"})
    @Scope
    public Person getPerson(){
        return new Person("zhangsan","男",20);
    }

    @Bean(name = {"p2"})
    @Scope
    public Person getPerson2(){return new Person("lisi","女",18);}

}

在测试类中使用getBean(String name)和getBean(String name,Class type)来获取Person类型bean对象。

package com.test;

import com.config.SpringConfiguration;
import com.entity.Person;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringDemo {
    public static void main(String[] args) {
        //ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringConfiguration.class);
        Person person1 = (Person) context.getBean("p1");
        Person person2 = context.getBean("p2",Person.class);

        System.out.println(person1);
        System.out.println(person2);
        System.out.println(person1==person2);
    }
}

运行结果:
Person{name='zhangsan', gender='男', age=20}
Person{name='lisi', gender='女', age=18}
false

第四种用法getBean(String name,Object[] args)必须结合@Component注解,Person实体类定义如下:

package com.entity;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Component("p1")
@Scope("prototype")
public class Person {
    private String name;
    private String gender;
    private int age;

    public Person() {
    }

    public Person(String name, String gender, int age) {
        this.name = name;
        this.gender = gender;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Person{" +
                "name='" + name + '\'' +
                ", gender='" + gender + '\'' +
                ", age=" + age +
                '}';
    }
}

在SpringConfiguration无需配置Person类型的bean.

在测试类中使用getBean(String name,Object[] args)获取bean.

package com.test;

import com.config.SpringConfiguration;
import com.entity.Person;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringDemo {
    public static void main(String[] args) {
        //ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(SpringConfiguration.class);
        Person person1 = (Person) context.getBean("p1");
        Person person2 = context.getBean(Person.class,new Object[]{"wangwu","男",22});

        System.out.println(person1);
        System.out.println(person2);
        System.out.println(person1==person2);
    }
}

运行结果:
Person{name='null', gender='null', age=0}
Person{name='wangwu', gender='男', age=22}
false