Spring
核心:
IOC(控制反转)
  --将控制管理权不由JavaBean管理,交给Spring容器管理
DI(依赖注 

 --分层
    --上层依赖于下层(栗子:Dao层服务于Service服务于Action)
    --下层服务于上层)

Spring环境搭建

  1.下载Spring框架

  下载地址:http://repo.spring.io/release/org/springframework/spring/

  2.创建web项目 导入Jar包

  

  2.在src目录创建spring配置文件

  --applicationContext.xml(默认配置文件名称)
   or--spring.xml(可自定义名称)

<?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/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!--将JavaBean的控制权交由Spring管理-->
    <bean name="DataBean" class="com.spring.model.DataBean" scope="prototype">
    <property name="name" value="Spring" />
</bean >
</beans>

  3.创建bean

    bean的单例&多例
        scope="singleton"[单例] | "prototype"[多例]
        使用场合:在对象经常发生改变时,要使用多例

<bean name="hello(key)" class="com.xxx.xxx(类全名)" scope="singleton(是否单例 默认是)" >
          <!-- 属性注入-->
          <property name="name" value="Spring" />
      </bean>

  4.测试

    * ApplicationContext
          --在加载Spring配置文件时就创建了bean
         * BeanFactory
          --在使用了某个bean时才创建

@Test
        public void test01(){
            //加载spring.xml
    //        ApplicationContext ac = new ClassPathXmlApplicationContext("spring.xml");
            BeanFactory factory = new ClassPathXmlApplicationContext("spring.xml");
    //        DataBean dataBean =(DataBean) factory.getBean("DataBean");
            //获取bean
            DataBean dataBean= factory.getBean("DataBean",DataBean.class);
            System.out.println(dataBean.getName());
        }

  5.运行结果:Spring