package jk.Test;
import jk.hellow.hellow;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class myTest {
public static void main(String[] args){
//解析beans .xml文件生成管理相应的bean对象
ApplicationContext context =new ClassPathXmlApplicationContext();
hellow hellow=(hellow)context.getBean("hellow");
hellow.show();
//思考? hellow 对象是谁创建的?spring容器
//思考? hellow 对象属性是谁创建的?spring容器设置
//这个过程就叫控制反转————控制的内容:指谁来控制对象的创建;传统的应用程序都是由程序本身创建的,
//但是spring 框架后,这个对象是由spring管理创建的
//————反转:正传是有程序创建对象,而反转是反过来了本身不去创建对象,而是被动的接收对象。
//总结:以前对象是由程序本身创建,使用Spring后,程序变为被动接收Spring创建的对象。
//控制反转:依赖注入(indpendency injection)
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans"> <bean class="jk.hellow.hellow" name="hellow"/><property name="name" value="小姜"/>
</beans>
package jk.hellow;
public class hellow {
private String name;
private void setName(String name) {
this.name=name;
}
public void show() {
System.out.println("hellow!"+name);
}
}