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

Spring之IOC

2016-06-29 00:00 302 查看
#Spring之IOC
##1.简单demo
在配置文件中:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> 
<bean id="customer"
class="com.gefufeng.Customer">
</bean>
</beans>

定义一个bean,然后在java文件中:

ApplicaitonContext ac = new ClassPathXmlApplicationContext(bean.xml);
Customer customer = (Customer)ac.getBean("customer");

就可以用了。

##2应用
模拟一种场景,有张三和李四两个测试员测试软件bug,程序要灵活的使用每个测试员,代码构成:张三类,李四类,测试员抽象类,部分代码:

在beans.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> 
<bean id="zhangsan" class="com.gefufeng.ZhangSan"></bean>
<bean id="lisi" class="com.gefufeng.LiSi"></bean>
<bean id="factory" class="com.gefufeng.Factory">
<property name="tester" ref="zhangsan"></property>
</bean>

</beans>

这里主要看Factory类中有一个tester属性,类型为Tester,其实现类为ZhangSan和LiSi,在Factory中有tester的setter方法,就可以使用bean 的property属性了。若改成LiSi,直接在ref中改为lisi就可以了。
###2.1属性注入
上面的例子其实就是属性注入,注意要有属性的stter方法,就能使用property标签进行注入了。
###2.2构造注入
构造注入有两种一种是根据构造方法中构造参数类型注入:

<bean id="exampleBean" class="examples.ExampleBean">
<constructor-arg type="int" value="7500000"/>
<constructor-arg type="java.lang.String" value="42"/>
</bean>

这样ExampleBean对应的两个属性就有值了

还有一种是索引注入:

<bean id="exampleBean" class="examples.ExampleBean">
<constructor-arg index="0" value="7500000"/>
<constructor-arg index="1" value="42"/>
</bean>

那么构造器中的第一个和第二个参数都有值了

当然这两种情况可以混合使用

###2.3工厂注入
####2.3.1非静态方法工厂注入

<bean id="factory" class="examples.Factory"></bean>
<bean id="clientService"
factory-bean="factory"
factory-method="createBean"/>

假如有一个工厂类,类中一个非静态方法createBean,方法的返回值类型为Bean,那么调用clientService就可以以生成一个Bean了。
###2.3.2静态方法工厂注入

<bean id="exampleBean" class="examples.ExampleBean" factory-method="createInstance"/>

就可以了,因为方法为静态的,所以类名.方法就能调用。
##3属性注入详解
一个类中的属性类型有简单的也有复杂的,共有这么多类型

bean | ref | idref | list | set | map | props | value | null

主要看collections

<bean id="moreComplexObject" class="example.ComplexObject">
<!-- results in a setAdminEmails(java.util.Properties) call -->
<property name="adminEmails">
<props>
<prop key="administrator">administrator@example.org</prop>
<prop key="support">support@example.org</prop>
<prop key="development">development@example.org</prop>
</props>
</property>
<!-- results in a setSomeList(java.util.List) call -->
<property name="someList">
<list>
<value>a list element followed by a reference</value>
<ref bean="myDataSource" />
</list>
</property>
<!-- results in a setSomeMap(java.util.Map) call -->
<property name="someMap">
<map>
<entry key="an entry" value="just some string"/>
<entry key ="a ref" value-ref="myDataSource"/>
</map>
</property>
<!-- results in a setSomeSet(java.util.Set) call -->
<property name="someSet">
<set>
<value>just some string</value>
<ref bean="myDataSource" />
</set>
</property>
</bean>

##4方法注入
用到的很少,作为了解

<bean id="command" class="fiona.apple.AsyncCommand" scope="prototype">
<!-- inject dependencies here as required -->
</bean>

<!-- commandProcessor uses statefulCommandHelper -->
<bean id="commandManager" class="fiona.apple.CommandManager">
<lookup-method name="createCommand" bean="command"/>
</bean>

##bean之间的关系
1.继承

<bean id="inheritedTestBean" abstract="true"
class="org.springframework.beans.TestBean">
<property name="name" value="parent"/>
<property name="age" value="1"/>
</bean>

<bean id="inheritsWithDifferentClass"
parent="inheritedTestBean" />
<property name="sex" value="男"/>
<!-- the age property value of 1 will be inherited from parent -->
</bean>

2.依赖

就是在初始化beanOne的时候,因为beanOne要依赖manager和accountDao,所以加上depends-on,就会先初始化manager和accountDao

<bean id="beanOne" class="ExampleBean" depends-on="manager,accountDao">
<property name="manager" ref="manager" />
</bean>

<bean id="manager" class="ManagerBean" />
<bean id="accountDao" class="x.y.jdbc.JdbcAccountDao" />

3.引用

引用就是ref的使用
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  spring IOC 控制反转