一、Bean的配置
元素 | 描述 |
---|---|
id | id是<bean>元素的唯一标识,Spring容器对Bean的配置和管理通过id属性完成,装配Bean时也需要根据id值获取对象 |
name | name属性可以为Bean指定多个名称,每个名称之间用逗号间隔 |
class | class属性可以指定Bean的具体实现类,其属性为对象所属类的全路径 |
scope | scope属性用于设定Bean实例的作用范围,其属性有singleton(单例)、prototype(原型)、request、session、和global session |
元素 | 描述 |
---|---|
<constructor-arg> | 使用<constructor-arg>元素可以为Bean的属性指定值 |
<property> | <property>元素的作用是调用Bean实例中的setter方法完成属性赋值,从而完成依赖注入 |
ref | ref是<constructor-arg>、<property>的属性,用于指定Bean工厂中某个Bean实例的引用 |
value | value是<property>、<constructor-arg>等元素的属性,用于直接指定一个常量值 |
<list> | <list>是<property>的子元素,用于指定Bean属性类型为List或数组 |
<set> | <set>是<property>的子元素,用于指定Bean属性类型为set |
<map> | <map>是<property>的子元素,用于指定Bean属性类型为map |
<entry> | <entry>是<map>的子元素,用于设定一个键值对 |
最常用的方式就是使用id和name来定义不同的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">
<!-- 使用id定义bean-->
<bean id="HellSpring" class="org.moyu.HellSpring">
<!-- 为名字进行赋值-->
<property name="userName" value="小黄"></property>
</bean>
<!-- 使用name属性定义bean-->
<bean name="Student" class="org.moyu.Student">
<property name="name" value="小张"></property>
<property name="age" value="18"></property>
<property name="sex" value="男"></property>
</bean>
</beans>
二、Bean的实例化
Bean实例化的作用是为了在Spring容器中创建一个具体的对象实例,该实例可以被其他组件或类注入、调用或使用。Bean的实例化常用的有三种,分别是构造方法实例化、静态工厂实例化、实例工厂实例化
1.构造方法实例化
构造方法实例化是Spring容器通过Bean对应的类中默认的无参构造方法来实例化Bean
例子
在java包下创建一个Bean1的类
package org.moyu.BeanFactory;
public class Bean1 {
public Bean1(){
System.out.println("这个是Bean1");
}
}
在resources包下创建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构造方法实例化-->
<bean id="bean1" class="org.moyu.BeanFactory.Bean1"></bean>
</beans>
构造方法启动类配置
package org.moyu.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* 该程序为Spring的测试启动类
*/
public class AppTest
{
public static void main( String[] args )
{
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
// 获取构造方法实例化Bean
System.out.println("构造方法实例化:" + applicationContext.getBean("bean1"));
}
}
2.静态工厂实例化
用静态工厂类中的方法创建Bean实例此时Bean配置文件中的class属性不再是Bean实例的实现类而是静态工厂类。
例子
在java包下创建一个fruit的实体类
package org.moyu.BeanFactory;
public class fruit {
private String name;
private double price;
@Override
public String toString() {
return "fruit{" +
"name='" + name + '\'' +
", price=" + price +
'}';
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
}
在java包下创建一个Bean2当静态工厂类
package org.moyu.BeanFactory;
public class Bean2 {
public static fruit getPrice(String name){
System.out.println("静态工厂获取水果价格...");
fruit f = new fruit();
f.setName("苹果");
f.setPrice(100);
return f;
}
}
在resources包下创建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静态工厂-->
<bean id="bean2" class="org.moyu.BeanFactory.Bean2" factory-method="getPrice">
<constructor-arg value="苹果"/>
</bean>
</beans>
3.实例方法实例化
实例工厂的实例化Bean就是直接创建Bean实例,不再使用class属性指向Bean实例所示的类,而是通过factory-bean属性指向Bean配置的实例工厂,并且使用factory-method属性指定要调用的实例工厂中的方法。
在java包下创建Bean3类
package org.moyu.BeanFactory;
public class Bean3 {
public Bean3(){
System.out.println("这个是实例工厂实例化");
}
}
在java包下创建MyBean3Factory类
package org.moyu.BeanFactory;
public class MyBean3Factory {
public MyBean3Factory(){
System.out.println("Bean3实例工厂实例化中");
}
// 创建bean3实例的方法
public Bean3 createBean(){
return new Bean3();
}
}
在resources包下创建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实例工厂实例化-->
<bean id="myBean3Factory" class="org.moyu.BeanFactory.MyBean3Factory">
</bean>
<!-- factory-bean 指向配置的实力工厂-->
<!-- factory-method 确定使用工厂中的哪个方法-->
<bean id="bean3" factory-bean="myBean3Factory" factory-method="createBean">
</bean>
</beans>
三、Bean的作用域
Bean常用作用域有这两个一个是singleton(单例)和prototype(原型)
1.singleton(单例)
单例模式是Spring容器默认的作用域,设置为该属性时,Spring只为Bean创建一个实例,该实例可以重复进行使用。Spring管理着Bean从创建、初始化、销毁过程的生命周期。该属性最大的优点是可以反复使用不会造成反复创建Bean而造成的资源浪费
例子
在java包下创建一个构造方法Bean1类(和上面构造方法实例化的bean1相同)
在resources包下创建applicationContext.xml
使用scope来设置作用域形式,默认为单例
<?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构造方法实例化-->
<bean id="bean1" class="org.moyu.BeanFactory.Bean1" scope="singleton"></bean>
</beans>
启动类
package org.moyu.BeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* 该程序为Spring的测试启动类
*/
public class AppTest
{
public static void main( String[] args )
{
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
System.out.println(applicationContext.getBean("bean1"));
System.out.println(applicationContext.getBean("bean1"));
}
}
运行结果
2.prototype(原型)
原型模式作为作用域时,每次对Bean进行请求都会创建一个新的Bean实例,Spring只负责创建不再管理其生命周期。
例子
原型的作用域只需要把上面单例的属性修改成prototype即可
把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构造方法实例化-->
<bean id="bean1" class="org.moyu.BeanFactory.Bean1" scope="prototype"></bean>
</beans>
运行结果
四、Bean的装配方式
Bean常用的装配方式有两种一个是setter注入还有一个是构造方法注入
1.setter方法注入
这种是使用的setter方法注入
2.构造方法注入
这种是属于构造方法注入
五、Bean的生命周期
Bean的生命周期指的是Bean实例从创建、初始化和销毁的过程。在bean中单例和原型对于Bean生命的周期管理是不同的,单例的作用域管理着bean的生命周期的过程,但是原型的生命周期是不管bean的生命周期的创建。