`
hht_mmtchina
  • 浏览: 11595 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

spring核心及bean的作用域

阅读更多
[size=large]Spring 问题:
1.spring核心:IOC和AOP  IOC容器就是依赖注入 在程序中不需要new关键字来实例对象,通过接口的引用,然后通过某种方式把接口的某个实现类的实例注入到引用里,从而实现接口与具体实现类的松耦合。
    通过ioc容器是通过xml直接的关系来对需要实例的对象进行注入从而有容器产生一个实例对象。传统的是在代码里控制,现在由容器来管理 即控制权的转换(DI反转控制)

   AOP(面向切面变成)他允许程序员对横切关注点进行模块化。比如登录时候,在进入登录页面前写入日志,很常用的,尤其是跟数据库有关的,或者跟支付有关的程序肯定会在每一步前面插入日志。AOP 的核心构造是方面,它将那些影响多个类的行为封装到可重用的模块中。

  Spring Bean的作用域:Singleton、Prototype request Session、globalSession
     (1)Singleton 当一个bean的作用域为singleton, 那么Spring IoC容器中只会存在一个共享的bean实例,并且所有对bean的请求,只要id与该bean定义相匹配,则只会返回bean的同一实例。
<bean id="accountService" class="com.foo.DefaultAccountService"/>

<!-- the following is equivalent, though redundant (singleton scope is the default) -->
<bean id="accountService" class="com.foo.DefaultAccountService" scope="singleton"/>

<!-- the following is equivalent, though redundant (and preserved for backward compatibility) -->
<bean id="accountService" class="com.foo.DefaultAccountService" singleton="true"/>

     (2)Prototype 当一个bean的作用域是Protopye时,那么在每次请求时对该bean都会产生一个新的bean的实例。根据经验,对所有有状态的bean应该使用prototype作用域,而对无状态的bean则应该使用 singleton作用域 。
<bean id="accountService" class="com.foo.DefaultAccountService" scope="prototype"/>
<bean id="accountService" class="com.foo.DefaultAccountService" singleton="false"/>

     (3)a.Request作用域:
在一次HTTP请求中,一个bean定义对应一个实例;即每次HTTP请求将会有各自的bean实例,它们依据某个bean定义创建而成。该作用域仅在基于web的Spring ApplicationContext情形下有效。
<bean id="userPreferences" class="com.foo.UserPreferences" scope="session"/>

b. Request作用域
在一个HTTP Session中,一个bean定义对应一个实例。该作用域仅在基于web的Spring ApplicationContext情形下有效。
<bean id="userPreferences" class="com.foo.UserPreferences" scope="session"/>


c.global session作用域
在一个全局的HTTP Session中,一个bean定义对应一个实例。典型情况下,仅在使用portlet context的时候有效。该作用域仅在基于web的Spring ApplicationContext情形下有效。
<bean id="userPreferences" class="com.foo.UserPreferences" scope="globalSession"/>


aop相关的内容请看下面的连接:http://my.oschina.net/roockee/blog/102752[/size]
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics