IOC和DI
什么是IOC(Inversion 反转 of Control 控制)====控制反转
- 对象之间的依赖关系应该由容器来建立
什么是DI(Dependency Injection)
容器可以通过调用set方法,或者构造器来建立对象之间的依赖关系.
注:
IOC是目标,DI是手段.
使用set方法来建立关系
<bean id="b1" class="cn.huang.ioc.B"/>
<!--
配置set方法注入
property元素的name属性:用于指定set方法名
ref属性:用于指定被注入的bean的id
-->
<bean id="a1" class="cn.huang.ioc.A">
<property name="b" ref="b1"/><!-- 调用A的setB()方法 -->
</bean>
对象的依赖关系由自己负责,即在自己类中创建别的类再调用别的类的方法
使用构造器的方式注入(不常用)
<bean id="wt1" class="cn.huang.ioc.Waiter"/>
<!-- 构造器注入 constructor-arg
index:用来指定参数的下标
ref:用于指定被注入的bean的id
-->
<bean id="rest1" class="cn.huang.ioc.Restaurant">
<constructor-arg index="0" ref="wt1"/>
</bean>
自动装配(注入)----(了解)
a:自动装配指的是,容器按照某种规则,自动建立对象之间的依赖关系.
即不用明确指定被注入的bean的id.
容器地城任然需要调用set方法或者构造器来完成依赖关系的建立.
b:容器默认情况下是不会自动装配的.
<bean id="en" class="cn.huang.autowire.Enginee"/>
<!-- autowire自动装配属性,用于指定自动装配的方式
byName:
容器首先检查该类中,以其属性名作为bean的id来查找对应的bean,找到之后调用该属性的set方法来完成注入;
如果找不到对应的bean,不调用set方法,则不注入
byType:
容器会先检查该类中,以其属性的类型作为bean的类型,来查找对应的bean,找到之后调用set方法来完成注入.
如果找不到对应的bean,则不注入,同上.
如果找到多个,则报错org.springframework.beans.factory.UnsatisfiedDependencyException
constructor:类似于byType,不过通过构造器完成注入
总结:自动装配少用,一定要用则byName
-->
<bean id="car" class="cn.huang.autowire.Car" autowire="byType"/>
注入基本类型的值
<!-- 注入基本类型的值(包括String),使用value属性 -->
<bean id="val" class="cn.huang.value.ValueBean">
<property name="name" value="花花"/>
<property name="age" value="23"/>
</bean>
注入集合类型的值
List ,Set ,Map ,Properties
List:
<property name="hobbies">
<list>
<value>抽烟</value>
<value>打麻将</value>
<value>看电视</value>
</list>
</property>
Set:
<property name="city">
<set>
<value>北京</value>
<value>上海</value>
<value>广州</value>
</set>
</property>
Map:
<property name="score">
<map>
<entry key="语文" value="30"/>
<entry key="数学" value="98"/>
</map>
</property>
Properties:
<property name="db">
<props>
<prop key="username">tom</prop>
<prop key="password">123</prop>
</props>
</property>
以引用的方式注入集合的值(值可以重用)
<!-- 使用引用的方式注入集合类型的值 -->
<util:list id="hobbiesBean">
<value>抽烟</value>
<value>喝酒</value>
<value>烫头</value>
</util:list>
<util:set id="cityBean">
<value>长沙</value>
<value>河南</value>
</util:set>
<util:map id="scoreBean">
<entry key="english" value="98"></entry>
<entry key="math" value="100"></entry>
</util:map>
<util:properties id="dbBean">
<prop key="username">sally</prop>
<prop key="password">159</prop>
</util:properties>
<bean id="vb2" class="cn.huang.value.ValueBean">
<property name="hobbies" ref="hobbiesBean"></property>
<property name="city" ref="cityBean"></property>
<property name="score" ref="scoreBean"></property>
<property name="db" ref="dbBean"></property>
</bean>
在Spring中读取properties文件
<!-- 读取.properties文件的内容
location:指定了properties文件的位置,Spring容器会读取指定文件的内容,然后将这些内容添加到Properties对象中
-->
<util:properties id="config" location="classpath:config.properties"/>
Spring表达式
<!-- 使用spring表达式读取其他bean的属性值 -->
<bean id="sb1" class="cn.huang.value.SpelBean">
<property name="name" value="#{val.name}"></property><!-- String:寻找id为val的bean,调用该对象的getName()方法获取属性值 -->
<property name="interest" value="#{val.hobbies[2]}"></property><!-- List -->
<property name="score" value="#{val.score['英语']}"></property><!-- Map:#{val.score.english} -->
<property name="pageSize" value="#{config['pageSize']}"></property><!-- Properties:#{config.pageSize} -->
</bean>