首页 技术 正文
技术 2022年11月18日
0 收藏 585 点赞 4,552 浏览 2039 个字

Spring入门篇 学习笔记

配置项

  • Id: 整个 IoC 容器中的唯一标识
  • Class: 具体实例化的类(必须配置项)
  • Scope: 作用域
  • Constructor arguments: 构造器参数
  • Properties: 属性
  • Autowiring mode: 自动装配模式
  • lazy-initialization mode: 懒加载模式
  • Initialization/destruction method: 初始化/销毁 方法

作用域

  • singleton: 单例(默认模式),指一个 Bean 容器中只存在一份
  • prototype: 每次请求(每次使用创建新的实例),destory 方式不生效
  • request: 每次 http 请求创建一个实例且仅在当前 request 內有效
  • session: 同上,每次 http 请求创建,当前 session 内有效
  • global session: 基于 portlet 的 web 中有效(portlet 定义了 global session),如果是在 web 中同 session

作用域示例

添加 BeanScope:

public class BeanScope {public void say() {
System.out.println("BeanScope say : " + this.hashCode());
}}

singleton

添加配置文件 spring-beanscope-singleton.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.xsd" > <bean id="beanScope" class="com.karonda.bean.BeanScope" scope="singleton"></bean> </beans>

添加测试 TestBeanScopeSingleton:

@RunWith(BlockJUnit4ClassRunner.class)
public class TestBeanScopeSingleton extends UnitTestBase {public TestBeanScopeSingleton() {
super("classpath*:spring-beanscope-singleton.xml");
}@Test
public void testSay() {
BeanScope beanScope = super.getBean("beanScope");
beanScope.say();BeanScope beanScope2 = super.getBean("beanScope");
beanScope2.say();
}}

prototype

添加配置文件 spring-beanscope-prototype.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.xsd" > <bean id="beanScope" class="com.karonda.bean.BeanScope" scope="prototype"></bean> </beans>

添加测试 TestBeanScopePrototype:

@RunWith(BlockJUnit4ClassRunner.class)
public class TestBeanScopePrototype extends UnitTestBase {public TestBeanScopePrototype() {
super("classpath*:spring-beanscope-prototype.xml");
}@Test
public void testSay() {
BeanScope beanScope = super.getBean("beanScope");
beanScope.say();beanScope = super.getBean("beanScope");
beanScope.say();
}}

源码:learning-spring

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,488
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,903
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,736
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,487
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,127
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,289