Spring Bean实例化实现过程解析

所属分类: 软件编程 / java 阅读数: 37
收藏 0 赞 0 分享

这篇文章主要介绍了Spring Bean实例化实现过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

Bean的实例化

1.构造器实例化:Spring容器通过Bean对应类中默认的无参构造方法来实例化Bean

package com.itheima.instance.constructor;
 
public class Bean1 {
 
}
<?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="bean1" class="com.itheima.instance.constructor.Bean1"></bean>
</beans>

在beans1.xml文件中,定义了一个id为bean1的Bean,并通过class属性指定其对应的实现类Bean1

package com.itheima.instance.constructor;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class InstanceTest1 {
  public static void main(String[] args) {
    //定义配置文件路径
    String xmlPath = "com/itheima/instance/constructor/beans1.xml";
    //ApplicationContext在加载配置文件时,对Bean进行实例化
    ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
    Bean1 bean = (Bean1) applicationContext.getBean("bean1");
    System.out.println(bean);
  }
}

在InstanceTest1类中,首先定义了配置文件的路径,然后Spring容器ApplicationContext会加载配置文件。在加载时,Spring容器会通过id为bean1的实现类Bean1中默认的无参构造方法对Bean进行实例化。

2. 静态工厂方法实例化

package com.itheima.instance.static_factory;
 
public class Bean2 {
 
}
package com.itheima.instance.static_factory;
 
public class MyBean2Factory {
  //使用自己的方法创建Bean2实例
  public static Bean2 createBean(){
    return new Bean2();
  }
}
<?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="bean2" class="com.itheima.instance.static_factory.MyBean2Factory" factory-method="createBean"></bean>
</beans>

定义id为bean2的Bean,通过class属性指定其对应的工厂实现类(MyBean2Factory.java),需要增加factory-method属性来告诉Spring容器其方法名称为createBean。

package com.itheima.instance.static_factory;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class InstanceTest2 {
  public static void main(String[] args) {
    //定义配置文件路径
    String xmlPath = "com/itheima/instance/static_factory/beans2.xml";
    //ApplicationContext在加载配置文件时,对Bean进行实例化
    ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
    System.out.println(applicationContext.getBean("bean2"));
  }
}

3.实例工厂方式实例化

package com.itheima.instance.factory;
 
public class Bean3 {
 
}
package com.itheima.instance.factory;
 
public class MyBean3Factory {
  public MyBean3Factory(){
    System.out.println("bean3工厂实例化中");
  }
  //创建Bean3实例的方法
  public Bean3 createBean(){
    return new Bean3();
  }
}
<?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="myBean3Factory" class="com.itheima.instance.factory.MyBean3Factory"></bean>
<!-- 使用 factory-bean属性指向配置的实例工厂
   使用factory-method属性确定使用工厂中的哪个方法 -->
   <bean id="bean3" factory-bean="myBean3Factory" factory-method="createBean"></bean>
</beans>

首先配置了一个工厂Bean,然后配置了需要实例化的Bean。在id为bean3的Bean中,使用factory-bean属性指向配置的实例工厂,使用factory-method属性来确定使用工厂中的createBean()方法

package com.itheima.instance.factory;
 
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
 
public class InstanceTest3 {
  public static void main(String[] args) {
    //指定配置文件路径
    String xmlPath = "com/itheima/instance/factory/beans3.xml";
    //ApplicationContext加载配置文件时,对Bean进行实例化
    @SuppressWarnings("resource")
    ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
    System.out.println(applicationContext.getBean("bean3"));
  }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

java 中maven pom.xml文件教程详解

这篇文章主要介绍了java 中maven pom.xml文件教程详解,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
收藏 0 赞 0 分享

spring boot整合netty的实现方法

这篇文章主要介绍了spring boot整合netty的实现方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Netty与Spring Boot的整合实现

这篇文章主要介绍了Netty与Spring Boot的整合的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Spring动态加载bean后调用实现方法解析

这篇文章主要介绍了Spring动态加载bean后调用实现方法解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
收藏 0 赞 0 分享

java实现画图板上画一条直线

这篇文章主要为大家详细介绍了java实现画图板上画一条直线,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Java通过python命令执行DataX任务的实例

今天小编就为大家分享一篇Java通过python命令执行DataX任务的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
收藏 0 赞 0 分享

springBoot集成redis的key,value序列化的相关问题

这篇文章主要介绍了springBoot集成redis的key,value序列化的相关问题,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

java实现登录案例

这篇文章主要为大家详细介绍了java实现登录案例的相关代码,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

java解决请求跨域的两种方法

这篇文章主要为大家详细介绍了java解决请求跨域的两种方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

SpringBoot集成Beetl后统一处理页面异常的方法

这篇文章主要介绍了SpringBoot集成Beetl后统一处理页面异常的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享
查看更多