spring 的 bean 配置

         spring 框架对于 J2EE 来说是个伟大的创举, 至今做的两个项目都离不开 spring ,关于 spring ,我深知自己还有很多要学习的地方,最近开始在从头研究 spring, 本文主要说明 spring 中 bean 的配置。

 一、bean 的定义

        bean 的定义通常在带有 spring 头的 xml 文件中, 类似于 

    code 1.0

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
	http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd 
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
	http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd"
	default-autowire="byName">

<bean id="bean1" class="com.wind.bean1" />

<bean id="bean2" class="com.wind.bean2">
  <constructor-arg  value="jack"/>
  <constructor-arg  value=23 />
</bean>

<bean id="bean3" class="com.wind.bean3">
  <property name="name" value="jack"/>
  <property name="age"  value=23 />
  <property name="bean2" ref="bean2" />
</bean>

</beans>

 

        以上 beans 中的一系列地址是对当前配置文件的规范声明, 可以参考 http://www.springframework.org/schema/ 查看,<beans.../> 是配置文件的根元素, 里面包含了大多数要注入的 bean , 每个 bean 都对应一个java 实例,定义 <bean> 元素基本需要指定它的 id 和 class 属性,id 是确定 bean 的唯一标示, 在容器中是唯一的, class 指定 bean 的具体实现类, 注意不能是接口。

 

例如上述 code 1.0 中 bean 的定义,这就相当于 spring 帮我们做了 bean1 = new com.wind.bean1() ,并且如果没有配置 bean 的作用域的话(后面会提到),默认容器会帮我们维护单一的实例。接下来, 说下 bean 的初始化方式, 在 java pojo 中, 我们初始化一个实例, 有两种方法,一是通过 构造器 , 二是通过 setter , 在这里也类似, 我们可以通过在 <bean>....</bean> 的定义内部通过 constructor-arg 属性来指定 bean 构造器中的参数值来初始化,  例如上说 code1.0 中 bean2 的定义, 此外, 还可以通过设置 bean 的 property 来初始化, 这里的 property 就是 bean 所在具体类中 set 的属性值,有时, 我们还需要初始化包含的 bean ,可以通过 ref 属性来搞定, 例如上说 code 1.0 中的 bean3, 当然, 这里还有很多细节, 可以参考官方的文档。

 

二、 bean 的作用域

       在我们定义 bean 时, 如果不指定作用域, 默认就是 singleton, 即 单例的, 我们可以通过 scope 属性来指定 bean 的作用域, 下面看下 spring 支持的 5 中作用域。

       1、singleton , 单例模式, 在整个SpringIoC容器中,使用singleton定义的Bean将只有一个实例。

       2、prototype, 原型模式, 每次通过容器的getBean方法获取prototype定义的Bean时,都将产生一个新的Bean实例。

       3、request, 对于每次HTTP请求,使用request定义的Bean都将产生一个新的实例,即每次HTTP请求都会产生不同的Bean实例。仅在 web 应用中有用。

       4、session, 对于每次HTTPSession,使用session定义的Bean都将产生一个新的实例时,即每次HTTP Session都将产生不同的Bean实例。仅在 web 应用中有用。

       5、global session, 每个全局的HTTPSession对应一个Bean实例。仅在portlet Context的时候才有效。仅在 page context 中有用。

 

   注意:以上 4,5 只有在 web 应用中才能生效, 并且, 为了让 request 和 session 作用域生效, 还需要把 http 请求绑定到 该请求的服务线程上, 这使得具有 request 和 session 作用域的 bean 在后面的作用链中能被访问到, 我们可以通过 web.xml 中做以下两种配置中的一种即可。

 

    通过 listener 配置

<listener-class>  
        org.springframework.web.context.request.RequestContextListener  
    </listener-class>  
</listener> 用。

 

    或者通过 filter 配置

<filter>  
    <filter-name>requestContextFilter</filter-name>  
    <filter-class>org.springframework.web.filter.RequestContextFilter</filter-class>  
</filter>  
<filter-mapping>  
    <filter-name>requestContextFilter</filter-name>  
    <url-pattern>/*</url-pattern>  
</filter-mapping>

 

      关于 request 和 session 作用域的 bean ,spring  会为每次请求或者会话创建 bean 实例, 在请求或者会话结束后, 该实例也会被注销。

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值