Spring入门(3)-Spring命名空间与Bean作用域

这篇文章主要介绍Spring的命名空间和Bean作用域

0. 目录

  1. Spring命名空间
  2. Bean作用域

1. Spring命名空间

在前面的文章中,Spring的配置文件中定义了beans,除了beans之外,Spring还定义了其他的命名空间,如下:

命名空间 用途
aop 为声明切面以及将@AspectJ注解的类代理为Spring切面提供了配置元素
beans 支持声明Bean和装配Bean,是Spring最核心也是最原始的命名空间
context 为配置Spring应用上下文提供了配置元素,包括自动检测和自动装配Bean、注入非Spring直接管理的对象
jee 提供了与Java EE API的集成,例如JNDI和EJB
jms 为声明消息驱动的POJO提供了配置元素
lang 支持配置由Groovy、JRuby或BeanShell等脚本实现的Bean
mvc 启动Spring MVC的能力,例如面向注解的控制器、视图控制器和拦截器
oxm 支持Spring的对象到XML映射配置
tx 提供声明式事务配置
util 提供各种各样的工具类元素,包括把集合配置为Bean、支持属性占位符元素

AOP例子

<aop:aspectj-autoproxy proxy-target-class="true"/>

context例子

<context:annotation-config />

2. Bean作用域

所有的Spring Bean默认都是单例。但除了单例之外,我们可能有别的需求,比如说多个实例。在Bean的声明中,把scope设置为prototype即可,如下:

<?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 name="PersonBll" class="com.chzhao.springtest.PersonBll"
scope="prototype"></bean>
</beans>

除了prototype之外,Spring还定义了另外的作用域,如下表所示。

作用域 定义
singleton 单例
prototype 每次调用创建一个实例
request 一次http请求对应一个实例
session 一个session对应一个实例
gobal-session 在全局的http session中,每个bean定义对应一个实例

Spring入门(3)-Spring命名空间与Bean作用域的更多相关文章

  1. Spring入门2. IoC中装配Bean

    Spring入门2. IoC中装配Bean 20131125 前言: 上一节学习了Spring在JavaProject中的配置,通过配置文件利用BeanFactory和ApplicationConte ...

  2. Spring入门(4)-注入Bean属性

    Spring入门(4)-注入Bean属性 本文介绍如何注入Bean属性,包括简单属性.引用.内部Bean.注入集合等. 0. 目录 注入简单值 注入引用 注入内部Bean 装配集合 装配空值 使用命名 ...

  3. Spring入门(二):自动化装配bean

    Spring从两个角度来实现自动化装配: 组件扫描(component scanning):Spring会自动发现应用上下文中需要创建的bean. 自动装配(autowiring):Spring会自动 ...

  4. Spring课程 Spring入门篇 2-1 IOC和bean容器

    课程链接: 本节讲了5部分内容,6为项目demo: 1 接口及面向接口编程 2 什么是IOC 3 Spring的bean配置 4 Bean的初始化 5 Demo 自己理解: 1 高层模块和底层模块都依 ...

  5. spring入门(六) spring mvc+mybatis

    1.引入依赖 <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis --> <dependency> < ...

  6. spring入门(五) spring mvc+hibernate

    核心是让SessionFactory由Spring管理 1.引入依赖 <!-- https://mvnrepository.com/artifact/org.springframework/sp ...

  7. spring入门(四) spring mvc返回json结果

    前提:已搭建好环境 1.建立Controller package com.ice.controller; import com.ice.model.Person; import org.springf ...

  8. spring入门(八) spring mvc设置默认首页

    1.web.xml配置如下 <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3// ...

  9. spring入门(七) spring mvc+mybatis+generator

    1.Mybatis-Generator下载 地址:https://github.com/mybatis/generator/releases 我使用的是 mybatis-generator-core- ...

随机推荐

  1. 【HDOJ】2774 Shuffle

    1. 题目描述有长度为$n \in [1, 10^5]$的序列,表示一个打乱的循环排列,即每当$[1 \cdots n]$中的数字全部出现后,再重新产生一个随机的覆盖$[1 \cdots n]$的序列 ...

  2. 购买使用Linode VPS必须知晓的十个问题

    Linode是国外非常著名的VPS商之一,目前在国内站长圈中备受推崇.有许多站长已经购买了Linode VPS,但是部分站长由于中英语言不通,对Linode的政策不了解,从而造成了许多不必要的损失.本 ...

  3. 8天学通MongoDB——第六天 分片技术

    在mongodb里面存在另一种集群,就是分片技术,跟sql server的表分区类似,我们知道当数据量达到T级别的时候,我们的磁盘,内存 就吃不消了,针对这样的场景我们该如何应对. 一:分片 mong ...

  4. 宏os_file_read_func

    # define os_file_read(file, buf, offset, offset_high, n) \ os_file_read_func(file, buf, offset, offs ...

  5. javascript倒计时代码

    其实就是用两个时间戳相减,余数转换为日期,就是所剩的年月日时分秒,不过年份-1970 $scope.timerID = null; $scope.timerRunning = false;$scope ...

  6. 【C#学习笔记】调用C++生成的DLL

    首先用vs2010建立win32项目,选择dll和空项目. 头文件add.h extern "C" __declspec(dllexport) int add(int a,int ...

  7. LeetCode: Reverse Words in a String && Rotate Array

    Title: Given an input string, reverse the string word by word. For example,Given s = "the sky i ...

  8. Tomcat 调优总结

    一. jvm参数调优 常见的生产环境tomcat启动脚本里常见如下的参数,我们依次来解释各个参数意义. export JAVA_OPTS="-server -Xms1400M -Xmx140 ...

  9. 【转载】两个Web.config中连接字符串中特殊字符解决方案

    userid =  test password = aps'"; 那么连接字符串的写法为: Provider=SQLOLEDB.1;Password="aps'"&quo ...

  10. hisi平台mii网络模式和rmii网络模式的uboot制作

    MII网络uboot编译说明 一:编译生成默认的uboot1. 进入到uboot目录a. cd /home/satan/Hi3518_SDK_V1.0.7.0/osdrv/uboot2. 新建临时文件 ...