Inverse of Control 反转控制的概念,就是将原本在程序中手动创建对象的控制权,交由Spring框架管理,简单说,就是创建对象控制权被反转到了Spring框架。
Dependency Injection 依赖注入,在Spring框架负责创建Bean对象时,动态的将依赖对象注入到Bean组件。如下例子
在UserService中提供一个get/set的name方法,在beans.xml中通过property去注入。
public class UserServiceImpl implements UserService { //提供一个属性,需要get/set方法 private String name; public String getName() { return name ; } public void setName(String name) { System. out . println("赋值了”+ name); this. name = name ; } public void add() { System. out . println("添加用户”+ name); } } <?xml version= "1.日”encoding="UTF-8"?> 。<beans xmlns= "http://www. springframework . org/ schema/beans xmlns:xsi= "http: //www. w3. org/2001/XML Schema- instance " xsi : schemaLocation= "http://www. springframework . org/schema/beans http://www. springframework . org/ schema/ beans/spring- beans.xsd"> <!--配置一个Bean --> <bean id= "userServiceId" class="com. gyf. spring. demo01. UserServiceImpl "> <property name= "name”value="zhangsan "></property> </bean> | </beans> 相当于 UserService service = new UserServiceImpl() service.setName(" )