3.spring环境搭建

1. 导入jar
    1.1 四个核心包一个日志包(commons-logging)
2. 在src 下新建applicationContext.xml
    2.1 文件名称和路径自定义.
    2.2 记住Spring 容器ApplicationContext,applicationContext.xml 配置的信息最终存储到了AppliationContext 容器中
    2.3 spring 配置文件是基于schema
            2.3.1 schema 文件扩展名.xsd
            2.3.2 把schema 理解成   .
                2.3.2.1 比DTD 具备更好的扩展性.
            2.3.3 每次引入一个xsd 文件是一个namespace(xmlns)
    2.4 配置文件中只需要引入基本schema
        2.4.1 通过<bean/> 创建对象.
        2.4.2 默认配置文件被加载时创建对象.
<?xml version="1.0" encoding="UTF-8"?>
<beans
    xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd">
<!-- id 表示获取到对象标识
class 创建哪个类的对象
-->
<bean id="peo" class="com.bjsxt.pojo.People"/>
</beans>
3. 编写测试方法
    3.1 getBean(“<bean>标签id 值”,返回值类型);如果没有第二个参数,默认是Object
    3.2 getBeanDefinitionNames(),Spring 容器中目前所有管理的所有对象.
 
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
People people = ac.getBean("peo",People.class);
System.out.println(people);
//查看spring管理了哪些类
// String[] names = ac.getBeanDefinitionNames();
// for (String string : names) {
// System.out.println(string);
// }
posted on 2019-03-19 09:17  一只猪儿虫  阅读(176)  评论(0编辑  收藏  举报