ssh(sturts2_spring_hibernate) 框架搭建之spring

 

一、spring总结:

  ⑴、spring是一个轻量级的JAVA开发框架,主要的作用是用来管理实例(可以解决JAVA类中new对象的问题,节省内存资源。)和降低代码之间的耦合性,促进代码模块化。

  ⑵、促进代码的模块化学也就是所说的IOC(Inversion Of Control)。然其中DI用的较为广泛。

二、spring搭建:

  ⑴、导入相关的jar包:

    ①、下载好spring后解压出来找到lib目录将其中除了javaDoc和sources的jar包全部导入项目中,并且在struts解压包lib目录中找到struts2-spring-plugin的jar包和Commons-logging的jar包导入项目中。如下:

    

    ②、在web.xml中进行相关的文件配置:

      

    ③、在struts.xml中进行声明:目的地是所有的实例创建都交给spring去做(使用spring来管理实例):

       

    ④、在项目src中新建一个xml文件即applicationContext.xml,在这里进行spring的bean的配置:

      ⑴、首先需要进行beans标签。beans标签中是基本的配置头信息,基本上不会改变,除了版本之间的差异之外需要改一下版本信息,其他基本变化不大。  

    <beans xmlns="http://www.springframework.org/schema/beans" 
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
      xmlns:p="http://www.springframework.org/schema/p" 
                xmlns:aop="http://www.springframework.org/schema/aop" 
      xmlns:context="http://www.springframework.org/schema/context" 
      xmlns:jee="http://www.springframework.org/schema/jee" 
      xmlns:tx="http://www.springframework.org/schema/tx" 
      xsi:schemaLocation=" 
      http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd 
      http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd 
      http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
      http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.2.xsd 
      http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">

        

       <!--之后在这里进行bean实例的配置。-->

    <beans>

       ⑵、其次配置bean实例中进行,下面是示例的配置:

        

         · bean标签中的id属性是bean的名字,可能一个实例中需要用到另外一个实例,就需要引用,这个id就是引用的名字。

         · bean标签中的class属性是指向的这个类,也就是会对这个类进行new实例,但是不是自己作,而是bean帮我们解决。  

         · bean标签中的scope是设置实例的创建形式,如:

              singleton:单实例,在bean无状态使用。

              prototype:每次对这个bean的请求都会创建一个实例。

              request:每次http请求将会有各自的bean实例,类似于prototype。

              session:在一个http session中,一个bean定义对应一个bean实例。

               global session:在一个全局的http session中,一个bean定义对应一个bean实例。典型情况下,仅在使用portlet context的时候有效。

           

        ① 、这行代码的意思是:首先,在action.ActionService类中需要有一个成员属性为ms,定义了这个属性的set方法(spring会帮忙注入这个实例),并且类型是一个services.MyServiceImp的类型或者是这个类的接口类型,如下:

  

         其次,这个属性引用了下面的bean的id为myServiceImp的实例。

         最后,这个句最终的作用就是为这个ms成员属性注入了一个myServiceImp的实例。

        

        ②、 这句代码的意思则是:new出了一个util.MyConnectionImp的实例,并且每次请求这个bean的时候都会new一个这个实例。


    讲解结束,谢谢浏览!

原文地址:https://www.cnblogs.com/demoMeng/p/5844926.html