当前位置: 移动技术网 > IT编程>开发语言>Java > Spring入门学习笔记(1)

Spring入门学习笔记(1)

2018年10月12日  | 移动技术网IT编程  | 我要评论

目录

该篇随笔,主要用于记录spring framework 基础知识。由于笔者是初学者,见识与能力有限,难免出现错误,如果发现错误,望不吝赐教。

spring好处

以下列出了使用spring framework的一些巨大好处

  • spring使开发人员能够使用pojo开发企业级应用程序。仅使用pojo的好处是您不需要ejb容器产品(如应用程序服务器),但您可以选择仅使用强大的servlet容器(如tomcat)或某些商业产品。

  • spring采用模块化方式组织。即使包和类的数量很大,你也只需要担心你需要的那些而忽略其余的。

  • spring并没有重新发明轮子,而是真正利用了一些现有技术,如几个orm框架,日志框架,jee,quartz和jdk计时器以及其他视图技术。

  • 测试用spring编写的应用程序很简单,因为依赖于环境的代码被移动到这个框架中。此外,通过使用javabeanstyle pojo,使用依赖注入来注入测试数据变得更加容易。

  • spring的web框架是一个设计良好的web mvc框架,它提供了一个很好的替代web框架,如struts或其他过度设计或不太流行的web框架。

  • spring提供了一个方便的api,用于将特定于技术的异常(例如,jdbc,hibernate或jdo抛出)转换为一致的,未经检查的异常。

  • 轻量级ioc容器往往是轻量级的,尤其是与ejb容器相比时。这有利于在具有有限内存和cpu资源的计算机上开发和部署应用程序。

  • spring提供了一致的事务管理接口,可以缩小到本地事务(例如,使用单个数据库)并扩展到全局事务(例如,使用jta)。

依赖注入

  • inversion of control
  • 编写java程序,尽可能独立于其他java类,增加重用这些类的可能性
  • 含义:a依赖于b类,意味着,b类将由ioc注入a类

面向面编程(aop)

  • 关键组件
  • 跨领域问题:日志记录,声明式事务,安全性,缓存等
  • aop中,模块化单元是面
  • aop可帮助您将交叉问题与它们所影响的对象分离
  • 允许定义方法拦截器和切入点,以便解耦。

spring framework

core container

  • beans
    • core,提供了框架的基本部分,包括ioc和依赖注入特征
    • bean提供了beanfactory,复杂的实现工厂模式
    • context由core和bean提供,它是访问任何定义和配置的对象的媒介。applicationcontext接口是上下文模块的焦点。
    • spel模块提供了一种强大的表达式语言,用于在运行时查询和操作对象图。

      data access/integration

  • jdbc
  • orm 模块为流行的对象关系映射api提供集成层,包括jpa、jdo、hibernate和ibatis
  • oxm 模块提供了一个抽象层,支持jaxb、castor、xmlbeans、jibx和xstream的对象/xml映射实现。
  • jms 包含用于生成和消费消息的特性。
  • transaction 模块为实现特殊接口的类和所有pojo支持编程和声明式事务管理

web

  • web web模块提供了基本的面向web的集成特性,例如多部分文件上传功能,以及使用servlet侦听器和面向web的应用程序上下文初始化ioc容器。
  • web-mvc 模块包含spring用于web应用程序的模型-视图-控制器(mvc)实现。
  • web-socket 模块在web应用程序中支持基于websocket的客户机和服务器之间的双向通信。
  • web-portlet 模块提供了在portlet环境中使用的mvc实现,并反映了web servlet模块的功能。

miscellaneous

  • aop模块提供了面向方面的编程实现,允许您将方法拦截器和切入点定义为实现应该分离的功能的干净解耦代码。
  • aspects模块提供了与aspectj的集成,这又是一个强大而成熟的aop框架。
  • instrumentation 模块提供了类插装支持和类装入器实现,用于某些应用服务器。
  • messaging模块支持stomp作为应用程序中使用的websocket子协议。它还支持一个注释编程模型,用于路由和处理来自websocket客户端的stomp消息。
  • test模块支持使用junit或testng框架测试spring组件

编写第一个程序

1.使用idea创建spring项目
2.创建helloworld文件

package top.ninwoo.learn;

public class helloworld {
    private string message;

    public void getmessage() {
        system.out.println("your message " + message);
    }

    public void setmessage(string message) {
        this.message = message;
    }


}

3.创建main函数

public class mainapp {
    public static void main(string[] args) {
        applicationcontext context = new classpathxmlapplicationcontext("beans.xml");
        helloworld obj = (helloworld) context.getbean("helloworld");
        obj.getmessage();
    }
}

使用classpathxmlapplicationcontext读取bean配置文件

4.配置文件

<?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">

    <bean id="helloworld" class="top.ninwoo.learn.helloworld">
        <property name="message" value="hello world!"/>
    </bean>
</beans>

property 传入了helloworld的参数

ioc容器

  • spring framework的核心
  • 容器船舰对象,并连接彼此,并管理其生命周期
  • di来管理组成应用程序的组件
  • 对象称为spring beans

spring ioc容器利用java pojo类和配置元数据来生成完全配置和可执行的系统或应用程序,配置元数据可以由:

  • xml
  • java注释或java代码表示

容器类型:

  • spring beanfactory: 最简单容器,目的向后兼容与spring集成的大量第三方框架
  • spring applicationcontext: 添加了更多企业特有的功能,比如能够解析来自属性文件的文本消息,
    以及能够向感兴趣的事件侦听器发布应用程序事件。

applicationcontext包含全部的beanfactory,beanfactory在小型化设备上仍可以用。

spring bean

构成应用程序主干并由spring ioc容器管理的对象称为bean.

bean的定义:

  • 如何创建bean
  • bean的生命周期细节
  • bean的依赖关系

参数:

  1. class :必须,指定创建的bean类
  2. name : 唯一指定bean标识符,xml中可以使用id/name来指定
  3. scope : 作用域
  4. construtor-arg : 用于注入依赖项
  5. properties : 用于注入依赖项
  6. autowiring mode : 用于注入依赖项
  7. lazy-initialization mode : 延迟初始化的bean告诉ioc容器在第一次请求时创建bean实例,而不是在启动时创建
  8. initialization method : 在容器设置了bean之后所有必要属性之后调用的回调。它将在bean生命周期章节中讨论
  9. destruction method : 当bean容器被销毁时使用的回调。

spring配置元数据

方法:

  • xml
  • 注解
  • java的配置
<?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-3.0.xsd">

   <!-- a simple bean definition -->
   <bean id = "..." class = "...">
      <!-- collaborators and configuration for this bean go here -->
   </bean>

   <!-- a bean definition with lazy init set on -->
   <bean id = "..." class = "..." lazy-init = "true">
      <!-- collaborators and configuration for this bean go here -->
   </bean>

   <!-- a bean definition with initialization method -->
   <bean id = "..." class = "..." init-method = "...">
      <!-- collaborators and configuration for this bean go here -->
   </bean>

   <!-- a bean definition with destruction method -->
   <bean id = "..." class = "..." destroy-method = "...">
      <!-- collaborators and configuration for this bean go here -->
   </bean>

   <!-- more bean definitions go here -->
   
</beans>

spring - bean scopes

当定义一个

spring framework 支持以下5种作用域,如果使用的是web-aware application只有三种可用。

  • singleton : 将bean定义范围限定为每个spring ioc容器的单个实例(默认)
  • prototype : 将bean定义范围限定为具有任意数量的对象实例
  • request : 将bean定义范围限定为http请求。仅在web感知spring applicationcontext的上下文中有效
  • session : 将bean定义范围限定为http会话。仅在web感知spring applicationcontext的上下文中有效。
  • global-session : 将bean定义范围限定为全局http会话。仅在web感知spring applicationcontext的上下文中有效。

singleton

只创建该bean定义的对象的一个实例。

<!-- a bean definition with singleton scope -->
<bean id = "..." class = "..." scope = "singleton">
   <!-- collaborators and configuration for this bean go here -->
</bean>

配置完成之后,如下的代码,有以下的效果。

      applicationcontext context = new classpathxmlapplicationcontext("beans.xml");
      helloworld obja = (helloworld) context.getbean("helloworld");

      obja.setmessage("i'm object a");
      obja.getmessage();

      helloworld objb = (helloworld) context.getbean("helloworld");
      objb.getmessage();

该程序打印:

i'm object a
i'm object a

这证明,实际只创建了一个bean对象,虽然调用了两次getbean,但返回的都是同一个对象。

prototype

每次调用bean,都会创建一个新的方法。

将上述的bean设置为prototype,执行同样的代码将会出现不同的效果。

该程序打印:

i'm object a
null

spring-bean life cycle

主要指的是,当bean被实例化时,可能需要执行一些初始化以使其进入可用状态。类似地,当不再需要bean并从容器中移除bean时,可能需要进行一些清理。

中间也存在一些活动,本章重点讨论以下两个声明周期回调方法。

  • initmethod
  • destroy-method

初始化回调

org.springframework.beans.factory.initializingbean已经为我们提供了开发接口。

void afterpropertiesset() throws exception;

这样我们就可以通过实现上面的接口实现初始化工作

public class examplebean implements initializingbean {
   public void afterpropertiesset() {
      // do some initialization work
   }
}

另外,对于xml的配置元数据,可以通过init-method指定具有void无参数的方法

<bean id = "examplebean" class = "examples.examplebean" init-method = "init"/>

销毁回调

org.springframework.beans.factory.disposablebean也已经提供好了接口

void destroy() throws exception;

这样,我们可以通过实现该接口实现销毁的工作

public class examplebean implements disposablebean {
   public void destroy() {
      // do some destruction work
   }
}

注意:如果想要使该函数起作用,需要注册registershutdownhook()方法。

同样,基于xml配置元数据,可以使用destroy-method指定具有void的无参方法

<bean id = "examplebean" class = "examples.examplebean" destroy-method = "destroy"/>

建议还是使用xml的方式,更加灵活。

默认的初始化和销毁方法

如果很多bean都需要同名的初始化和销毁方法,可以在

<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-3.0.xsd"
   default-init-method = "init" 
   default-destroy-method = "destroy">

   <bean id = "..." class = "...">
      <!-- collaborators and configuration for this bean go here -->
   </bean>
   
</beans>

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网