Java技术专题

spring入门

2018-04-27  本文已影响18人  爱撒谎的男孩

SSM框架

作用

使用

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

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>4.3.12.RELEASE</version>
</dependency>
<!-- 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获取类中不存在无参构造方法,但是存在静态工厂方法类的对象

    <!-- 通过静态工厂方法创建对象
        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();
    }

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

实例工厂方法

实例

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

<!-- 配置工厂类 -->
    <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)

    <!-- id: 自定义名称
        class : 需要spring管理的类的路径
     -->
    <bean id="date" class="java.util.Date" scope="prototype"></bean>

单例(Singleton)

懒加载

<bean id="date" class="java.util.Date" scope="singleton" lazy-init="true"></bean>

prototype

request

session

globalSession

Bean的延迟初始化

<bean id="date" class="java.util.Date"  lazy-init="true"></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>
上一篇下一篇

猜你喜欢

热点阅读