前言
如果把Spring看作一个大型工厂,那么Spring容器中的Bean就是该工厂的产品。要想使用Spring工厂生产和管理Bean,就需要在配置文件中指明需要哪些Bean,以及需要使用何种方式将这些Bean装配到一起。
叙述
Bean是什么
Spring容器支持两种格式的配置文件,分别为Properties文件格式和xml文件格式,而在实际的开发当中,最常使用的额是xml文件格式,因此在如下的讲解中,我们以xml文件格式的配置方式进行说明。XML配置文件的根元素是,其可以包含多个子元素,每个子元素定义一个Bean,并描述了Bean该如何被装配到Spring容器中。
- id:Bean的唯一标识符,Spring对Bean的配置、管理都通过该属性来完成;
- name:Spring同样可以通过name对Bean进行配置和管理,name属性可以为Bean定义多个名称,每个名称以逗号隔开;
- class:该属性指定了Bean的具体实现类,必须是一个完成的类名,使用类的全限定名;
- scope:设定Bean实例的作用域,其属性有singleton(单例)、prototype(原型)、request、session、和global
- Session,默认值为singleton,该属性会在下一篇博客中详细讲解;
- constructor-arg:元素的子元素,可以使用此元素传入构造参数进行实例化(上一篇博客的最后补充就是使用此属性进行实例化的),该元素的index属性指定构造参数的序号(从0开始);
- property:元素的子元素,通过调用Bean实例中的setter方法完成属性赋值,从而完成依赖注入;
- ref:property、constructor-arg等元素的子元素,该元素中的bean属性用于指定对Bean工厂中某个Bean实例的引用;
- value:property、constructor-arg等元素的子元素,用来直接指定一个常量值;
- list:用于封装List或数组类型的依赖注入; set:用于封装Set或数组类型的依赖注入;
- map:用于封装Map或数组类型的依赖注入;
- entry:map元素的子元素,用于设定一个键值对,其key属性指定字符串类型的键值,ref或value子元素指定其值。
Bean的实例化
Spring IoC容器如何实例化Bean呢?传统应用程序可以通过new和反射方式进行实例化Bean。而Spring IoC容器则需要根据Bean定义里的配置元数据使用反射机制来创建Bean。在Spring IoC容器中根据Bean定义创建Bean主要有以下几种方式.
默认方式
我们之前写的例子就是使用的默认方式,这种方式spring ioc容器会调用bean的无参构造方法来创建对象,所以此时务必要保证这些bean有无参构造方法。
- 使用空构造器进行定义,使用此种方式,class属性指定的类必须有空构造
<!--默认方式,需要保证该bean中有无参的构造方法-->
<bean id="studentService" init-method="init" destroy-method="destory" class="com.monkey1024.service.impl.StudentServiceImpl"/>
- 使用有参数构造器进行定义,使用此中方式,可以使用<
constructor-arg>标签指定构造器参数值,其中index表示位置,value表示常量值,也可以指定引用,指定引用使用ref来引用另一个Bean定义;
实例工厂
这里需要自己定义一个工厂类,在该类中的方法都是非静态的。
/**
* 实例工厂,方法是非静态的
*/
public class MyBeanFactory {
public StudentService createStudentService() {
return new StudentServiceImpl();
}
}
将applicationContext配置文件中修改如下,这里需要告诉spring要使用的工厂类和方法:
<!--实例工厂-->
<bean id="myFactory" class="com.monkey1024.factory.MyBeanFactory"/>
<bean id="studentService" factory-bean="myFactory" factory-method="createStudentService"/>
在测试类中添加测试方法:
/**
* 实例工厂的方式
*/
@Test
public void instanceFactory() {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
StudentService studentService = (StudentService) context.getBean("studentService");
studentService.study();
}
静态工厂
这里需要自定一个工厂类,里面的方法是static修饰的
/**
* 静态工厂,方法都是static修饰
*/
public class MyBeanFactory {
public static StudentService createStudentService() {
return new StudentServiceImpl();
}
}
将applicationContext配置文件中修改如下,这里需要告诉spring要使用的工厂类和方法:
<!--静态工厂-->
<bean id="studentService" class="com.monkey1024.factory.MyBeanFactory" factory-method="createStudentService"/>
创建测试方法:
/**
* 静态工厂的方式
*/
@Test
public void staticFactory() {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
StudentService studentService = (StudentService) context.getBean("studentService");
studentService.study();
}
用 setter 方式
这种方式,只要写上对应的set、get方法,然后再bean.xml文件中利用property注入值即可。
小结
spring的bean的实例化,今天就分享到此了,学习的道路上,一起努力.
学习参考链接:
https://www.cnblogs.com/zhanglei93/p/6221546.html
https://www.jianshu.com/p/646c1f657144
感谢您的阅读~~