<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
">
<!--1.1.在Spring中声明bean(对象),通过id名获得对象-->
<bean id="stu1" class="entity.Student"></bean>
<!--1.2.在Spring中声明bean(对象),通过name名获得对象-->
<bean name="stu2" class="entity.Student"></bean>
<!--1.3.通过工厂的静态方法获得对象-->
<bean id="stu3" class="tool.EntityFactory1" factory-method="getStudent3"></bean>
<!--1.4.通过厂的实例方式获得对象-->
<bean id="factory2" class="tool.EntityFactory2"></bean>
<bean id="stu4" factory-bean="factory2" factory-method="getStudent4"></bean>
2.1.set属性注入,前提属性必须有set方法(类里面有属性是其他的对象)
<bean id="c1" class="entity.Tclass">
<property name="cid" value="1"></property>
<property name="cname" value="JavaEE2101"></property>
</bean>
<bean id="stu21" class="entity.Student">
<!--类中普通属性,直接用value注值-->
<property name="sid" value="101"></property>
<property name="sname" value="张三"></property>
<property name="sage" value="18"></property>
<!--类中对象属性,用ref引用bean对象注值-->
<property name="tclass1" ref="c1"></property>
</bean>
2.2.构造注入
<bean id="c2" class="entity.Tclass">
<!--通过构造方法的参数位置给值(上面的0,1是在实体类中构造方法从上往下数的位置,value是赋值)-->
<constructor-arg index="0" value="2"></constructor-arg>
<constructor-arg index="1" value="JavaEE2102"></constructor-arg>
</bean>
<bean id="stu22" class="entity.Student">
<!--通过构造方法的名称给值(通过属性的名字来进行赋值)-->
<constructor-arg name="sid" value="102"></constructor-arg>
<constructor-arg name="sname" value="李四"></constructor-arg>
<constructor-arg name="sage" value="17"></constructor-arg>
<constructor-arg name="tclass1" ref="c2"></constructor-arg>
</bean>
2.3.P命名注入,前提在头部导入p命名空间,属性要有set方法
<bean id="c3" class="entity.Tclass" p:cid="3" p:cname="JavaEE2103"></bean>
<bean id="stu23" class="entity.Student" p:sid="103" p:sage="23" p:sname="王麻子" p:tclass1-ref="c3"></bean>
2.4.spl表达式注入
<bean id="stu24" class="entity.Student">
<property name="sid" value="#{stu23.sid}"></property>
<property name="sname" value="#{stu23.sname}"></property>
<property name="sage" value="#{stu23.sage}"></property>
<property name="tclass1" value="#{stu23.tclass1}"></property>
</bean>
<!--2.5.其他类型属性值注入-->
<bean id="emp25" class="entity.Emp">
<!--给数组属性注值(不是一定要用<set>用<array><list>也可以)-->
<property name="obs1">
<set>
<value>11</value>
<value>钱七</value>
<ref bean="stu23"></ref>
</set>
</property>
<!--给list集合注值-->
<property name="arr2">
<list>
<value>3.14</value>
<value>Hello</value>
<ref bean="stu23"></ref>
</list>
</property>
<!--给set集合注值-->
<property name="set3">
<set>
<value>3.14</value>
<value>Hello</value>
<ref bean="stu23"></ref>
</set>
</property>
<!--给map集合注值-->
<property name="map4">
<map>
<entry key="a1" value="11"></entry>
<entry key="a2" value="哈哈"></entry>
<entry key="a3" value-ref="stu23"></entry>
</map>
</property>
<!--给properties注值-->
<property name="prop5">
<props>
<prop key="b1">12</prop>
<prop key="b2">world</prop>
<prop key="b3">12.12</prop>
</props>
</property>
</bean>
<bean id="tclass" class="entity.Tclass" p:cid="3" p:cname="JavaEE2103"></bean>
测试bean的常用属性的使用,当前类的对象是多例的还是单例scope="prototype/singleton"
lazy-init=""属性只能对scope="singleton"的bean有效,对scope="prototype"无效,scope="prototype"它的bean永远在获得bean时加载
bean的scope="singleton",如果lazy-init="false"加载spring容器时就加载这个bean;如果lazy-init="true"加载spring容器后获得bean时就加载这个bean
autowire=""给bean的对象属性自动注值,如果autowire="byType",那么根据属性的数据类型来注入bean,如果这个数据类型的bean没有,属性就是null;如果这个数据类型的bean有一个,属性就是当前bean;如果这个数据类型的bean有多个,就抛异常
如果autowire="byName",那么这个spring容器中如果有bean的名称与属性名相同就注入到属性中,如果没有bean的名称与属性名相同就是null
<bean id="t1" class="entity.Teacher" scope="prototype" lazy-init="false" init-method="init" destroy-method="destory" autowire="byName">
</bean>
</beans>
spring对象容器的对象创建配置
最新推荐文章于 2022-12-08 09:47:58 发布