SSM框架

作者: 爱撒谎的男孩 | 来源:发表于2018-10-07 22:22 被阅读5次

SSM框架

  • Spring + springMVC + mybatis

作用

  • 管理对象: 当开发人员需要某一个类的对象时,不需要自行new对象,而是通过spring直接获取即可

使用

【掌握】通过spring获取存在无参构造方法类的对象

  • 创建Maven Project

  • 当项目创建好之后,生成web.xml,解决默认提示错误

  • 选择tomcat, 项目右击 - > properties -> Target Runtimes

  • 打开 http://mvnrepository.com,搜索springwebmvc,在结果中找到的Grouporg.springframework,选择版本,并且复制xml代码

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>4.3.12.RELEASE</version>
</dependency>
  • 配置spring的配置文件(applicationContext.xml)
<!-- id: 自定义名称
        class : 需要spring管理的类的路径
     -->
    <bean id="date" class="java.util.Date"></bean>
  • 测试
import java.util.Date;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestDemo1 {
    public static void main(String[] args) {
        //spring的配置文件
        String conf="applicationContext.xml";
        //获取spring容器
        AbstractApplicationContext context=new ClassPathXmlApplicationContext(conf);
        //获取配置文件中指定的bean,参数是自定义的id
        Date date=(Date) context.getBean("date");
        //打印出日期,对象创建成功
        System.out.println(date);
        context.close();
    }
}

内存泄露或者内存溢出

  • 当需要释放某个对象所占用的内存空间时,如果对象没有正确关闭,将导致无法释放,由于这个对象可能已经没有了引用,这个对象再也无法使用,却一直被误认为被使用,就会变成长期存在于内存中的垃圾数据,就是内存泄露

  • 其实少量的内存泄露是灭有危害的。但是如果存在大量的内存泄露,就可导致可用内存明显变少,计算机的运行性能就会下降,当内存泄露到极点的时候,就会溢出。尽管少量的内存泄露是没有危害的,但是应该严谨的编程,尽量不要出现内存泄露

【了解】通过spring获取类中不存在无参构造方法,但是存在静态工厂方法类的对象

  • 我们使用spring获取java.util.Calendar的对象

  • factory-method : 这个属性指定的静态工厂方法

  • 在spring的配置文件中配置这个对象

    <!-- 通过静态工厂方法创建对象
        id : 自定义的名称
        class: 类的全路径
        factory-method : 静态工厂方法
     -->
    <bean  id="calendar" class="java.util.Calendar" factory-method="getInstance"></bean>
  • 测试
@Test
    public void testStatice() {
        // spring的配置文件
        String conf = "applicationContext.xml";
        // 获取spring容器
        AbstractApplicationContext context = new ClassPathXmlApplicationContext(
                conf);
        // 获取配置文件中指定的bean,参数是自定义的id
        Calendar calendar=(Calendar) context.getBean("calendar");
        // 打印出日期,对象创建成功
        System.out.println(calendar.getTime());
        context.close();
    }

【了解】类中不存在无参构造方法,也没有静态工厂方法,但是存在实例工厂方法

实例工厂方法

  • 实例工厂方法: 指另一个类中有工厂方法,可以获取目标类型的对象,即X类中有工厂方法(非静态的)可以获取Y类的对象

实例

  • 假设存在PhoneFactory类中,该类中有非静态方法getPhone()可以获取Phon类型的对象,并且Phone没有无参构造方法

  • Phone

public class Phone {
    public String name;
    public Phone(String name) {
        this.name=name;
    }
}
  • PhoneFactory
public class PhoneFactory {
    public Phone getPhone() {
        return new Phone("小米6");
    }
}

  • spring配置文件
    • factory-bean : 是工厂类的id
    • factory-method : 工厂类获取Phone对象的非静态的方法
<!-- 配置工厂类 -->
    <bean id="phoneFactory" class="cn.tedu.spring.beans.PhoneFactory"></bean>

    <!-- 配置Phone类的对象
        factory-bean : 是工厂类的id
        factory-method : 工厂类获取Phone对象的非静态的方法
     -->
    <bean id="phone" class="cn.tedu.spring.beans.Phone" factory-bean="phoneFactory" factory-method="getPhone"></bean>

Bean的作用域(Scope)

  • 默认情况下,由spring配置的对象是单例的
  • 在配置时,在<bean>节点添加scope属性即可调整,当该属性为singleton时是单例的,当属性为prototype为非单例的
    <!-- id: 自定义名称
        class : 需要spring管理的类的路径
     -->
    <bean id="date" class="java.util.Date" scope="prototype"></bean>

单例(Singleton)

懒加载

  • 在默认情况下,spring创建对象的是使用饿汉式,即是在spring配置文件开始加载的时候就创建对象,但是我们可以使用lazy-init取值我true的时候,就会使用懒加载(懒汉式)
<bean id="date" class="java.util.Date" scope="singleton" lazy-init="true"></bean>

prototype

  • 一个Bean定义对应多个对象实例

request

  • 在一次Http请求中,一个Bean只创建一个实例,仅限于web环境

session

  • 在一个HttpSession中,一个Bean定义对应一个实例

globalSession

  • 在一个全局的HttpSession中,一个bean定义对应一个实例

Bean的延迟初始化

  • 在spring创建Bean的实例的时候默认是使用单例,并且是饿汉式加载,即是在spring的配置文件在开始加载的时候就创建bean的实例对象

  • 但是我们可以使用lazy-init来延迟初始化,使用懒加载即可,当lazy-init为true的时候便是延迟加载

<bean id="date" class="java.util.Date"  lazy-init="true"></bean>
  • 我们还可以在<beans>根节点中添加一个default-lazy-init,可以为容器中的所有bean设置为懒加载
<beans default-lazy-init="true"></beans>

spring配置文件的全部约束

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
</beans>

相关文章

网友评论

    本文标题:SSM框架

    本文链接:https://www.haomeiwen.com/subject/tqvraftx.html