1、Bean元素
Bean元素:使用该元素描述需要spring容器管理的对象
class属性:被管理对象的完整类名
name属性:给被管理对象起个名字。获得对象时根据该名称获得对象,可以重复,可以使用特殊字符
id属性:与name属性一模一样,但是名称不可以重复,不可以使用特殊字符
结论:尽量使用name属性
<bean id="" name="user" class="com.hciot.bean.User">
2、Bean元素进阶
a、scope属性
singleton(默认值):单例对象.被标识为单例的对象在spring容器中只会存在一个实例。
扫描二维码关注公众号,回复: 163966 查看本文章prototype:多例原型.被标识为多例的对象,每次再获得才会创建.每次创建都是新的对象.整合struts2时,ActionBean必须配置为多例的。
b、生命周期属性
init-method:配置一个方法作为生命周期初始化方法.spring会在对象创建之后立即调用.
destory-method:配置一个方法作为生命周期的销毁方法.spring容器在关闭并销毁所有容器中的对象之前调用.
<bean name="user" class="com.hciot.bean.User" init-method="init" destroy-method="destroy"></bean>
3、spring创建对象的方式
a.空参构造方式
<bean name="user" class="com.hciot.bean.User">b.静态工厂(了解)
c.实例工厂(了解)
4、Spring属性注入
4.1 set方法注入
<!-- set方式注入: -->
<bean name="user" class="com.hciot.bean.User">
<property name="name" value="tom"></property>
<property name="age" value="18"></property>
<property name="car" ref="car"></property>
</bean>
输出结果
user对象空参构造方法 User(String name, Car car) User [name=tom, age=18, car=Car [name=兰博基尼, color=黄色 ]]
4.2构造函数注入
<!-- 构造函数注入: -->
<!-- name:构造函数参数名 -->
<!-- index:构造函数参数索引 -->
<bean name="user2" class="com.hciot.bean.User">
<constructor-arg name="name" value="hhp" index="0"></constructor-arg>
<constructor-arg name="car" ref="car" index="1"></constructor-arg>
</bean>
输出结果
user对象空参构造方法 User(String name, Car car) User [name=hhp, age=null, car=Car [name=兰博基尼, color=黄色 ]]
4.3 复杂类型注入
4.3.1 数组
<!-- array注入: --> <!-- 如果数组中只准备注入一个对象(值),直接使用value|ref即可: <property name="arr" value="tom"></property> --> <!-- array注入,多个元素注入: --> <property name="arr"> <array> <value>tom</value> <value>hhp</value> <ref bean="user2" /> </array> </property>
4.3.2 List
<!-- list注入: --> <!-- 如果List中只准备注入一个对象(值),直接使用value|ref即可: <property name="list" value="tom"></property> --> <!-- list注入,多个元素注入: --> <property name="list"> <list> <value>tom</value> <value>hhp</value> <ref bean="user2" /> </list> </property>
4.3.3 Map
<!-- Map注入 --> <property name="map"> <map> <entry key="url" value="jdbc:mysql:///crm"></entry> <entry key="user" value-ref="user2"></entry> <entry key-ref="user" value-ref="user2"></entry> </map> </property>
4.3.4 Properties
<!-- properties注入 --> <property name="prop"> <props> <prop key="driverClass">com.jdbc.mysql.Driver</prop> <prop key="username">root</prop> <prop key="password">root</prop> </props> </property>附录
测试代码
package com.hciot.c_injection; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.hciot.bean.User; public class Demo { @Test public void fun1(){ //1.创建容器对象 ApplicationContext ac = new ClassPathXmlApplicationContext("com/hciot/c_injection/applicationContext.xml"); //2.向容器要user对象 User u = (User) ac.getBean("user"); //3.打印user对象 System.out.println(u); } @Test public void fun2(){ //1.创建容器对象 ApplicationContext ac = new ClassPathXmlApplicationContext("com/hciot/c_injection/applicationContext.xml"); //2.向容器要user对象 User u = (User) ac.getBean("user2"); //3.打印user对象 System.out.println(u); } @Test public void fun3(){ //1.创建容器对象 ApplicationContext ac = new ClassPathXmlApplicationContext("com/hciot/c_injection/applicationContext.xml"); //2.向容器要user对象 Collection cb=(Collection) ac.getBean("cb"); //3.打印user对象 System.out.println(cb); } }User对象
package com.hciot.bean; public class User { private String name; private Integer age; private Car car; public User() { System.out.println("user对象空参构造方法"); } public User(String name, Car car) { System.out.println("User(String name, Car car)"); this.name = name; this.car = car; } public User(Car car,String name) { System.out.println("User(String name, Car car)"); this.name = name; this.car = car; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Car getCar() { return car; } public void setCar(Car car) { this.car = car; } public void init(){ System.out.println("初始化方法"); } public void destroy(){ System.out.println("销毁方法"); } @Override public String toString() { return "User [name=" + name + ", age=" + age + ", car=" + car + "]"; } }Car对象
package com.hciot.bean; public class Car { private String name; private String color; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getColor() { return color; } public void setColor(String color) { this.color = color; } @Override public String toString() { return "Car [name=" + name + ", color=" + color + "]"; } }配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd "> <!-- set方式注入: --> <bean name="user" class="com.hciot.bean.User"> <property name="name" value="tom"></property> <property name="age" value="18"></property> <property name="car" ref="car"></property> </bean> <!-- 将car对象配置到容器中 --> <bean name="car" class="com.hciot.bean.Car"> <property name="name" value="兰博基尼"></property> <property name="color" value="黄色 "></property> </bean> <!-- ====================================================================== --> <!-- 构造函数注入: --> <!-- name:构造函数参数名 --> <!-- index:构造函数参数索引 --> <bean name="user2" class="com.hciot.bean.User"> <constructor-arg name="name" value="hhp" index="0"></constructor-arg> <constructor-arg name="car" ref="car" index="1"></constructor-arg> </bean> <!-- ====================================================================== --> <!-- 复杂类型注入: --> <bean name="cb" class="com.hciot.c_injection.Collection"> <!-- ====================================================================== --> <!-- array注入: --> <!-- 如果数组中只准备注入一个对象(值),直接使用value|ref即可: <property name="arr" value="tom"></property> --> <!-- array注入,多个元素注入: --> <property name="arr"> <array> <value>tom</value> <value>hhp</value> <ref bean="user2" /> </array> </property> <!-- ====================================================================== --> <!-- list注入: --> <!-- 如果数组中只准备注入一个对象(值),直接使用value|ref即可: <property name="list" value="tom"></property> --> <!-- list注入,多个元素注入: --> <property name="list"> <list> <value>tom</value> <value>hhp</value> <ref bean="user2" /> </list> </property> <!-- ====================================================================== --> <!-- Map注入 --> <property name="map"> <map> <entry key="url" value="jdbc:mysql:///crm"></entry> <entry key="user" value-ref="user2"></entry> <entry key-ref="user" value-ref="user2"></entry> </map> </property> <!-- ====================================================================== --> <!-- properties注入 --> <property name="prop"> <props> <prop key="driverClass">com.jdbc.mysql.Driver</prop> <prop key="username">root</prop> <prop key="password">root</prop> </props> </property> </bean> </beans>