Spring的学习之路(二)的Spring详细配置bean的基础配置

2023-05-16

 

 

1 xml的提示配置

1.1 Schema的配置

 

<?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">
	<!-- Spring的入门配置 -->
	<bean id = "UserDAO" class = "com.heshihua.spring.demo1.UserDAOImpl">
		<property name="name" value="李四"></property>
	</bean>
</beans>

将xml文件中的http://www.springframework.org/schema/beans/spring-beans.xsd复制下来

打开window->Perferences如下图

将前面复制的地址粘贴到key那一栏

然后点击 File System..选择正确的文件

spring-framework-4.2.4.RELEASE-schema这个文件自行从网上下载

Key Type选择Schema location

具体配置如下图

1.2 Bean的相关配置

1.2.1 bean的id和name配置

id 使用了唯一约束,并且里面不可以使用特殊字符。

name 没有使用唯一约束,理论上可以重复但是在实际开发当中不能重复,但是里面可以出现特殊字符。

以后当把Spring和Struts1框架整合的时候

struts1要求要加上“\”<bean name = "/user" calss = ""/>

很明显id是不能满足的因而只能使用name

还好在Spring中di和name的效果是一样的

1.2.2 bean的生命周期的配置

 还是先编写一个接口

package com.heshihua.spring.demo2;

public interface CustomerDAO {
	public void save();
}

编写类继承接口

package com.heshihua.spring.demo2;

public class CustomerDAOImpl implements CustomerDAO{
	public void setup() {
		System.out.println("CustomerDAOImpl被初始化了。。。。。。。");
	}
	public void save() {
		System.out.println("CustomerDAOImpl的save方法执行了。。。。。");
	}
	public void destroy() {
		System.out.println("CustomerDAOImpl被销毁了。。。。。。");
	}
}

编写bean

<!-- Spring的bean的生命周期的配置 -->
	<bean id = "customerDAO" class = "com.heshihua.spring.demo2.CustomerDAOImpl" init-method = "setup" destroy-method = "destroy"></bean>

编写测试类

package com.heshihua.spring.demo2;

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

public class SpringDome2 {
	/*
	 * 生命周期的配置
	 * **/
	@Test
	public void demo1() {
		ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
		CustomerDAO customerDAO = (CustomerDAO) applicationContext.getBean("customerDAO");
		customerDAO.save();
		applicationContext.close();
	}
}

 

运行结果如下图

init-method在你在创建customerDAO对象的时候,就会自动调用其所对应的setup方法

destroy-method在你在销毁customerDAO对象的时候,就会自动调用其所对应的destroy方法(当bean是单利模式的时候);

1.2.3 bean的作用范围的配置(重点)

scope  配置bean的作用范围

    singleton       :默认的,Springhi使用单例模式创建这个对象

    prototype       :多例模式(在整合struts2的时候会用到)

    request          :应用在web项目中,Spring创建这个类会将这个类存入到request的范围中

    session          :应用在web项目中,Spring创建这个类会将这个类存入到session的范围中

    globalsession :应用在web项目中,必须在porlet环境中使用。如果没有这中环境就相当于session的作用

测试默认是否是单例模式、

	@Test
	public void demo2() {
		ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
		CustomerDAO customerDAO1 = (CustomerDAO) applicationContext.getBean("customerDAO");
		CustomerDAO customerDAO2 = (CustomerDAO) applicationContext.getBean("customerDAO");
		System.out.println(customerDAO1);
		System.out.println(customerDAO2);
		System.out.println(customerDAO1==customerDAO2);

	}

运行结果如下图

 

结果证明他只实例化了一次

俩个对象实际上是一个对象,地址也是一样的

set-up函数只运行了一次

所以说默认是单例模式

如果把scope配置成singleton

结果是一样的这里不上结果图了没意义(爱信不信哈哈)

如果把scope配置成prototype

如图所示变成多例模式了

在这里如果调用applicationContext.close()会发现不会销毁成功。因为存在多个对象他不知道取销毁哪一个。

 

 

 

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Spring的学习之路(二)的Spring详细配置bean的基础配置 的相关文章

  • libnet的使用详解

    最近搬砖需要对libnet进行介绍在这里对知识进行汇总 1 libnet简介 在 libnet 出现以前 xff0c 如果要构造数据包并发送到网络中 xff0c 程序员要通过一些复杂的接口来处理 libnet 的出现 xff0c 为程序员提
  • electron启动报错

    一个简单的程序启动居然报错了 xff0c 那就排查原因吧 xff0c 搜索网上的资料没有一个一样的 xff0c 大致类似的说的是electron版本和node不一致 还有说是electron的版本问题 但排查后都不是 xff0c 最后我把m
  • 手把手使用 Egg+TypeScript+mongoDB快速实现增删改查

    创建一个Egg的TS项目 xff08 Egg js官方教程 xff09 安装MogoDB Egg 依赖 span class token function npm span span class token function install
  • 小白学java——做一个歌手比赛系统(一)

    xfeff xfeff 完整代码加实验报告都在https download csdn net download qq 39980334 11232331 我已经设置成0积分下载了 xff0c 有需要的自行下载 xff0c 有问题的多看看代码
  • 使用尾插法创建链表并打印输出

    include lt stdio h gt include lt stdlib h gt include lt string h gt typedef struct LNode struct LNode next int data LNod
  • RabbitMQ 发展史与安装

    RabbitMQ 天降奇兵 解决的实例1 xff1a 统计用户的行为 xff0c 利用消息队列解耦模块和认证服务器 xff0c 认证模块被设计为 xff0c 在每一次请求页面的时候 xff0c 发送一条认证请求消息到rabbitmq xff
  • 小白学java------做一个歌手比赛系统(二)

    完整代码加实验报告都在 https download csdn net download qq 39980334 11232331 我已经设置成0积分下载了 xff0c 有需要的自行下载 xff0c 如果页面打不开可能还在审核中 xff08
  • 网络爬虫入门

    1 网络爬虫 网络爬虫 xff08 Web crawler xff09 xff0c 是一种按照一定的规则 xff0c 自动地抓取万维网信息的程序或者脚本 1 1 爬虫入门程序 1 1 1 环境准备 JDK1 8IntelliJ IDEAID
  • Java API入门篇

    文章目录 1 API1 1 API概述1 2 如何使用API帮助文档 2 String类2 1 String类概述2 2 String类的特点2 3 String类的构造方法2 4 创建字符串对象两种方式的区别2 5 字符串的比较2 5 1
  • Java异常篇

    文章目录 1 异常2 JVM默认处理异常的方式3 try catch方式处理异常4 Throwable成员方法5 编译时异常和运行时异常的区别6 throws方式处理异常7 throws和throw的区别8 自定义异常 1 异常 异常的概述
  • Java集合篇

    文章目录 1 Collection集合1 1 集合体系结构1 2 Collection集合概述和基本使用1 3 Collection集合的常用方法1 4 Collection集合的遍历1 5 集合使用步骤图解1 6 集合的案例 Collec
  • macOS下安装JDK11和配置环境变量

    1 下载 官网下载地址 华为云下载JDK11地址 tar包或者dmg xff0c 二者区别在于 xff1a tar你自己解压 xff0c 放在你想要的地方 xff08 配置JAVA HOME的时候是你自己选的位置 xff01 xff09 d
  • Java文件操作、IO流

    文章目录 1 File类1 1 File类概述和构造方法1 2 File类创建功能1 3 File类判断和获取功能1 4 File类删除功能 2 递归2 1 递归2 2 递归求阶乘2 3 递归遍历目录 3 IO流3 1 IO流概述和分类3
  • Java多线程

    文章目录 1 实现多线程1 1 进程和线程1 2 实现多线程方式一 xff1a 继承Thread类1 3 设置和获取线程名称1 4 线程优先级1 5 线程控制1 6 线程的生命周期1 7 实现多线程方式二 xff1a 实现Runnable接
  • Java编程思想(第4版)习题答案

    https span class token operator span span class token operator span span class token operator span greggordon span class
  • Bootstrap给表格设置宽度不起作用

    更改名称为table的class 将table layout属性设置为fixed span class token selector table span span class token punctuation span span cla
  • C++作用域运算符“::“的作用

    1 当存在具有相同名称的局部变量时 xff0c 要访问全局变量 include lt iostream gt using namespace std int x Global x int main int x 61 10 Local x c
  • HTML实现鼠标拖动元素排序

    1 简介 拖放 xff08 Drag和 drop xff09 是 HTML5 标准的组成部分 xff0c 为了使元素可拖动 xff0c 必须把 draggable 属性设置为 true xff0c 整个拖拽事件触发的顺序如下 xff1a d
  • MacOS安装svn客户端

    一 安装Homebrew环境 打开终端 输入以下命令 bin zsh c span class token string 34 span class token variable span class token variable span
  • MySql数据库root账户无法远程登陆解决办法

    最近换了新的腾讯云服务器后 xff0c 通过宝塔面板安装了mysql 数据库 xff0c 之后使用root用户通过navicat远程连接登录不了 解决办法如下 两行代码ok MySQL5 7和MySql8 开启root 用户远程访问 mys

随机推荐