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. [UVA796]Critical Links(割边, 桥)

    题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  2. Git使用简介

    git创建分支并直接切换到分支:git checkout -b name git提交分支到远程服务器: git push origin name/git push origin name:name   ...

  3. Oracle默认的用户名和密码

    你是说默认的用户名和密码么scott 密码是 tigersys 密码是 change_on_installsystem 密码是 managersysman 密码是 oem_temp 其中直接管理模式可 ...

  4. overload和override

    Overload是重载的意思,Override是覆盖的意思,也就是重写. 重载Overload表示同一个类中可以有多个名称相同的方法,但这些方法的参数列表各不相同(即参数个数或类型不同). 重写Ove ...

  5. return File

    public ActionResult DownloadMessage() { string strExportData = "无数据!"; byte[] data = Syste ...

  6. BZOJ2298: [HAOI2011]problem a

    题目:http://www.lydsy.com/JudgeOnline/problem.php?id=2298 题解:刚开始思考的方向错了...一直在想LIS什么的,又发现不合法的情况不好判断,真是个 ...

  7. Oracle数据文件管理

    1.数据文件概述 Oracle数据库的数据文件(扩展名为DBF的文件)是用于保存数据库中数据的文件,系统数据.数据字典数据.临时数据.索引数据.应用数据等都物理地存储在数据文件中.用户对数据库中数据的 ...

  8. jQuery的datatable怎么才能给某一列添加超链接?

    aocolumeDef.这个里面去定义.return返回的字符串会代替原来cell里面的内容 e.g: datatable=$('#dt_basic').dataTable({ "bAuto ...

  9. linux - 开机启动thunderbird、chromium

    cd ~/.config/autostart/(没有autostart,自己 mkdir autostart) vim thunderbird.desktop,输入以下: [Desktop Entry ...

  10. Java [Leetcode 118]Pascal's Triangle

    题目描述: Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5 ...