SSM框架(Spring + SpringMVC + MyBatis)学习笔记,第一课:Spring IoC

  1. Spring框架概述图
    这张图展示了,搭建项目时所需要的模块(对应的jar包),例如:使用spring框架连接数据库,则需要用到Core, Aop, Dao模块的jar包。
    Spring框架概述

  2. 下载使用eclipse搭建ssm框架(Spring + SpringMVC + MyBatis)所需要的jar包。这个jar包只能支持1.7及以下版本的JDK,若使用了高版本的JDK,需要去spring官网去下载jar包

    解压之后获得
    在这里插入图片描述
    本章主要介绍SpringIoC,所以只使用其中的ioc文件夹下的jar包和applicationContext.xml文件
    在这里插入图片描述
    在这里插入图片描述

  3. 创建web project
    打开eclipse,选择File >>> New >>> Dynamic Web Project
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    填写完项目名称之后,直接点击Finish,项目就创建完成了。
    创建之后的目录结构如下:
    在这里插入图片描述

  4. 打开下载的jar包,将ioc文件夹下的jar包复制到lib目录下,将applicationContext.xml文件复制到src目录下
    在这里插入图片描述
    到此,环境已经准备完成。

  5. 使用SpringIoC创建对象的三种方法
    src目录下创建一个class文件,用于接下来的测试
    在这里插入图片描述
    第一种方法:采用new构造器创建对象
    不使用框架

package org.springioc.test;

import java.util.Calendar;
import java.util.GregorianCalendar;

public class TestBean {
   

	public static void main(String[] args) {
   
		Calendar c1 = new GregorianCalendar();
		System.out.println("c1="+c1);

	}
	
}

使用框架
首先在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"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util"  
	xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:mvc="http://www.springframework.org/schema/mvc"
	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/util http://www.springframework.org/schema/util/spring-util-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/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">

<!-- 使用new构造器方法创建对象 -->
<bean id="c2" class="java.util.GregorianCalendar"></bean>

</beans>

然后在测试类TestBean中通过框架创建对象

package org.springioc.test;

import java.util.Calendar;
import java.util.GregorianCalendar;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class TestBean {
   

	public static void main(String[] args) {
   
		Calendar c1 = new GregorianCalendar();
		System.out.println("c1="+c1);
		
		String conf = "applicationContext.xml";//配置文件的文件名
		//实例化容器
		ApplicationContext ac = new ClassPathXmlApplicationContext(conf);
		//创建对象,
		//ac.getBean("c2", Calendar.class)
		//c2为配置文件applicationContext.xml中的id
		//Calendar.class为获取对象的类型
		Calendar c2222 = ac.getBean("c2", Calendar.class);
		System.out.println("c2222="+c2222);

	}
	
}

第二种方法:采用静态工厂方法创建对象
不使用框架
测试类TestBean

Calendar c3 = Calendar.getInstance();

使用框架
首先在applicationContext.xml中增加配置

<!-- 使用静态工厂方法创建对象 -->
<bean id="c3" class="java.util.Calendar" factory-method="getInstance"></bean>

测试类TestBean

Calendar c3333 = ac.getBean("c3", Calendar.class);
System.out.println("c3333="+c3333);

第三中方法:采用对象工厂方法创建对象
不使用框架
测试类TestBean

Date date = c3.getTime();
System.out.println("date="+date);

使用框架
首先在applicationContext.xml中增加配置,c3为第二种方法种配置的id

<!-- 采用对象工厂方法创建对象 -->
<bean id="date" factory-bean="c3" factory-method="getTime"></bean>

测试类TestBean

Date date3333 = ac.getBean("date", Date.class);
System.out.println("date3333="+date3333);
  1. 创建对象时单例,非单例控制
    使用以上方式创建对象,默认是单例(singleton),但是可以通过scope属性 更改,它的值有singleton(单例)和prototype(非单例)。
    下面举例说明:
    src目录下新建目录org.spingioc.bean,在此目录下新建类ExampleBean
package org.springioc.bean;

public class ExampleBean {
   

	public void execute() {
   
		System.out.println("调用execute方法");
	}

}

使用框架,创建对象
首先配置applicationContext.xml

<bean id="e1" class="org.springioc.bean.ExampleBean"></bean>

编写测试类ExampleTest

package org.springioc.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springioc.bean.ExampleBean;

public class ExampleTest {
   

	public static void main(String[] args) {
   
		String conf ="applicationContext.xml";
		ApplicationContext ac = new ClassPathXmlApplicationContext(conf);
		ExampleBean e1 = ac.getBean("e1", ExampleBean.class);
		ExampleBean e2 = ac.getBean("e1", ExampleBean.class);
		System.out.println(e1==e2);
	}

}

结果为true,代表为singleton模式。
applicationContext.xml增加scope属性,将它改为非单例模式

<bean scope="prototype" id="e1" class="org.springioc.bean.ExampleBean"></bean>

再次运行测试类ExampleTest,结果为false,代表为非单例模式。

  1. 创建对象时初始化动作
    不使用框架,可以使用构造器构造代码块实现对象的初始化
package org.springioc.bean;

public class ExampleBean {
   
	
	{
   
		System.out.println("构造代码块!");
	}
	
	public ExampleBean
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值