(1)IoC = Inversion of Control(由容器控制程序之间的关系)
IoC,用白话来讲,就是由容器来控制程序中的各个类之间的关系,而非传统实现中,直接在代码中由程序代码直接操控。这也就是所谓"控制反转"的概念所在:控制权由应用代码中转到了外部容器,控制权的转移,是所谓反转。
(2)IOC 是一种使应用程序逻辑外在化的设计模式
因为提供服务的组件是被注入而不是被写入到客户机代码中。将 IOC 与接口编程应用结合从而产生出 Spring 框架的架构,这种架构能够减少客户机对特定实现逻辑的依赖。
(3)IoC的设计目标
不创建对象,但是描述创建它们的方式。在代码中不直接与对象和服务连接,但在配置文件中描述哪一个组件需要哪一项服务。
======================================================
配置文件:
-----------------------------------------------------------------
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN " "http://www.springframework.org/dtd/spring-beans.dtd ">
<beans>
<bean id= "createCreditCard " class= "springexample.creditcardaccount.CreateCreditCardAccount ">
<property name= "creditRatingInterface ">
<ref bean= "creditRating " />
</property>
<property name= "creditLinkingInterface ">
<ref bean= "creditLinking " />
</property>
<property name= "emailInterface ">
<ref bean= "email " />
</property>
</bean>
<bean id= "creditLinking " class= "springexample.creditlinking.CreditLinking ">
<property name= "url ">
<value> http://localhost/creditLinkService </value>
</property>
</bean>
<bean id= "creditRating " class= "springexample.creditrating.CreditRating ">
</bean>
<bean id= "email " class= "springexample.email.Email ">
<property name= "smtpHost ">
<value> localhost </value>
</property>
<property name= "fromEmail ">
<value> mycompanyadmin@mycompanyadmin.com </value>
</property>
<property name= "userId ">
<value> myuserid </value>
</property>
<property name= "password ">
<value> mypassword </value>
</property>
</bean>
</beans>
-----------------------------------------------------------------
问题1:
以上的 <property name= "url "> <value> http://localhost/creditLinkService </value> </property>
是指类CreditLinking里的属性“url”在初始化时变量值是“http://localhost/creditLinkService”吗?
如果是,那么在类CreditLinking的set*方法不也是给变量值赋值吗?
SPRING会自动调用 set XXX 方法,来根据配置文件,给相关的类赋值
问题2:
在例子中.java文件里,有直接new实例的,如ICustomer icustomer = new Customer();
也有用IOC的,如
CreateCreditCardAccountInterface creditCardAccount = (CreateCreditCardAccountInterface)appContext.getBean( "createCreditCard ");
到底什么样的类要在XML文件中注册呀?
直接new 出来的,不用配置。 如: Class1 c1=new Class1()
通过beanfactory得到的,需要配置。如: Class1 c1=(Class1)beanfactory.getBean( "class1 ");
用IOC是因为CreateCreditCardAccountInterface接口不能实例化,所以用工厂模式来创建对象;工厂模式也要有CreateCreditCardAccountInterface接口的实现类来创建对象,所以在配置文件中要用 <bean id= "createCreditCard " class= "springexample.creditcardaccount.CreateCreditCardAccount "> 来注册;这样理解对吗?
bean id=createCreditCard节点下的为什么要说 <property name= "emailInterface "> <ref bean= "email " /> </property> 呢?在bean id=email节点下,又为什么要声明 <property name= "smtpHost "> <value> localhost </value> </property> 这个属性的取值呢?
<bean name= "xxx " ...>
<property name= "smtpHost "> <value> localhost </value> </property>
</bean>
其中的 <property> 一行,因为带有 <value> 标签,所以表示对xxx这个类中的smtpHost这个“基本属性”赋值
如果存在以下情况:
<bean name= "xxx " ...>
<property name= "creditRatingInterface ">
<ref bean= "creditRating " />
</property>
</bean>
其中的 <property> 一行,带有 <ref> 标签,所以表示,是对xxx这个类中的复合成员(就是类成员)赋值。
====================================
Spring 从核心而言,是一个DI (dependence injection )容器,其设计哲学是提供一种无侵入式的高扩展性框架。即无需代
码中涉及Spring专有类,即可将其纳入Spring容器进行管理。
,EJB则是一种高度侵入性的框架规范,它制定了众多的接口和编码规范,要求实现者必须
遵从。侵入性的后果就是,一旦系统基于侵入性框架设计开发,那么之后任何脱离这个框架的企图都将付
出极大的代价。