详解Spring框架---IOC装配Bean
ioc装配bean
(1)spring框架bean实例化的方式提供了三种方式实例化bean
- 构造方法实例化(默认无参数,用的最多)
- 静态工厂实例化
- 实例工厂实例化
下面先写这三种方法的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" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemalocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- bean的三种实例化方式=================== --> <!-- 2.1 使用无参的构造器 --> <bean id="bean1" class="com.study.spring.b_instance.bean1"></bean> <!-- 2.2使用静态工厂方法 factory-method 是工厂提供的静态方法 --> <bean id="bean2" class="com.study.spring.b_instance.bean2" factory-method="createinstance"></bean> <!-- 2.3配置实例化工厂的方法 --> <bean id="bean3factory" class="com.study.spring.b_instance.bean3factory"></bean> <bean id="bean3" factory-bean="bean3factory" factory-method="getinstance"></bean> <!-- end.bean的三种实例化方式==================== -->
bean1类
public class bean1 { //必须提供无参的构造函数 系统有默认无参的构造函数 }
bean2类
public class bean2 { private static bean2 bean2 = new bean2(); private bean2() { } public static bean2 createinstance() { return bean2; } }
bean3类
public class bean3 { }
bean3factory类
public class bean3factory { private bean3factory(){ } public bean3 getinstance(){ return new bean3(); } }
测试类instancedemo
import org.junit.test; import org.springframework.context.applicationcontext; import org.springframework.context.support.classpathxmlapplicationcontext; public class instancedemo { //实例化工厂方法 @test public void demo3(){ //加载配置文件 创建工厂 applicationcontext applicationcontext=new classpathxmlapplicationcontext("applicationcontext.xml"); bean3 bean3 =(bean3) applicationcontext.getbean("bean3"); system.out.println(bean3); } //静态工厂方法 @test public void demo2(){ //加载配置文件 创建工厂 applicationcontext applicationcontext=new classpathxmlapplicationcontext("applicationcontext.xml"); bean2 bean2 =(bean2) applicationcontext.getbean("bean2"); system.out.println(bean2); } //构造方法得到bean对象 @test public void demo1(){ //加载配置文件 创建工厂 applicationcontext applicationcontext=new classpathxmlapplicationcontext("applicationcontext.xml"); bean1 bean1 =(bean1) applicationcontext.getbean("bean1"); system.out.println(bean1); } } /* * 这三个都得到类似于com.study.spring.b_instance.bean1@7229c204 的内存地址 */
(2).bean的其他配置:
一般情况下,装配一个bean时,通过指定一个id属性作为bean的名称
id 属性在ioc容器中必须是唯一的
id 的命名要满足xml对id属性命名规范 必须以字母开始,可以使用字母、数字、连字符、下划线、句话、冒号
如果bean的名称中含有特殊字符,就需要使用name属性 例如: <bean name="#person" class="cn.itcast.bean.person"/>
因为name属性可以相同,所以后出现bean会覆盖之前出现的同名的bean
id和name的区别:
id遵守xml约束的id的约束.id约束保证这个属性的值是唯一的,而且必须以字母开始,可以使用字母、数字、连字符、下划线、句话、冒号
name没有这些要求
如果bean标签上没有配置id,那么name可以作为id.
bean的scope属性
<!-- 3.bean的scope属性==================== --> <bean id="product" class="com.study.spring.c_scope.product" scope="singleton"></bean> <!-- end.bean的scope属性=========== -->
* singleton :单例的.(默认的值.)
* prototype :多例的.
* request :web开发中.创建了一个对象,将这个对象存入request范围,request.setattribute();
* session :web开发中.创建了一个对象,将这个对象存入session范围,session.setattribute();
* globalsession :一般用于porlet应用环境.指的是分布式开发.不是porlet环境,globalsession等同于session;
3.bean属性的依赖注入
前面已经知道如何获得对象,那我们接下来要知道如果给对象对象的属性赋值。
下面通过举例说明:
car 类
public class car { private string name; private double price; public car(string name, double price) { super(); this.name = name; this.price = price; } @override public string tostring() { return "car [name=" + name + ", price=" + price + "]"; } }
car2类
public class car2 { private string name; private double price; public void setname(string name) { this.name = name; } public void setprice(double price) { this.price = price; } @override public string tostring() { return "car2 [name=" + name + ", price=" + price + "]"; } }
carinfo类
public class carinfo { public string getname(){ return "哈弗h6"; } public double caculateprice(){ return 110000; } }
collectionbean类
import java.util.list; import java.util.map; import java.util.properties; import java.util.set; public class collectionbean { private string name; private integer age; private list<string> hobbies; private set<integer> numbers; private map<string, string> map; private properties properties; 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 list<string> gethobbies() { return hobbies; } public void sethobbies(list<string> hobbies) { this.hobbies = hobbies; } public set<integer> getnumbers() { return numbers; } public void setnumbers(set<integer> numbers) { this.numbers = numbers; } public map<string, string> getmap() { return map; } public void setmap(map<string, string> map) { this.map = map; } public properties getproperties() { return properties; } public void setproperties(properties properties) { this.properties = properties; } @override public string tostring() { return "collectionbean [name=" + name + ", age=" + age + ", hobbies=" + hobbies + ", numbers=" + numbers + ", map=" + map + ", properties=" + properties + "]"; } }
employee类
public class employee { private string name; private car2 car2; public void setname(string name) { this.name = name; } public void setcar2(car2 car2) { this.car2 = car2; } @override public string tostring() { return "employee [name=" + name + ", car2=" + car2 + "]"; } }
testdi测试类
import org.junit.test; import org.springframework.context.applicationcontext; import org.springframework.context.support.classpathxmlapplicationcontext; public class testdi { @test public void demo6() { applicationcontext applicationcontext = new classpathxmlapplicationcontext("applicationcontext.xml"); collectionbean collectionbean = (collectionbean) applicationcontext.getbean("collectionbean"); system.out.println(collectionbean); } @test public void demo5() { applicationcontext applicationcontext = new classpathxmlapplicationcontext("applicationcontext.xml"); car2 car2 = (car2) applicationcontext.getbean("car2_2"); system.out.println(car2); } @test public void demo4() { applicationcontext applicationcontext = new classpathxmlapplicationcontext("applicationcontext.xml"); employee e = (employee) applicationcontext.getbean("employee2"); system.out.println(e); } @test public void demo3() { applicationcontext applicationcontext = new classpathxmlapplicationcontext("applicationcontext.xml"); employee e = (employee) applicationcontext.getbean("employee"); system.out.println(e); } @test public void demo2() { applicationcontext applicationcontext = new classpathxmlapplicationcontext("applicationcontext.xml"); car2 car2 = (car2) applicationcontext.getbean("car2"); system.out.println(car2); } @test public void demo1() { applicationcontext applicationcontext = new classpathxmlapplicationcontext("applicationcontext.xml"); car car = (car) applicationcontext.getbean("car"); system.out.println(car); } }
上面这几个类都不是最主要的,我们主要是来看配置文件怎么写,这才是最关键的:
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" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemalocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- bean的依赖注入=========== --> <!-- 4.1构造器注入 --> <bean id="car" class="com.study.spring.e_di.car"> <!-- 方式一.根据索引的位置 --> <!-- <constructor-arg index="0" value="保时捷"></constructor-arg> <constructor-arg index="1" value="1500000"></constructor-arg> --> <!-- 方式二.根据名字配置 --> <!-- <constructor-arg name="name" value="宝马"></constructor-arg> <constructor-arg name="price" value="500000"></constructor-arg> --> <!-- 方式三.根据类型配置 --> <constructor-arg type="java.lang.string" value="奔驰"></constructor-arg> <constructor-arg type="double" value="600000"></constructor-arg> </bean> <!-- 4.2setter方法中注入 --> <bean id="car2" class="com.study.spring.e_di.car2"> <property name="name" value="雪佛兰"></property> <property name="price" value="100000"></property> </bean> <bean id="employee" class="com.study.spring.e_di.employee"> <property name="name" value="张三"></property> <property name="car2" ref="car2"></property> </bean> <!-- 引用p命名空间 --><!-- 如果要引用p命名,那在最上面sxd中就要配置 xmlns:p="http://www.springframework.org/schema/p"--> <bean id="car22" class="com.study.spring.e_di.car2" p:name="宝马" p:price="500000"> </bean> <bean id="employee2" class="com.study.spring.e_di.employee" p:name="李四" p:car2-ref="car22"></bean> <!-- 引入spel表达式 --> <bean id="carinfo" class="com.study.spring.e_di.carinfo"></bean> <bean id="car2_2" class="com.study.spring.e_di.car2"> <property name="name" value="#{carinfo.name}"></property> <property name="price" value="#{carinfo.caculateprice()}"></property> </bean> <!-- 复杂属性的依赖注入 --> <bean id="collectionbean" class="com.study.spring.e_di.collectionbean"> <!-- 简单属性的注入 --> <property name="name" value="归谷"></property> <property name="age" value="12"></property> <!-- 注入list集合 --> <property name="hobbies"> <list> <value>吃饭</value> <value>睡觉</value> <value>敲代码</value> </list> </property> <!-- 注入set集合 --> <property name="numbers"> <set> <value>10</value> <value>20</value> <value>30</value> <value>40</value> <value>50</value> </set> </property> <!-- 注入map集合 --> <property name="map"> <map> <entry key="birthday" value="2017-1-1"></entry> <entry key="address" value="杭州西湖"></entry> <entry key="sex" value="female"></entry> </map> </property> <!-- 注入properties --> <property name="properties"> <props> <prop key="compamy">杭州归谷</prop> <prop key="pnum">200</prop> </props> </property> </bean> <!-- end bean的依赖注入============ --> <import resource="classpath:bean1.xml"/> <import resource="classpath:bean2.xml"/> <!-- 这里导入是指如果在src下还有其它的beans.xml我们可以这样去调用 --> </beans>
有关applicationcontext.xml这个配置文件里的内容一定要看懂,我写的还是比较基础和全面的。
有关命名空间p的使用我这里在解释下:
p:<属性名>="xxx" 引入常量值
p:<属性名>-ref="xxx" 引用其它bean对象
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。