理论:spring
IOC(Inversion of Control控制反转)spring-mvc
DI(依赖注入) (Dependency Injection)mvc
它不是一种技术而是一种思想。当初IOC理论的提出就是为了解决对象之间的“解耦”。在传统的建立对象的方式中咱们直接在对象内部经过new进行建立对象,是程序主动去建立依赖对象;而如今IOC是有专门一个容器来建立这些对象,即由IOC容器来控制对象的建立,并将依赖对象注入给调用者,这样就产生为了“依赖注入”的概念,也就是"DI"。spa
<?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" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd"> <!-- 以上是所有Spring都同样的配置文件 、beans是根元素--> <bean id="stoneAxe" class="com.StoneAxe"></bean> <!-- 下面是根据注解方式进行注入 --> <context:annotation-config/> <bean id="chinese" class="com.Chinese"/> </beans>
public class Chinese implements Person { @Autowired private Axe axe; }
<?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" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd"> <!-- 以上是所有Spring都同样的配置文件 、beans是根元素--> <bean id="stoneAxe" class="com.StoneAxe"></bean> <!-- 下面是根据属性注入,也就是set方法注入 --> <bean id="chinese" class="com.Chinese"> <property name="axe" ref="stoneAxe"/> </bean> </beans>
<?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" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd"> <!-- 以上是所有Spring都同样的配置文件 、beans是根元素--> <bean id="stoneAxe" class="com.StoneAxe"></bean> <!-- 下面是根据构造器注入 --> <bean id="chinese" class="com.Chinese"> <constructor-arg ref="stoneAxe"/> </bean> </beans>