Spring-IOC 之 XML配置数据元
Spring的控制反转IOC容器,在其官网上就有这么一句话:IoC也称为依赖注入(DI)。
Spring官方在解释Dependency Injection(DI)依赖注入的时候已经提供了相应的解释。
在此过程中,对象仅通过构造函数参数,工厂方法的参数或在构造或从工厂方法返回后在对象实例上设置的属性来定义其依赖项(即,与它们一起使用的其他对象) 。然后,容器在创建bean时注入那些依赖项。spring.io.
若果上述这句话不清楚的话,请看下边:
通过控制反转,对象在被创建的时候,由一个调控系统内所有对象的外界实体将其所依赖的对象的引用传递给它。也可以说,依赖被注入到对象中。科普中国.
(这里面的外界实体其实就是IOC)
IOC 源数据的配置
1.以简单直观的XML格式:
官方提供两种常见的ApplicationContext的实现类:
- FileSystemXmlApplicationContext
ApplicationContext context = new FileSystemXmlApplicationContext("D:\\beans.xml");
- ClassPathXmlApplicationContext(常见)
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
两者的构造函数的格式大致相同,支持可变参数(不定数量的同类型参数),数组等。
在这里提供一些xml里面主流标签的简单实用方式:
<?xml version="1.0" encoding="UTF-8"?> <!--XML的标准格式-->
<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" <!--引用p命名空间-->
xmlns:c="http://www.springframework.org/schema/c" <!--引用c命名空间-->
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- 加载petBeans.xml -->
<import resource="petBeans.xml"/>
<!--别名 既可以用yangyang获取相关Bean-->
<alias name="childBean" alias="yangyang"/>
<!--构造器初始化bean 必须和构造函数相匹配(参数对应) -->
<!-- constructor-arg 基于构造器依赖注入
index 代表下标 赋值 0开始
type 代表 类型赋值
name 代表属性名赋值
ref 引用
注意:循环依赖问题只能用setter方法创建bean
-->
<bean id="childBean" class="com.link.childBean">
<constructor-arg name="age" value="20"></constructor-arg>
<constructor-arg index="1" value="link_child"></constructor-arg>
</bean>
<!--c:命名空间执行与 基于构造函数的依赖项注入相同的操作 -->
<bean id = "childBean" class="com.link.Child" c:_0="20" c:name="link" >
<!--bean:name属性支持符号等分割 既 可以用user去获取bean也可以用userInfo获取-->
<bean id="userInfo" name="user,userInfo" class="com.link.UserInfoBean" >
<!--此处注意使用property 主要是调用对象的 setter 方法 基于setter依赖注入
单纯用property 赋值 必须存在无参构造函数
-->
<property name="name" value="10"></property>
<!-- ref 引用其他bean数据元 也可以单独抽出来做一个字标签 -->
<!-- <property name="child" ><null/></property> -->
<property name="child" ref=“childBean”>
<!--<ref bean="childBean"></ref>-->
</property>
</bean>
<!--p命名空间 主要用的也是对象的 Setter 方法注入属性-->
<bean id="userInfo" name="user" class="com.link.UserInfoBean" p:child-ref="childBean" p:adress="某个地方">
<!--这里是一些Collections和Map的属性-->
<bean id="colloctionAndMapBean" class="com.link.ColloctionAndMap">
<property name="properties">
<props>
<prop key="keyOne">valueOne</prop>
<prop key="keyTwo">valueTwo</prop>
<prop key="keyThree">valueThree</prop>
</props>
</property>
<property name="list">
<list>
<value>a list element followed by a reference</value>
<ref bean="childBean" />
</list>
</property>
<!-- results in a setSomeMap(java.util.Map) call -->
<property name="map">
<map>
<entry key="mapOneKey" value="just some string"/>
<entry key ="mapTwoKey" value-ref="childBean"/>
</map>
</property>
<!-- results in a setSomeSet(java.util.Set) call -->
<property name="set">
<set>
<value>just some string</value>
<ref bean="childBean" />
</set>
</property>
</bean>
</beans>
当然这些知识一部分常用的 ,其他的就不做解释了,比如:bean标签下的 abstract=“true/false”(代表抽象,默认false),autowire(自动装配依赖下的bean引用,大白话就是引用对象属性自动装配)常用byName/byType ,scope(作用域)=“prototype/singleton” prototype 属性每次getBean时候都会创建一个新的Bean对象,而singleton则是单例,默认是单例;parent 代表父类是谁谁谁等;destroy-method 摧毁时调用的方法。这些都是官网明确说明的。
2.基于注释的配置元数据(Spring2.5版本以后支持)
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<!--注解注入-->
<context:annotation-config/>
</beans>
@Autowired 底层是通过反射实现注入所以并不需要set方法,先通过ByType寻址,如果存在多个再用byName过滤 ,可用于构造器,setter,以及属性上
@Resource 先通过ByName的类型方式去匹配,如果没有相同名字就会按照ByType匹配,可用于setter,以及属性上
@Qualifier(value = "") 指定注释是那个名称 ,可用于构造器,setter,以及属性上。
3.基于注释的配置元数据(Spring3.0版本以后支持)