Spring之IOC和单例、多例

spring之IOC

  1. 什么是IOC?
    IOC:控制反转,将对象的创建、销毁、初始化等一系列的生命周期的过程交给spring容器来处理
    在这里插入图片描述

搭建spring框架的过程:

第一步

新建一个Java Project,并建立一个lib文件夹,然后导入spring的jar包
在这里插入图片描述

第二步

书写主配置文件(applicationContext.xml)
不是必须这个名字,但是规范是这么写,一般大家都会遵守规范。
先建一个class这里叫黄鼠狼

package cn.java.ioc1;
/**
 * @Title:  YellowMouseWolf.java
 * @Package cn.java.ioc1
 * @author: Matthew
 * @date: 2019年3月6日 下午6:00:24
 */

public class YellowMouseWolf {

	public YellowMouseWolf() {
		System.out.println("YellowMouseWolf......一只黄鼠狼出生了");
	}
	
	public void behavior(){
		System.out.println("偷鸡");
	}
}

然后书写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"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
    
    <!-- bean:将一个类的对象创建过程交给spring容器
    	 class:指定类的具体路径
    	 id:唯一标识符
    -->
	<bean id="yellowMouseWolf" class="cn.java.ioc1.YellowMouseWolf"></bean>
	
</beans>

在这里插入图片描述

第三步

启动框架测试

package cn.java.ioc1;

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

/**
 * @Title:  Window.java
 * @Package cn.java.ioc1
 * @author: Matthew
 * @date: 2019年3月6日 下午6:01:58
 */

public class Window {

	public static void main(String[] args) {
//		YellowMouseWolf yellow = new YellowMouseWolf();
//		1.启动框架(context代表spring容器)
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");		
	}
}

结果为:
YellowMouseWolf…一只黄鼠狼出生了

spring的对象声明

在Window文件下面这样写

YellowMouseWolf yellowMouseWolf1 = (YellowMouseWolf)context.getBean("yellowMouseWolf");

getBean中的值,为bean中的id,id唯一。
调用方法直接用yellowMouseWolf1.behavior就可以了

spring中单例与多例

package cn.java.ioc1;

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

/**
 * @Title:  Window.java
 * @Package cn.java.ioc1
 * @author: Matthew
 * @date: 2019年3月6日 下午6:01:58
 */

public class Window {

	public static void main(String[] args) {
//		YellowMouseWolf yellow = new YellowMouseWolf();
//		1.启动框架(context代表spring容器)
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		
//		2.获取spring容器中创建的对象(通过id获取)
		YellowMouseWolf yellowMouseWolf1 = (YellowMouseWolf)context.getBean("yellowMouseWolf");
		YellowMouseWolf yellowMouseWolf2 = (YellowMouseWolf)context.getBean("yellowMouseWolf");		
//		yellowMouseWolf1.behavior();
		System.out.println(yellowMouseWolf1 == yellowMouseWolf2);
		
		
	}
}

结果为true
也就是说spring默认为单例模式,那我们想要多例怎么办尼
有两种方法:

第一种方法

在applicationContext.xml中再加一个bean声明

 	<bean id="yellowMouseWolf" class="cn.java.ioc1.YellowMouseWolf"></bean>
	<bean id="yellowMouseWolf222" class="cn.java.ioc1.YellowMouseWolf"></bean>

Window.java改写为

	public static void main(String[] args) {
//		YellowMouseWolf yellow = new YellowMouseWolf();
//		1.启动框架(context代表spring容器)
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		
//		2.获取spring容器中创建的对象(通过id获取)
		YellowMouseWolf yellowMouseWolf1 = (YellowMouseWolf)context.getBean("yellowMouseWolf");
		YellowMouseWolf yellowMouseWolf2 = (YellowMouseWolf)context.getBean("yellowMouseWolf222");		
//		yellowMouseWolf1.behavior();
		System.out.println(yellowMouseWolf1 == yellowMouseWolf2);
		
		
	}

结果为
YellowMouseWolf…一只黄鼠狼出生了
YellowMouseWolf…一只黄鼠狼出生了
false

第二种方法

我们可以在bean中增加一个scope属性,改变scope中的值来改变。
我们新建一个类叫Dog

package cn.java.singleton2;
/**
 * @Title:  Dog.java
 * @Package cn.java.singleton2
 * @author: Matthew
 * @date: 2019年3月6日 下午6:17:13
 */

public class Dog {
	
	public Dog() {
		System.out.println("一只单身狗诞生了");
	}
	
	public void behavior(){
		System.out.println("狗会叫");
	}
}

然后在applicationContext中写

<!-- 
		scope:用来控制spring容器产生对象的方式,可以为单例也可以为多例
		常用的值有:singleton(单例)、prototype(多例),默认值为单例
		不常用的有:request、session
	 -->
	<bean id="dog" class="cn.java.singleton2.Dog" scope="prototype"></bean>

新建一个Window

package cn.java.singleton2;

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

/**
 * Description:单例与多例
 * @Title:  Window.java
 * @Package cn.java.ioc1
 * @author: Matthew
 * @date: 2019年3月6日 下午6:01:58
 */

public class Window {

	public static void main(String[] args) {

//		1.启动框架(context代表spring容器)
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//		2.获取spring容器中创建的对象(通过id值获得)
		Dog dog1 = (Dog)context.getBean("dog");
		Dog dog2 = (Dog)context.getBean("dog");

		System.out.println(dog1 == dog2);
		
	}
}

运行结果为:
一只单身狗诞生了
一只单身狗诞生了
false

在这里插入图片描述

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值