码迷,mamicode.com
首页 > 编程语言 > 详细

spring3

时间:2014-06-22 00:24:01      阅读:278      评论:0      收藏:0      [点我收藏+]

标签:spring

singleton 

prototype

request

session

global session

后三种只适用于容器为webaware applicationContext  例如XmlWebApplicationContext

当 后三种范围bean 被依赖时候,需要添加   <aop:scoped-proxy/>   cglib代理

   <?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:aop="http://www.springframework.org/schema/aop" 
      xsi:schemaLocation="http://www.springframework.org/schema/beans 
          http://www.springframework.org/schema/beans/spring-beans.xsd 
          http://www.springframework.org/schema/aop 
          http://www.springframework.org/schema/aop/spring-aop.xsd"> 


      <!-- an HTTP Session-scoped bean exposed as a proxy --> 
      <bean id="userPreferences" class="com.foo.UserPreferences" scope="session"> 
          <!-- instructs the container to proxy the surrounding bean --> 
          <aop:scoped-proxy/> 
      </bean> 


      <!-- a singleton-scoped bean injected with a proxy to the above bean --> 
      <bean id="userService" class="com.foo.SimpleUserService"> 
          <!-- a reference to the proxied userPreferences bean --> 
          <property name="userPreferences" ref="userPreferences"/> 
      </bean> 
  </beans> 
  


jdk 动态代理。


<bean id="userPreferences" class="com.foo.UserPreferences" scope="session"> 
      <aop:scoped-proxy proxy-target-class="false"/> 
  </bean> 
  <bean id="userManager" class="com.foo.UserManager"> 
      <property name="userPreferences" ref="userPreferences"/> 
  </bean> 


可以自定义范围,继承org.springframework.beans.factory.config.Scope


bean 创建之前做一些事情 的方法:

<bean id="exampleInitBean" class="examples.ExampleBean" init-method="init"/> 


  public class ExampleBean { 


      public void init() { 
          // do some initialization work 
      } 


  } 

这种不如上一种好。 

<bean id="exampleInitBean" class="examples.AnotherExampleBean"/> 


  public class AnotherExampleBean implements InitializingBean { 


      public void afterPropertiesSet() { 
          // do some initialization work 
      } 


  } 



同样 bean 销毁之后做一些事情怎么办?

<bean id="exampleInitBean" class="examples.ExampleBean" destroy-method="cleanup"/> 


  public class ExampleBean { 


      public void cleanup() { 
          // do some destruction work (like releasing pooled connections) 
      } 


  } 


不如上一种好


  <bean id="exampleInitBean" class="examples.AnotherExampleBean"/> 


  public class AnotherExampleBean implements DisposableBean { 


      public void destroy() { 
          // do some destruction work (like releasing pooled connections) 
      } 


  } 

每个bean 都去指定初始化方法很麻烦,有一种简单方法,就是

 public class DefaultBlogService implements BlogService { 


      private BlogDao blogDao; 


      public void setBlogDao(BlogDao blogDao) { 
          this.blogDao = blogDao; 
      } 


      // this is (unsurprisingly) the initialization callback method 
      public void init() { 
          if (this.blogDao == null) { 
              throw new IllegalStateException( "The [blogDao] property must be set."); 
          } 
      } 


  } 


  <beans default-init-method="init"> 


      <bean id="blogService" class="com.foo.DefaultBlogService"> 
          <property name="blogDao" ref="blogDao" /> 
      </bean> 


  </beans> 


父子定义:

  <bean id="inheritedTestBeanWithoutClass" abstract="true"> 
      <property name="name" value="parent"/> 
      <property name="age" value="1"/> 
  </bean> 


  <bean id="inheritsWithClass" class="org.springframework.beans.DerivedTestBean" 
          parent="inheritedTestBeanWithoutClass" init-method="initialize"> 
      <property name="name" value="override"/> 
      <!-- age will inherit the value of 1 from the parent bean definition--> 
  </bean> 





spring3,布布扣,bubuko.com

spring3

标签:spring

原文地址:http://blog.csdn.net/xiaoliuliu2050/article/details/32342317

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!