Spring是基于IOC与AOP的框架,而其中的IOC(Inversion of Control)即反转控制是Spring的基础。
在以前学过的知识中,一个新的对象全部为自己手动new出来的,而在Spring中创建对象的的过程则交由了Spring框架来完成。
依赖注入(Decpendency Inject)的意思则为拿到对象的时候,该对象的属性值已被自动注入了,可以直接使用。
<?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <bean name="c" class="com.how2java.pojo.Category"> <property name="name" value="category 1" /> </bean> </beans>
bean标签:
id:指定bean的名称,在获取bean和被依赖时使用。
name:指定bean的别名。
class:指定bean的来源。
property标签:
name:指定要注入的属性名。
value:被注入属性的值。
ref:用于注入另外一个对象,内表明所注入对象的的名称。
<bean name="p" class="com.how2java.pojo.Product"> <property name="name" value="product1" /> <property name="category" ref="c" /> </bean>
上述将Category对象注入了Product对象之中。
原文:https://www.cnblogs.com/huqingfeng/p/12373079.html