1.通过工厂模式+反射+配置文件的方式,实现程序的解耦,将类交给Spring框架统一管理
HelloTest类中使用UserService类对象
传统方式:
UserService userService = new UserServiceImpl();
Spring方式:
public void demo1(){
//使用Spring工厂:
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
//通过工厂获得类
UserService userService = (UserService)applicationContext.getBean("userService");
userService.sayHello();
}
2.applicationContext.xml配置文件格式:
2.IOC 和 DI的概念 1.IOC
IOC Inverse of Control 反转控制的概念,就是将原本再程序中手动创建的UserService对象的控制权,交由Spring框架管理;
简单的说,就是创建UseService对象控制权被反转到了Spring框架。
2.DI Dependency Injection 依赖注入的概念就是在Spring创建这个对象的过程中,将这个对象所依赖的属性注入进去



