4.Spring容器创建的三种方式
加载spring容器的三种方式:
1.类路径获得配置文件
配置文件在src下的情况,classes的路径可以直接写beans.xml
1 @Test() 2 public void test2(){ 3 4 //Spring容器加载有三种方式 5 //第一种:ClassPathXmlApplicationContext ClassPath类路径加载:指的就是classes路径 6 ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); 7 IUserService userService = (IUserService) context.getBean("userService"); 8 userService.add(); 9 }
如果将beans.xml移至目录下,beans.xml文件的路径也要发生改变
1 @Test() 2 public void test2(){ 3 //1.加载beans.xml配置文件 4 ApplicationContext context = new ClassPathXmlApplicationContext("com/gyf/beans.xml"); 5 //2.从spring容器获取userService对象 6 UserService userService1 = (UserService)context.getBean("userService"); 7 userService1.add(); 8 }
2.文件系统路径获得配置文件
1 @Test() 2 public void test2(){ 3 4 //第二种:文件系统路径获得配置文件【绝对路径】 5 ApplicationContext context = new FileSystemXmlApplicationContext("F:\\IdeaProjects\\spring-demo2\\out\\production\\spring-demo2\\beans.xml"); 6 7 IUserService userService = (IUserService) context.getBean("userService"); 8 userService.add(); 9 }
3.使用BeanFactory(了解)
1 @Test() 2 public void test2() { 3 4 //第三种:使用BeanFactory(了解) 5 String path = "F:\\IdeaProjects\\spring-demo2\\out\\production\\spring-demo2\\beans.xml"; 6 BeanFactory context = new XmlBeanFactory(new FileSystemResource(path)); 7 8 IUserService userService = (IUserService) context.getBean("userService"); 9 userService.add(); 10 }
♦BeanFactory和ApplicationContext对比
→BeanFactory 采取延迟加载,第一次getBean时才会初始化Bean
→ApplicationContext 即时加载
→是对BeanFactory扩展,提供了更多功能
- 国际化处理
- 事件传递
- Bean 自动装配
- 各种不同应用层的Context实现
============================================================
在UserServiceImpl里添加一个无参构造方法来比较延时加载和即时加载
UserServiceImpl.java
1 public class UserServiceImpl implements IUserService { 2 3 private String name; 4 5 public String getName() { 6 return name; 7 } 8 9 public void setName(String name) { 10 this.name = name; 11 } 12 13 public UserServiceImpl() { 14 System.out.println("UserServiceImpl()。。调用了"); 15 } 16 17 @Override 18 public void add() { 19 System.out.println("添加用户。。。"+name); 20 } 21 }
main
1.【ClassPathXmlApplicationContext】加载配置文件,但不调用getBean(),即时加载(加载即生成对象)
1 @Test() 2 public void test2() { 3 4 //Spring容器加载有三种方式 5 //第一种:ClassPathXmlApplicationContext ClassPath类路径加载:指的就是classes路径 6 ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); 7 8 //第二种:文件系统路径获得配置文件【绝对路径】 9 //ApplicationContext context = new FileSystemXmlApplicationContext("F:\\IdeaProjects\\spring-demo2\\out\\production\\spring-demo2\\beans.xml"); 10 11 //第三种:使用BeanFactory(了解) 12 //String path = "F:\\IdeaProjects\\spring-demo2\\out\\production\\spring-demo2\\beans.xml"; 13 //BeanFactory context = new XmlBeanFactory(new FileSystemResource(path)); 14 15 //IUserService userService = (IUserService) context.getBean("userService"); 16 //userService.add(); 17 }
=========================================
结果:UserServiceImpl()。。调用了
2.【ClassPathXmlApplicationContext】加载配置文件,调用getBean()时创建对象,延时加载
1 @Test() 2 public void test2() { 3 4 //Spring容器加载有三种方式 5 //第一种:ClassPathXmlApplicationContext ClassPath类路径加载:指的就是classes路径 6 //ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); 7 8 //第二种:文件系统路径获得配置文件【绝对路径】 9 //ApplicationContext context = new FileSystemXmlApplicationContext("F:\\IdeaProjects\\spring-demo2\\out\\production\\spring-demo2\\beans.xml"); 10 11 //第三种:使用BeanFactory(了解) 12 String path = "F:\\IdeaProjects\\spring-demo2\\out\\production\\spring-demo2\\beans.xml"; 13 BeanFactory context = new XmlBeanFactory(new FileSystemResource(path)); 14 15 //IUserService userService = (IUserService) context.getBean("userService"); 16 //userService.add(); 17 }
========================================
结果:空
================================================
调用getBean()
================================================
1 @Test() 2 public void test2() { 3 4 //Spring容器加载有三种方式 5 //第一种:ClassPathXmlApplicationContext ClassPath类路径加载:指的就是classes路径 6 //ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml"); 7 8 //第二种:文件系统路径获得配置文件【绝对路径】 9 //ApplicationContext context = new FileSystemXmlApplicationContext("F:\\IdeaProjects\\spring-demo2\\out\\production\\spring-demo2\\beans.xml"); 10 11 //第三种:使用BeanFactory(了解) 12 String path = "F:\\IdeaProjects\\spring-demo2\\out\\production\\spring-demo2\\beans.xml"; 13 BeanFactory context = new XmlBeanFactory(new FileSystemResource(path)); 14 15 IUserService userService = (IUserService) context.getBean("userService"); 16 userService.add(); 17 }
==========================================
结果:
UserServiceImpl()。。调用了
添加用户。。。alex
============================================================
♦spring内部创建对象原理
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> 5 <!--配置一个bean【对象】--> 6 <!--spring内部创建对象的原理 7 1.解析xml文件,获取类名,id,属性 8 2.通过反射,用类型创建对象 9 3.给创建的对象赋值 10 --> 11 <bean id="userService" class="com.gyf.service.UserServiceImpl"> 12 <!--依赖注入数据,调用属性的set方法--> 13 <property name="name" value="alex"></property> 14 </bean> 15 </beans>