首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

Spring容易介绍

2012-09-27 
Spring简单介绍Spring的简单介绍最近学习了Spring,所以把我学到滴和大家一起分享一下,偶做了一点Spring简

Spring简单介绍
Spring的简单介绍
           最近学习了Spring,所以把我学到滴和大家一起分享一下,偶做

了一点Spring简介的介绍,还有就是一些比较简单的例子,希望对学习

Spring的同学有所帮助。小小建议:如果学习Spring给大家推荐一下,可以

看看尚学堂的视频,还有一本叫《Sping in Action》这本书,挺不错。在这

里指写了使用Spring的IOC容器管理Bean. 一些比较简单的例子。还有Spring

的很多知识没有分解,请关注下一次分解。

一.Spring简介

Spring 在英文里面有春天的意思,Spring是一个开源框架。框架的主要优势

之一就是其分层架构,分层 架构允许选择使用哪一个组件,同时为 J2EE 应

用程序开发提供集成的框架。Spring使用基本的JavaBean来完成以前只可能

由EJB(Enterprise JavaBean)完成的事情。然而,Spring的用途不仅限于服

务器端的开发。从简单性、可测试性和松耦合的角度而言,任何Java应用都

可以从 Spring中受益。

1.1    Spring的特点

Spring是一个轻量级的IOC和AOP容器。

IOC: 反向控制,设计好的类交给系统去控制,而不是在你的类内部控制(容

器在实例化对象时主动将其依赖类注入给它)

AOP: 面向切面,讲业务逻辑从系统服务中分离,实现内聚开发。系统对象只

做其该做滴业务逻辑不负责其他系统问题(如事务和日志)

DI :依赖注入,即在运行期由容器将依赖关系注入到组件之中,是在运行期,

由Spring根据配置文件,将其他对象的引用通过组件的提供的setter方法进

行设定。

1.2    依赖注入

依赖注入(DI)背后的基本原理是对象之间的依赖关系(即一起工作的其它

对象)只会通过以下几种方式来实现:构造器的参数、工厂方法的参数,或

给由构造函数或者工厂方法创建的对象设置属性。因此,容器的工作就是创

建bean时注入那些依赖关系。相对于由bean自己来控制其实例化、直接在构

造器中指定依赖关系或则类似服务定位器(Service Locator)模式这3种自

主控制依赖关系注入的方法来说,控制从根本上发生了倒转,这也正是控制

反转(Inversion of Control, IoC) 名字的由来。

二、Spring中如何加载多个配置文件

1. 第一种,使用数组

ApplicationContext contex=new ApplicationContest(bew String

[“a1.xml”,”a2.xml”]);

2. 第二种,只用通配符

ApplicationContext contex=new ApplicationContest(”a*.xml”);

   但此种方法只对文件系统中滴xml文件有效,针对jar包中的无效

3.第三种,引入

   ApplicationContext contex = new ClassXmlApplicationContext

(“a1.xml”);

   在al.xml中,执行resource为相对a1.xml路径的路径

*******************************************************************

**************************************

二:使用Spring的IoC容器管理Bean

目标:理解IOC原理;掌握Spring中使用依赖注入方式配置bean。

产品:MyEclipse 5.5、Tomcat 5.5

技术:spring

知识点:

?依赖注入的原理(控制反转)
?Spring中的依赖注入方式(2种)
?Spring中创建bean的3种方法
?bean高级特性深入
一、依赖注入的原理

     什么是IOC?控制反转与依赖注入

对象依赖的使用

public class Chinese {

   private Axe axe;

public void setAxe( Axe  axe){

    this.axe=axe;

}

   public void useAxe() {

      // TODO Auto-generated method stub

      System.out.println(axe.chop());

   }

    public Chinese(){

      System.out.println(“Chinese实例化!”);

    }

}




传统方式的缺点

1、对象直接new,不灵活

2、对象不能被别的组件共享对象

3、对象直接new,如果new时报异常,异常范围扩大了

举例说明:依赖注入类似于,相亲

    1)自己new 对象(传统方式)

2)由别人new对象,来找你(直接依赖注入)

3)由中介所为你提供一个对象(由工厂产生一个对象注入)

总结一下,大量组件生命周期和相互之间的关系有自己负责,就增加了系统

的复杂度,建议已装配的组装大量组件方式,把实例化好的对象注入进来。

改写程序

    添加set方法,不需要关心对象的实例化和具体实现,可以实现共享,注

入的是接口,可以使用模拟实现来测试,不需要真实的数据库。

增加了业务层的模型


2、spring的两种容器

u  BeanFactory

u  ApplicationContext

通过XML配置

不用关心组件的装配和创建

ApplicationContext所有对象在读去XMl时都被new一次,而且以后不再new

3、Bean的作用域

通过scope指定

  (默认)Singleton:仅一个实例

   prototype:每次都创建一个新实例

4、ICO的依赖关系图



二、Spring中的依赖注入方式(2种)

构造方法注入:保证在一次性注入

设置属性注入:可以一次注入一个,对于复杂的依赖关系,构造的过于臃肿

。推荐值依赖注入,可以看到是依赖

   <!– 默认在使用时,才注入依赖,也可以强制初始化bean,即设置

depends-on=”steelAxe”,提前注入–>

   <bean id=”chinese1″ >

   <!–设值注人:主要方式–>

   <constructor-arg>

     <ref local=”steelAxe”/>

   </constructor-arg>

   </bean>

   <bean id=”stoneAxe”></bean>

   <bean id=”steelAxe” ></bean>




三、Spring中创建bean的3种方法

u  调用构造器new一个bean实例

u  使用静态工厂

u  使用实例工厂

1、调用构造器new一个bean实例

<!– 默认在使用时,才注入依赖,也可以强制初始化bean,即设置depends

-on=”steelAxe”,提前注入–>

   <bean id=”chinese1″ >

   <!–设值注人:主要方式–>

      <property name=”axe”>

         <ref local=”steelAxe”/>

      </property>

   </bean>

   <bean id=”stoneAxe”></bean>

   <bean id=”steelAxe” ></bean>




2、使用静态工厂

工厂类

public class AxeFactory {

   public static Axe getAxe(String msg){

      if(msg.equals(“铁的”)){

        return new StealAxe();

      }else{

        return new StoneAxe();

      }

   }

}




注意 : <constructor-arg>

       <property>

applicationContext.xml

<bean id=”stoneaxe” factory-method=”getAxe”>

     <constructor-arg>

       <value>石头的</value>

     </constructor-arg>

   </bean>

   <bean id=”stealaxe” factory-method=”getAxe”>

     <constructor-arg>

       <value>铁的</value>

     </constructor-arg>

   </bean>

   <bean id=”chinese” scope=”prototype”>

    <property name=”axe”>

       <ref bean=”stealaxe”/>

    </property>

   </bean>




3、使用实例工厂

工厂类

public class AxeFactory {

   public Axe getAxe(String msg){

      if(msg.equals(“铁的”)){

        return new StealAxe();

      }else{

        return new StoneAxe();

      }

   }

}




applicationContext.xml

<!– 
   <bean  id=”stoneaxe”  class=”dao.StoneAxe”/>

   <bean id=”stealaxe”  class=”dao.StealAxe”/>

   –>

   <bean id=”factory” class=”factory.AxeFactory”/>

   <bean id=”stoneaxe” factory-bean=”factory” factory-method=”

getAxe”>

     <constructor-arg>

       <value>石头的</value>

     </constructor-arg>

   </bean>

   <bean id=”stealaxe” factory-bean=”factory” factory-method=”

getAxe”>

     <constructor-arg>

       <value>铁的</value>

     </constructor-arg>

   </bean>

   <bean id=”chinese” scope=”prototype”>

    <property name=”axe”>

       <ref bean=”stealaxe”/>

    </property>

   </bean>
  



四、bean特性深入

u  普通注入value值

注入基本类型:int,String 等

<property  name=”email”><value>11</value></property>

u  注入引用类型ref(也可以使用bean子标签)

<property  name=””><ref  local=/></property>

<property  name=””><ref  bean=/></property>

bean可以用于确定不同xml文档中的对象,local确定同一个xml文档

u  注入null

<property  name=”” ><null/></property>

u  注入List和数组

u  注入Set不能包含重复元素

u  注入Map

u  注入Properties

Chinese.java

 
public class Chinese{

   //下面是系列集合属性,分别表示学校、成绩、健康和斧子

   private List schools=new ArrayList();

   private Map  score=new HashMap();

   private Properties health=new Properties();

   private Set   axes=new HashSet();

   public Set getAxes() {

      return axes;

   }

   public Properties getHealth() {

      return health;

   }

   public List getSchools() {

      return schools;

   }

   public Map getScore() {

      return score;

   }

   public void setAxes(Set axes) {

      this.axes = axes;

   }

   public void setHealth(Properties health) {

      this.health = health;

   }

   public void setSchools(List schools) {

      this.schools = schools;

   }

   public void setScore(Map score) {

      this.score = score;

   }

}




applicationContext.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-2.0.xsd“>

   <bean id=”chinese”  >

       <property name=”schools”>

         <list>

            <value>小学</value>

            <value>中学</value>

            <value>大学</value>

         </list>

       </property>

       <property name=”score”>

         <map>

            <entry key=”数学”>

                 <value>87</value>

            </entry>

             <entry key=”英语”>

                 <value>89</value>

            </entry>

             <entry key=”语文”>

                 <value>82</value>

            </entry>

         </map>

       </property>

       <property name=”health”>

         <props>

            <prop key=”血压”>正常</prop>

            <prop key=”身高”>175</prop>

         </props>

       </property>

       <property name=”axes”>

         <set>

            <value>字符串斧子</value>

            <bean/>

            <ref local=”stoneAxe”/>

         </set>

       </property>

   </bean>

   <bean id=”stoneAxe”/>

</beans>

热点排行