一.(*)根据bean的类型从IOC容器中获取bean的实例
将上一篇博客中的代码进行改进(*) https://blog.csdn.net/myjess/article/details/105656310
测试代码改为:
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class test {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
HelloSpring bean = applicationContext.getBean(HelloSpring.class);
bean.sayHello();
}
}
因为容器创建了两个对象,而HelloSpring bean = applicationContext.getBean(HelloSpring.class);
只能获取一个对象,程序报错:NoUniqueBeanDefinitionException: No qualifying bean of type ‘HelloSpring’ available: expected single matching bean but found 2: hello01,hello02
如果IOC容器中该类型 bean 有多个,查找失败;
只能用 id 来查找
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class test {
public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");
HelloSpring hello01 = (HelloSpring)applicationContext.getBean("hello01", HelloSpring.class);
HelloSpring hello02 = (HelloSpring)applicationContext.getBean("hello02", HelloSpring.class);
hello01.sayHello();
hello02.sayHello();
}
}
二. 通过构造器给bean的属性赋值
(*)方法一:
( property标签是调用的setter方法)
直接调用有参构造器
方法2
constructor-arg 标签中只指定 value 不指定 name,那么标签顺序一定要是构造起参数的顺序。
也可以添加 index 属性(下标从0开始)防止顺序错误,如:index=3,则是第四个参数
<constructor-arg value="myjess" index="0"></constructor-arg>
<constructor-arg value="20" index="1"> </constructor-arg>
若原来有4个参数的构造方法重载为两个含3个参数的构造方法
要指定参数类型,不然随机赋值
<constructor-arg value="20" index="1" type="java.lang.Integer"></constructor-arg>
三. 为各种属性赋值
//省略 getter/setter 和 tostring 方法
public class HelloSpring {
//基本数据类型直接使用property标签
private String name;
private String age;
private pen pens;
private List<book> books;
private Map<String,Object> maps;
private Properties properties;
}
public class book {
private String bookname;
private String author;
}
public class pen {
private String color;
private Integer price;
}
为 pen 类型赋值
<!-- 方法一 -->
<bean id="pen11" class="pen">
<property name="color" value="red"></property>
<property name="price" value="15"></property>
</bean>
<bean id="pen01" class="HelloSpring">
<!-- ref: 引用外面的一个值 -->
<!-- 若容器中的pen改变了,引用的pen随之改变 -->
<property name="pens" ref="pen11"></property>
</bean>
<!-- 方法二 -->
<!-- 引用内部bean -->
<bean id="pen02" class="HelloSpring">
<property name="pens">
<bean class="pen">
<property name="color" value="black"></property>
</bean>
</property>
</bean>
为 List 类型赋值
<bean id="book01" class="book">
<property name="author" value="jess"></property>
<property name="bookname" value="javabook"></property>
</bean>
<bean id="List01" class="HelloSpring">
<property name="books" >
<list>
<!-- 引用内部对象-->
<bean class="book">
<property name="bookname" value="cbook"></property>
<property name="author" value="Eunice"></property>
</bean>
<!-- 引用外部对象-->
<ref bean="book01"></ref>
</list>
</property>
</bean>
内部的bean不能被获取到,就算添加了 id 也没有用
给Map类型赋值
<bean id="map" class="HelloSpring">
<property name="maps">
<map>
<!-- 一个entry代表一个键值对 -->
<entry key="key01" value="myjess"></entry>
<entry key="key02" value="20"></entry>
<entry key="key03" value-ref="book01"></entry>
<entry key="key04">
<bean class="pen">
<property name="color" value="grey"></property>
</bean>
</entry>
</map>
</property>
</bean>
给property类型赋值
<bean id="property" class="HelloSpring">
<property name="properties">
<props>
<!-- 值直接写在标签体中 -->
<prop key="username">root</prop>
<prop key="passwurd">123456</prop>
</props>
</property>
</bean>
四. util 命名空间创建集合类型的bean
便于他人引用
util 命名空间可以帮助我们快速的定义list、map、set等。如果要使用它,我们首先需要在XML配置文件中引入其对应的namespace。
<?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:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.1.xsd">
</beans>
一定要在xmlns:context="http://www.springframework.org/schema/context"
下方输入 xmlns:util= 才会有提示
示例:
<bean class="HelloSpring">
<property name="maps" ref="mymap"></property>
</bean>
<!-- 相当于new LinkedHashMap-->
<util:map id="mymap">
<entry key="key01" value="myjess"></entry>
<entry key="key02" value="20"></entry>
<entry key="key03" value-ref="book01"></entry>
</util:map>
<!-- list内有4个元素-->
<!-- list[]、HelloSpring对象、16、Map-->
<util:list id="mylist">
<list></list>
<bean class="HelloSpring"></bean>
<value>16</value>
<ref bean="mymap"></ref>
</util:list>
五. 级联属性赋值
级联属性:属性的属性(如:color,它是Hellospring对象的属性pens的属性)
<bean id="person05" class="HelloSpring">
<property name="pens" ref="pen11"></property>
<property name="pens.color" value="yellow"></property>
</bean>
<!-- 虽然引用的pen11中的color是red,但是级联赋值后都变成了yellow -->
级联属性可以修改属性的属性,但是原来的bean值可能会被修改