1.导入jar包
1.1 四个核心包和一个日志包(commons-logging)
2.在src下新建applicationContext.xml
2.1 文件名称和路径可以自定义
2.2 记住Spring容器ApplicationContext,applicationContext.xml配置的信息最终存储到了ApplicationContext容器中
2.3 spring 配置文件是基于 schema
2.3.1 schema 文件扩展名.xsd
2.3.2 把 schema 理解成 DTD 的升级版.
2.3.2.1 比 DTD 具备更好的扩展性.
2.3.3 每次引入一个 xsd 文件是一个 namespace(xmlns)
2.4 配置文件中只需要引入基本 schema(配置文件的头部分可以在Spring帮助文档中Ctrl + F搜索 xmlns)
2.4.1 通过<bean/>创建对象.
2.4.2 默认配置文件被加载时创建对象.
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans 3 xmlns="http://www.springframework.org/schema/beans" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xsi:schemaLocation="http://www.springframework.org/sc 6 hema/beans 7 http://www.springframework.org/schema/beans/spring-be 8 ans.xsd"> 9 <!-- id 表示获取到对象标识 10 class 创建哪个类的对象 11 --> 12 <bean id="peo" class="com.bjsxt.pojo.People"/> 13 </beans>
3. 编写测试方法
3.1 getBean("<bean>标签的id值", 返回值类型);如果没有第二个参数的话,就默认为Object
3.2 getBeanDefinitionNames(), 获取Spring容器中目前管理的所有对象的名称
1 ApplicationContext ac = new 2 ClassPathXmlApplicationContext("applicationContext.xm 3 l"); 4 People people = ac.getBean("peo",People.class); 5 System.out.println(people); 6 // String[] names = ac.getBeanDefinitionNames(); 7 // for (String string : names) { 8 // System.out.println(string); 9 // }
原文:https://www.cnblogs.com/stitchZsx/p/9846587.html