posts - 3, comments - 15, trackbacks - 0, articles - 26
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

spring-framework-1.2.6-chapter3

Posted on 2005-12-28 19:38 morcble的blog 阅读(231) 评论(0)  编辑  收藏 所属分类: Spring

最基础的包中有两个是org.springframework.beans 和org.springframework.context .这两个包提供了spring依赖注射功能的基础。
BeanFactory提供了高级的配置机制用于管理beans。
ApplicationContext接口建立在BeanFactory接口之上(BeanFactory的子类),又添加了其它的功能,比如:更容易与Spring AOP功能结合,信息资源处理,事件传播,建立ApplicationContext和父Contexts的机制,应用层特殊的context(如WebApplicationContext)。
ApplicationContext是BeanFactory的扩展,所有BeanFactory的功能都能在ApplicationContext中实现。在建立的项目处于j2ee环境的时候最好选择ApplicationContext;当你对内存的占有很关心的时候(如一个applet会关心每KB大小)可以选择BeanFactory(占内存少)。
BeanFactory 是为了访问Spring bean container,可以把它看成事实上的container.一般简单的BeanFactory的实现是org.springframework.beans.factory.xml.XmlBeanFactory。
创建 BeanFatory的3个办法如下 
1。Resource res = new FileSystemResource("beans.xml");
   XmlBeanFactory factory = new XmlBeanFactory(res);
2。ClassPathResource res = new ClassPathResource("beans.xml");
   XmlBeanFactory factory = new XmlBeanFactory(res);
3。ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext(new String[] {"applicationContext.xml", "applicationContext-part2.xml"});
      // of course, an ApplicationContext is just a BeanFactory
   BeanFactory factory = (BeanFactory) appContext;
   (注:public interface Resourceextends InputStreamSource,
        public interface InputStreamSource)
多数用法中,用户不需要编写代码初始化BeanFactory 或 ApplicationContext,因为Spring框架会自动完成。例如:在web层提供了代码自动装载
ApplicationContext,作为正常的j2eeWEB应用程序开始的一部分。
 

 配置bean的XML几种格式如下:
 (1)。通过构造函数创建bean,bean可以是javabean 也可以不是,也不用实现别的接口。
    <bean id="exampleBean" class="examples.ExampleBean"/>
    <bean name="anotherExample" class="examples.ExampleBeanTwo"/>
    如果你需要实现IOC机制,你的bean需要一个默认的空参数构造函数。
 (2)。通过静态工厂函数创建bean.
   <bean id="exampleBean" class="examples.ExampleBean2"
     factory-method="createInstance"/>
      createInstance是examples.ExampleBean2的静态工厂方法,用于生成exampleBean。
 (3).通过实例工厂方法创建bean.
 <!-- The factory bean, which contains a method called createInstance -->
  <bean id="myFactoryBean" class="...">
  ...
   </bean>
   <!-- The bean to be created via the factory bean -->
  <bean id="exampleBean" factory-bean="myFactoryBean" factory-method="createInstance"/>


To singleton or not to singleton
<bean id="exampleBean"
class="examples.ExampleBean" singleton="false"/>
<bean name="yetAnotherExample"
class="examples.ExampleBeanTwo" singleton="true"/>


几种依赖注射格式
(1)setter注射

<bean id="exampleBean" class="examples.ExampleBean">
<property name="beanOne"><ref bean="anotherExampleBean"/></property>
<property name="beanTwo"><ref bean="yetAnotherBean"/></property>
<property name="integerProperty"><value>1</value></property>
</bean>
<bean id="anotherExampleBean" class="examples.AnotherBean"/>
<bean id="yetAnotherBean" class="examples.YetAnotherBean"/>
public class ExampleBean {
private AnotherBean beanOne;
private YetAnotherBean beanTwo;
private int i;

public void setBeanOne(AnotherBean beanOne) {
this.beanOne = beanOne;
}
public void setBeanTwo(YetAnotherBean beanTwo) {
this.beanTwo = beanTwo;
}
public void setIntegerProperty(int i) {
this.i = i;
}
}

(2)构造函数注射
<bean id="exampleBean" class="examples.ExampleBean">
<constructor-arg><ref bean="anotherExampleBean"/></constructor-arg>
<constructor-arg><ref bean="yetAnotherBean"/></constructor-arg>
<constructor-arg type="int"><value>1</value></constructor-arg>
</bean>
<bean id="anotherExampleBean" class="examples.AnotherBean"/>
<bean id="yetAnotherBean" class="examples.YetAnotherBean"/>
public class ExampleBean {
private AnotherBean beanOne;
private YetAnotherBean beanTwo;
private int i;
public ExampleBean(AnotherBean anotherBean, YetAnotherBean yetAnotherBean, int i) {
this.beanOne = anotherBean;
this.beanTwo = yetAnotherBean;
this.i = i;
}
}

(3)静态工厂注射
<bean id="exampleBean" class="examples.ExampleBean" factory-method="createInstance">
<constructor-arg><ref bean="anotherExampleBean"/></constructor-arg>
<constructor-arg><ref bean="yetAnotherBean"/></constructor-arg>
<constructor-arg><value>1</value></constructor-arg>
</bean>
<bean id="anotherExampleBean" class="examples.AnotherBean"/>
<bean id="yetAnotherBean" class="examples.YetAnotherBean"/>

public class ExampleBean {
 ...
 // a private constructor
   private ExampleBean(...) {
   ...
   }
 // a static factory method
 // the arguments to this method can be considered the dependencies of the bean that
 // is returned, regardless of how those arguments are actually used.
     public static ExampleBean createInstance(AnotherBean anotherBean, YetAnotherBean yetAnotherBean, int i) {
        ExampleBean eb = new ExampleBean(...);
       // some other operations
        ...
      return eb;
     }
}


构造函数配置的另一种格式
<bean id="exampleBean" class="examples.ExampleBean">
<constructor-arg index="0"><value>7500000</value></constructor-arg>
<constructor-arg index="1"><value>42</value></constructor-arg>
</bean>
可以在配置文件中加入验证数据的类型 如下
<bean id="exampleBean" class="examples.ExampleBean">
<constructor-arg index="0" type="int"><value>7500000</value></constructor-arg>
<constructor-arg index="1" type="java.lang.String"><value>42</value></constructor-arg>
</bean>

一般属性配置格式
<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<!-- results in a setDriverClassName(String) call -->
  <property name="driverClassName">
     <value>com.mysql.jdbc.Driver</value>
  </property>
  <property name="url">
     <value>jdbc:mysql://localhost:3306/mydb</value>
  </property>
  <property name="username">
     <value>root</value>
  </property>
</bean>

注意:<value></value> 默认为空字符串,


属性null的格式如下
<bean class="ExampleBean">
 <property name="email"><null/></property>
</bean>


属性collection的格式如下
<bean id="moreComplexObject" class="example.ComplexObject">
<!-- results in a setPeople(java.util.Properties) call -->
<property name="people">
   <props>
     <prop key="HarryPotter">The magic property</prop>
     <prop key="JerrySeinfeld">The funny property</prop>
   </props>
</property>
<!-- results in a setSomeList(java.util.List) call -->
<property name="someList">
   <list>
     <value>a list element followed by a reference</value>
     <ref bean="myDataSource"/>
   </list>
</property>
<!-- results in a setSomeMap(java.util.Map) call -->
<property name="someMap">
   <map>
     <entry>
         <key><value>yup an entry</value></key>
         <value>just some string</value>
     </entry>
     <entry>
         <key><value>yup a ref</value></key>
         <ref bean="myDataSource"/>
     </entry>
  </map>
</property>
<!-- results in a setSomeSet(java.util.Set) call -->
 <property name="someSet">
   <set>
     <value>just some string</value>
     <ref bean="myDataSource"/>
   </set>
 </property>
</bean>

3.3.3.4. Inner bean definitions via nested bean elements
<bean id="outer" class="...">
<!-- Instead of using a reference to target, just use an inner bean -->
   <property name="target">
      <bean class="com.mycompany.PersonImpl">//嵌套的bean不需要id
         <property name="name"><value>Tony</value></property>
         <property name="age"><value>51</value></property>
      </bean>
   </property>
</bean>

复合属性定义
<bean id="foo" class="foo.Bar">
<property name="fred.bob.sammy" value="123"/> 属性fred下有属性bob,bob属性下有sammy 把sammy置为123。
</bean>

3.3.4. Method Injection(方法注射)
<!-- a stateful bean deployed as a prototype (non-singleton) -->
<bean id="singleShotHelper class="..." singleton="false">
</bean>
<!-- myBean uses singleShotHelper -->
<bean id="myBean" class="...">
   <lookup-method name="createSingleShotHelper" bean="singleShotHelper"/>
   <property>
   ...
   </property>
</bean>
我的理解:myBean 中必须实现 protected [abstract] SingleShotHelper createSingleShotHelper();
singleShotHelper中必须实现 protected  SingleShotHelper createSingleShotHelper();
运行时  singleShotHelper的createSingleShotHelper会覆盖myBean的createSingleShotHelper。//未验证

3.3.4.2. Arbitrary method replacement(任意的方法替换)
public class MyValueCalculator {
     public String computeValue(String input) {
     ... some real code
     }
     ... some other methods
  }

进行替换的类必须实现接口org.springframework.beans.factory.support.MethodReplacer
public class ReplacementComputeValue implements MethodReplacer {
   public Object reimplement(Object o, Method m, Object[] args) throws Throwable {
  // get the input value, work with it, and return a computed result
   String input = (String) args[0];
   ...
   return ...;
  }
}

<bean id="myValueCalculator class="x.y.z.MyValueCalculator">
<!-- arbitrary method replacement -->
    <replaced-method name="computeValue" replacer="replacementComputeValue">
       <arg-type>String</arg-type>     这里表示方法接受的参数,有几个列几个。
    </replaced-method>                 这样myValueCalculator中的computeValue方法就被替换了
</bean>
<bean id="replacementComputeValue" class="a.b.c.ReplaceMentComputeValue"/>


3.4.1生命周期
<bean id="exampleInitBean" class="examples.ExampleBean" init-method="init"/>
public class ExampleBean {
   public void init() {
    // do some initialization work
   }
}
Is exactly the same as:
<bean id="exampleInitBean" class="examples.AnotherExampleBean"/>
  public class AnotherExampleBean implements InitializingBean {
  public void afterPropertiesSet() {
  // do some initialization work
  }
}


<bean id="exampleInitBean" class="examples.ExampleBean" destroy-method="cleanup"/>
public class ExampleBean {
    public void cleanup() {
   // do some destruction work (like closing connection)
   }
}
Is exactly the same as:
<bean id="exampleInitBean" class="examples.AnotherExampleBean"/>
    public class AnotherExampleBean implements DisposableBean {
    public void destroy() {
      // do some destruction work
    }
}

 

===============================================
3.7. 用BeanPostProcessors自定义beans
   ConfigurableBeanFactory bf = new .....; // create BeanFactory
    ... // now register some beans
   // now register any needed BeanPostProcessors
    MyBeanPostProcessor pp = new MyBeanPostProcessor();
   bf.addBeanPostProcessor(pp);
   // now start using the factory

3.8. 用BeanFactoryPostProcessors 自定义bean factories
   XmlBeanFactory factory = new XmlBeanFactory(new FileSystemResource("beans.xml"));
     // create placeholderconfigurer to bring in some property
     // values from a Properties file
   PropertyPlaceholderConfigurer cfg = new PropertyPlaceholderConfigurer();
   cfg.setLocation(new FileSystemResource("jdbc.properties"));
   // now actually do the replacement
   cfg.postProcessBeanFactory(factory);
(也可以声明式定义
   <!-- property placeholder post-processor -->
        <bean id="placeholderConfig"  class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
             <property name="location" value="jdbc.properties"/>
        </bean>

由上
以下这个处于beanfactory容器里的数据源就按jdbc.properties里的配置了
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
  <property name="driverClassName" value="${jdbc.driverClassName}"/>
  <property name="url" value="${jdbc.url}"/>
  <property name="username" value="${jdbc.username}"/>
  <property name="password" value="${jdbc.password}"/>
</bean>

其中jdbc.properties如下
jdbc.driverClassName=org.hsqldb.jdbcDriver
jdbc.url=jdbc:hsqldb:hsql://production:9002
jdbc.username=sa
jdbc.password=root


3.8.2. The PropertyOverrideConfigurer

3.19. Creating an ApplicationContext from a web application
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/daoContext.xml /WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>(the listener cannot be used in Servlet 2.2 compatible
containers  但是在Servlet2.4后都普遍采用它)

<!-- OR USE THE CONTEXTLOADERSERVLET INSTEAD OF THE LISTENER
<servlet>
<servlet-name>context</servlet-name>
<servlet-class>org.springframework.web.context.ContextLoaderServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
-->


只有注册用户登录后才能发表评论。


网站导航: