标签:
Spring的控制反转:把对象的创建、初始化、销毁等工作交给spring容器来做。由spring容器控制对象的生命周期。
步骤:两个步骤,步骤1有两个加载配置文件形式,通过加载配置文件实例化容器
IOC面向接口的强大
无参构造函数
<bean id=“personService" class="cn.itcast.bean.impl.PersonServiceImpl"/>
package _1demo1;
public class FactoryDemo {
public HelloWordDemo getIns(){
return new HelloWordDemo();
}
}
要实例的方法
package _1demo1;
public class HelloWordDemo {
public void sayHello(){
System.out.println("say hello");
}
}
配置文件配置参数
测试类
package _1demo1;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class test {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
HelloWordDemo helloword = (HelloWordDemo) context.getBean("aaa");
helloword.sayHello();
}
}
标签:
原文地址:http://my.oschina.net/u/2356176/blog/469096