SpringIOC学习
SpringIOC学习
第一步:
导入相关jar包:
commons-logging-1.1.3.jar;
spring-beans-4.0.0.RELEASE.jar;
spring-context-4.0.0.RELEASE.jar;
spring-core-4.0.0.RELEASE.jar;
spring-expression-4.0.0.RELEASE.jar
第二步:
配置spring相关信息
代码展示:
Hello.java
public class Hello {
private String name;
public void setName(String name) {
this.name = name;
}
public void show(){
System.out.println("hello:" + name);
}
}
beans.xml
<?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:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
<bean name = "hello" class = "com.cn.bean.Hello">
<property name = "name" value = "全蛋男神"></property>
</bean>
</beans>
HelloTest.java
public class HelloTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
Hello hello = (Hello) context.getBean("hello");
hello.show();
}
}
控制的内容:由谁去创建对象,传统的方式是由程序去创建对象,加入spring后,由spring去创建对象。
反转:正转是由程序去创建对象,反转则是由程序去接收对象。