Spring Bean实例化实现过程解析
这篇文章主要介绍了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代码
下一篇:Spring Security如何使用URL地址进行权限控制
本文标题:Spring Bean实例化实现过程解析
本文地址:http://www.codeinn.net/misctech/28622.html


阅读排行
- 1Java Swing组件BoxLayout布局用法示例
- 2java中-jar 与nohup的对比
- 3Java邮件发送程序(可以同时发给多个地址、可以带附件)
- 4Caused by: java.lang.ClassNotFoundException: org.objectweb.asm.Type异常
- 5Java中自定义异常详解及实例代码
- 6深入理解Java中的克隆
- 7java读取excel文件的两种方法
- 8解析SpringSecurity+JWT认证流程实现
- 9spring boot里增加表单验证hibernate-validator并在freemarker模板里显示错误信息(推荐)
- 10深入解析java虚拟机




