您的位置:首页 > 编程语言 > Java开发

IOC

2016-11-07 00:00 113 查看
摘要: 依赖注入(Dependecy Injection)和控制反转(Inversion of Control)是同一个概念,具体的讲:当某个角色
需要另外一个角色协助的时候,在传统的程序设计过程中,通常由调用者来创建被调用者的实例。但在spring中
创建被调用者的工作不再由调用者来完成,因此称为控制反转。创建被调用者的工作由spring来完成,然后注入调用者
因此也称为依赖注入。

使用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:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> <!--静态注入-->
<!--使用默认空的构造器注入-->
<bean id="duke" class="com.zc.demo.di.Juggler"/>
<!--使用该有参数的构造器注入-->
<bean id="duke1" class="com.zc.demo.di.Juggler">
<constructor-arg value="15"/>
</bean>
<!--通过构造器注入变量和引用-->
<bean id="sonnet29" class="com.zc.demo.di.Sonnet29"/>
<bean id="poeticDuke" class="com.zc.demo.di.PoeticJuggler">
<constructor-arg value="15"/>
<constructor-arg ref="sonnet29"/>
</bean>
<!--通过静态工厂方法注入实例-->
<bean id="theStage" class="com.zc.demo.di.Stage" factory-method="getInstance"/>
<!--设置作用域  prototype覆盖默认的单例模式 每次调用都创建一个新的实例-->
<bean id="ticket" class="com.zc.demo.di.Ticket" scope="prototype"/>
<!--通过init-method和destroy-method初始化和销毁bean-->
<bean id="auditorium" class="com.zc.demo.di.Auditorium" init-method="turnOnLights" destroy-method="turnOffLights"/>

<!-- 对于私用变量,通过setter方法注入-->
<bean id="kenny" class="com.zc.demo.di.Instrumentalist">
<property name="song" value="Jingle Bells"/>
<property name="age" value="35"/>
<property name="instrument" ref="saxophone"/>
</bean>
<bean id="saxophone" class="com.zc.demo.di.Saxophone"/>
<!--注入内部类-->
<bean id="kennyInner" class="com.zc.demo.di.Instrumentalist">
<property name="song" value="Jingle Bells"/>
<property name="instrument">
<bean class="com.zc.demo.di.Saxophone"/>
</property>
</bean>
<!--使用命名空间 P 装配属性-->
<bean id="kennyToP" class="com.zc.demo.di.Instrumentalist"
p:song="Jingle Bells" p:age="20"
p:instrument-ref="saxophone"/>
<!--注入集合-->
<bean id="guitar" class="com.zc.demo.di.Guitar"/>
<bean id="cymbal" class="com.zc.demo.di.Cymbal"/>
<bean id="harmonica" class="com.zc.demo.di.Harmonica"/>
<bean id="hank" class="com.zc.demo.di.OneManBand">
<property name="instruments">
<list>
<ref bean="guitar"/>
<ref bean="cymbal"/>
<ref bean="harmonica"/>
</list>
</property>
<!--set会去重-->
<property name="instrumentSet">
<set>
<ref bean="guitar"/>
<ref bean="cymbal"/>
<ref bean="harmonica"/>
<ref bean="harmonica"/>
</set>
</property>
<property name="instrumentMap">
<map>
<entry key="GUITAR" value-ref="guitar"/>
<entry key="CYMBAL" value-ref="cymbal"/>
<entry key="HARMONICA" value-ref="harmonica"/>
</map>
</property>
<property name="properties">
<props>
<prop key="Guitar">strum strum strum</prop>
<prop key="Cymbal">crash crash crash</prop>
<prop key="Harmonica">hum hum hum</prop>
</props>
</property>
</bean>

<!--使用spring表达式  SpEL 装配-->
<bean id="expression" class="com.zc.demo.di.Expreession">
<property name="count" value="#{5}"/>
<property name="message" value="The value is #{5}"/>
<property name="frequency" value="#{89.7}"/>
<property name="capacity" value="#{1e4}"/>
<property name="name" value="#{'Chuck'}"/>
<property name="enabled" value="#{false}"/>
<property name="instrument" value="#{saxophone}"/>
<!--动态装配一个bean下的属性-->
<!-- <property name="song" value="#{kenny.song}"/>-->
<!--动态装配一个bean 调用其方法  并对返回值大写-->
<property name="songSelect" value="#{songSelect.selectSong().toUpperCase()}"/>
<!--为了避免NullPointerException异常 使用?.代替.  保证当selectNull()返回null时  不调用toUpperCase()函数-->
<property name="selectNull" value="#{songSelect.selectNull()?.toUpperCase()}"/>
<!--使用T()运算符直接操作类-->
<property name="multiplier" value="#{T(java.lang.Math).PI}"/>
<property name="randomNumber" value="#{T(java.lang.Math).random()}"/>
<!--算术运算符-->
<property name="addition" value="#{songSelect.return10() + 40}"/>
<property name="subtraction" value="#{songSelect.return10() - 11}"/>
<property name="multiplication" value="#{2 * T(java.lang.Math).PI * songSelect.return10()}"/>
<property name="division" value="#{songSelect.return10() / songSelect.num}"/>
<property name="remainder" value="#{songSelect.num % 2}"/>
<property name="area" value="#{T(java.lang.Math).PI * songSelect.num ^ 2}"/>
<!--关系运算符  ==:eq <:lt >:gt <=:le >=ge 建议使用文本型运算符-->
<property name="equal1" value="#{songSelect.num eq 5}"/>
<property name="equal2" value="#{songSelect.num lt 5}"/>
<property name="equal3" value="#{songSelect.num le 5}"/>
<property name="equal4" value="#{songSelect.num gt 5}"/>
<property name="equal5" value="#{songSelect.num ge 5}"/>
<!--逻辑运算 and or not或!-->
<property name="equal6" value="#{songSelect.num eq 5 and songSelect.name=='zc'}"/>
<property name="equal7" value="#{songSelect.num eq 6 or songSelect.name=='zc'}"/>
<property name="equal8" value="#{!songSelect.flag}"/>
<property name="equal9" value="#{not songSelect.flag}"/>
<!--条件运算-->
<property name="condition" value="#{songSelect.name=='zc1' ? songSelect.name : 'zhangchi'}"/>
<property name="condition1" value="#{songSelect.name != null ? songSelect.name : 'zhangchi'}"/>
<property name="condition2" value="#{songSelect.name ?: 'zhangchi'}"/>
<!--正则表达式运算-->
<property name="matches" value="#{songSelect.email matches '[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.com'}"/>
</bean>
<bean id="songSelect" class="com.zc.demo.di.SongSelect"/>

<!--操作集合-->
<!--定义5个city bean的集合-->
<util:list id="cities">
<bean class="com.zc.demo.di.City" p:name="北京" p:state="BJ" p:population="87984564"/>
<bean class="com.zc.demo.di.City" p:name="天津" p:state="TJ" p:population="56445643"/>
<bean class="com.zc.demo.di.City" p:name="上海" p:state="SH" p:population="28531146"/>
<bean class="com.zc.demo.di.City" p:name="重庆" p:state="CQ" p:population="21321322"/>
<bean class="com.zc.demo.di.City" p:name="深圳" p:state="SZ" p:population="10000000"/>
</util:list>
<bean id="beijing" class="com.zc.demo.di.City" p:name="北京" p:state="BJ" p:population="87984564"/>
<bean id="tianjin" class="com.zc.demo.di.City" p:name="天津" p:state="TJ" p:population="56445643"/>
<bean id="shanghai" class="com.zc.demo.di.City" p:name="上海" p:state="SH" p:population="28531146"/>
<bean id="chongqing" class="com.zc.demo.di.City" p:name="重庆" p:state="CQ" p:population="21321322"/>
<bean id="shenzhen" class="com.zc.demo.di.City" p:name="深圳" p:state="SZ" p:population="10000000"/>
<util:map id="citiesMap">
<entry key="beijing" value-ref="beijing"/>
<entry key="tianjin" value-ref="tianjin"/>
<entry key="shanghai" value-ref="shanghai"/>
<entry key="chongqing" value-ref="chongqing"/>
<entry key="shenzhen" value-ref="shenzhen"/>
</util:map>
<util:properties id="settings" location="classpath:props/settings.properties"/>

<!--spring提供systemEnvironment环境变量集合、systemProperties程序启动时配置的参数集合-->
<bean id="chosenCity" class="com.zc.demo.di.ChosenCity">
<!--通过下标直接访问集合-->
<property name="city" value="#{cities[2]}"/>
<!--随机获取集合内容-->
<property name="randomCity" value="#{cities[T(java.lang.Math).random() * cities.size()]}"/>
<property name="mapCity" value="#{citiesMap['shanghai']}"/>
<property name="propertiesCity" value="#{settings['twitter.accessToken']}"/>
<property name="pathHome" value="#{systemEnvironment['HOME']}"/>
<property name="applicationHome" value="#{systemProperties['user.name']}"/>
<!--根据条件查询集合-->
<property name="cityList" value="#{cities.?[population gt 20000000]}"/>
<!--返回符合条件的第一个集合-->
<property name="firstCity" value="#{cities.^[population gt 20000000]}"/>
<!--范湖媳妇和条件的最后一个集合-->
<property name="lastCity" value="#{cities.$[population gt 20000000]}"/>
<!--投影集合-->
<property name="cityNames" value="#{cities.![name]}"/>
<property name="cityNameAndStates" value="#{cities.![name + ',' + state]}"/>
</bean>
</beans>


使用注解annotation ,在使用annotation时需在xml文件中开启支持。

<!--开启注解装配-->
<context:annotation-config/>
<context:component-scan base-package="com.zc"/>


public class ExecutionAnnotation extends BaseTest{

@Autowired
@Qualifier("guitarAnnotation")
private InstrumentAnnotation instrumentAnnotation;
@Autowired
@Qualifier("saxophoneAnnotation")
private InstrumentAnnotation saxophone;

@Autowired
@Allegro
private InstrumentAnnotation allegro;

@Value(value = "${twitter.accessToken}")
private String token;

@Test
public void oneMethod(){
instrumentAnnotation.play();
saxophone.play();
allegro.play();
System.out.println(token);
}

}

项目的demo源码可从https://git.oschina.net/ciyuan/ssm.git获取
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  spring ioc di 依赖注入