ApplicationContext接口中getBean()方法

原文链接:接口ApplicationContext中的getBean()方法的详解_一只不会飞的菜鸟的博客-CSDN博客_getbean

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

1.getBean(String name)

参数name表示IOC容器中已经实例化的bean的id或者name,且无论是id还是name都要求在IOC容器中是唯一的不能重名。那么这种方法就是通过id或name去查找获取bean.获取bean的参考代码如下:

<?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.xsd">

    <bean id="student" class="com.zjy.pojo.Student">
        <!--第一种:普通值注入,value-->
        <property name="name" value="zjy"/>
    </bean>
</beans>
public class MyTest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        Student student = (Student) context.getBean("student");
        System.out.println(student.toString());

    }
}

2.getBean(Class<T> type)

参数Class<T> type表示要加载的Bean的类型。如果该类型没有继承任何父类(Object类除外)和实现接口的话,那么要求该类型的bean在IOC容器中也必须是唯一的。比如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.xsd">

    <bean class="com.zjy.pojo.Student">
        <!--第一种:普通值注入,value-->
        <property name="name" value="zjy"/>
    </bean>

    <bean class="com.zjy.pojo.Student">
        <!--第一种:普通值注入,value-->
        <property name="name" value="jhh"/>
    </bean>
</beans>
public class MyTest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        Student student = context.getBean(Student.class);
        System.out.println(student.toString());

    }
}

        由于属于com.bean.Person的bean在IOC容器中不唯一,所以这里会抛出NoUniqueBeanDefinitionException异常。 

3.getBean(String name,Class<T> type)

这种方式比较适合当类型不唯一时,再通过id或者name来获取bean。

例如applicationContext.xml配置有如下bean:

<?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.xsd">

    <bean name="student1" class="com.zjy.pojo.Student">
        <!--第一种:普通值注入,value-->
        <property name="name" value="zjy"/>
    </bean>

    <bean name="student2" class="com.zjy.pojo.Student">
        <!--第一种:普通值注入,value-->
        <property name="name" value="jhh"/>
    </bean>
</beans>
public class MyTest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        Student student = context.getBean("student1",Student.class);
        System.out.println(student.toString());

    }
}

4.getBean(String name,Object[] args)

这种方式本质还是通过bean的id或者name来获取bean,通过第二个参数Object[] args可以给bean的属性赋值,赋值的方式有两种:构造方法和工厂方法。但是通过这种方式获取的bean必须把scope属性设置为prototype,也就是非单例模式。

先在com.factory包下设计有如下的工厂类:

public class StudentFactory {
//静态工厂注入
    public static Student getPersonInstance(String name)throws Exception
    {
        Student student = (Student)Class.forName("com.bean.Student").newInstance();
        Method m = p.getClass().getMethod("setName", java.lang.String.class);
        m.invoke(p, name);
        
        return student;
    }
}

 在applicationContext.xml中配置有如下bean:

<bean name="student" class="com.bean.student" scope="prototype"/>

 获取bean的参考代码如下:

public class MyTest {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        Student student = (Student)context.getBean("student",new Object[]{"zjy"});
        System.out.println(student.toString());

    }
}

如果想通过工厂注入属性,在applicationContext.xml配置如下bean:

<bean name="student" class="com.factory.StudentFactory" factory-method="getStudentInstance" scope="prototype">
    <constructor-arg name="name">
        <null/>
    </constructor-arg>
</bean>

总结:getBean(String name)和getBean(Class<T> type)的异同点。

  • 相同点:都要求id或者name或者类型在容器中的唯一性。

  • 不同点:getBean(String name)获得的对象需要类型转换而getBean(Class<T> type)获得的对象无需类型转换。

  • 5
    点赞
  • 39
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值