spring笔记1

一.概述
1.Spring是分层的Java SE/EE应用 full-stack轻量级开源框架,以IoC和AOP为内核,提供了展现层Spring MVC和持久层Spring JDBC以及业务层事务管理等众多的企业级应用技术,还能整合开源世界众多著名的第三方框架和类库,逐渐成为使用最多的Java EE企业应用开源框架。

2.Rod Johnson(spring之父)
     Expert One-to-One J2EE Design and Development(2002)
     阐述了J2EE使用EJB开发设计的优点及解决方案
     Expert One-to-One J2EE Development without EJB(2004)
     阐述了J2EE开发不使用EJB的解决方式(Spring雏形)

3.优势:
方便解耦,简化开发;
AOP编程的支持;
声明式事务的支持;
方便程序的测试;
方便集成各种优秀框架;
降低JavaEE API的使用难度;
Java源码是经典学习范例           

二.相关概念
耦合:类之间的依赖关系称为耦合;
控制反转-Inversion Of Control:将主动new对象的方式转变为通过工厂进行被动创建的方式,称为控制反转,是一种思想转变;
依赖注入:将属性值注入到属性中的具体技术,spring中有两种依赖注入方式:构造方法注入/set方式注入;


三.环境搭建
1.导包;
4+2:4个spring核心包,2个依赖包(阿帕奇的日志包logging/log4j)
图1

2.创建类
3.创建配置文件(applicationContext.xml)
举例:
  

  1   <?xml version="1.0" encoding="UTF-8"?>
  2      <!--引入约束  -->
  3      <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans"
  4          xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd ">
  5          <!-- 将user类交给spring管理
  6                 name:不可重复;
  7                  class:类的全限定名;
  8                  scope:指定spring中对象的作用的域
  9                     singleton(默认值):单例,对象在启动时创建;
 10                      prototype:原型多例,每次获得对象时,都创建新的对象;
 11          -->
 12      <!--以下注入为spring调用构造方法创建 /set方法注入-->
 13          <bean name="user1" class="huguangqin.com.cnblogs.domain.User">
 14              <property name="name" value="jack"></property>
 15              <property name="age" value="18"></property>
 16          </bean>

4.测试
   
  1  package huguangqin.com.cnblogs.demo;
  2      import org.junit.Test;
  3     import org.springframework.context.support.ClassPathXmlApplicationContext;
  4      public class demo1 {
  5          @Test
  6          public void demoTest() {
  7              // 创建spring容器
  8             ClassPathXmlApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
  9              // 从spring容器中取user对象
 10             Object bean = ac.getBean("user1");
 11              System.out.println(bean);
 12          }
 13      }
 14 
 15 

四.依赖注入配置(applicationContext.xml)
1.set方法注入
  1     <!-- 以下注入为spring调用构造方法创建或set方法注入 -->
  2     <bean name="user1" class="huguangqin.com.cnblogs.domain.User">
  3          <property name="name" value="jack"></property>
  4          <property name="age" value="18"></property>
  5      </bean>

2.set方法注入--p名称空间注入

  1     <!--约束中需添加:xmlns:p="http://www.springframework.org/schema/p" -->
  2     <bean name="user3" class="huguangqin.com.cnblogs.domain.User" p:name="rose" p:age="22" p:car-ref="car"></bean>

3.构造方法注入

  1      <!--以下为构造方法注入-->
  2     <bean name="user2" class="huguangqin.com.cnblogs.domain.User">
  3         <constructor-arg name="name" value="tom"></constructor-arg>
  4         <constructor-arg name="age" value="8"></constructor-arg>
  5          <constructor-arg name="car" ref="car"></constructor-arg>
  6      </bean>
  7      <bean name="car" class="huguangqin.com.cnblogs.domain.Car">
  8          <constructor-arg name="name" value="Benz"></constructor-arg>
  9          <constructor-arg name="color" value="red"></constructor-arg>
 10      </bean>

4.复杂类型注入
   

  1  <bean name="cb" class="huguangqin.com.cnblogs.domain.CollectionBean">
  2          <!--Object[] array -->
  3          <property name="array">
  4              <array>
  5                  <value>rose</value>
  6                  <ref bean="car"></ref>
  7              </array>
  8          </property>
  9 
 10         <!--List list-->
 11          <property name="list">
 12              <list>
 13                  <value>jack</value>
 14                  <ref bean="car"></ref>
 15              </list>
 16          </property>
 17 
 18          <!--Map map-->
 19          <property name="map">
 20              <map>
 21                  <entry key="lilei" value-ref="car"></entry>
 22                  <entry key-ref="car" value="hanmeimei"></entry>
 23              </map>
 24          </property>
 25 
 26          <!--Properties props-->
 27          <property name="props">
 28              <props>
 29                  <prop key="poli">波莉</prop>
 30                  <prop key="lisa">莉莎</prop>
 31              </props>
 32          </property>
 33      </bean>
 34 
 35     <bean name="car" class="huguangqin.com.cnblogs.domain.Car">
 36          <constructor-arg name="name" value="Benz"></constructor-arg>
 37          <constructor-arg name="color" value="red"></constructor-arg>
 38      </bean>
 39 

posted @ 2017-09-07 10:52  Guangqin_Hu  阅读(167)  评论(0编辑  收藏  举报