zoukankan      html  css  js  c++  java
  • 3.The IoC container(2)

    http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/beans.html#beans-factory-nature

    4.6.1 Lifecycle callbacks

    4.6.1.1 Initialization callbacks

    The org.springframework.beans.factory.InitializingBean interface allows a bean to perform initialization work after all necessary properties on the bean have been set by the container.接口InitializingBean只有一个方法

    1 void afterPropertiesSet() throws Exception;

    然而,更推荐用XML-based configuration metadata去配置,For example:

    <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
      }
    }

    同样,Implementing the org.springframework.beans.factory.DisposableBean interface allows a bean to get a callback when the container containing it is destroyed. The DisposableBean interface specifies 只有一个方法:

    1 void destroy() throws Exception;

    推荐:

    <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)
      }
    }

    4.7 Bean definition inheritance(继承)

     1 <bean id="inheritedTestBean" abstract="true"
     2     class="org.springframework.beans.TestBean">
     3   <property name="name" value="parent"/>
     4   <property name="age" value="1"/>
     5 </bean>
     6 
     7 <bean id="inheritsWithDifferentClass"
     8       class="org.springframework.beans.DerivedTestBean"
     9       parent="inheritedTestBean" init-method="initialize">
    10 
    11   <property name="name" value="override"/>
    12   <!-- the age property value of 1 will be inherited from  parent -->
    13 
    14 </bean>

    4.8.1.1 Example: Hello World, BeanPostProcessor-style

     1 package scripting;
     2 
     3 import org.springframework.beans.factory.config.BeanPostProcessor;
     4 import org.springframework.beans.BeansException;
     5 
     6 public class InstantiationTracingBeanPostProcessor implements BeanPostProcessor {
     7 
     8   // simply return the instantiated bean as-is
     9   public Object postProcessBeforeInitialization(Object bean, String beanName)
    10                                                                      throws BeansException {
    11       return bean; // we could potentially return any object reference here...
    12   }
    13 
    14   public Object postProcessAfterInitialization(Object bean, String beanName)
    15                                                                      throws BeansException {
    16       System.out.println("Bean '" + beanName + "' created : " + bean.toString());
    17       return bean;
    18   }
    19 }
    20 
    21 <?xml version="1.0" encoding="UTF-8"?>
    22 <beans xmlns="http://www.springframework.org/schema/beans"
    23      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    24      xmlns:lang="http://www.springframework.org/schema/lang"
    25      xsi:schemaLocation="http://www.springframework.org/schema/beans
    26          http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    27          http://www.springframework.org/schema/lang
    28          http://www.springframework.org/schema/lang/spring-lang-3.0.xsd">
    29 
    30   <lang:groovy id="messenger"
    31         script-source="classpath:org/springframework/scripting/groovy/Messenger.groovy">
    32       <lang:property name="message" value="Fiona Apple Is Just So Dreamy."/>
    33   </lang:groovy>
    34 
    35   <!--
    36       when the above bean (messenger) is instantiated, this custom
    37       BeanPostProcessor implementation will output the fact to the system console
    38    -->
    39   <bean class="scripting.InstantiationTracingBeanPostProcessor"/>
    40 
    41 </beans>
    42 ///////////////////////////////////
    43 import org.springframework.context.ApplicationContext;
    44 import org.springframework.context.support.ClassPathXmlApplicationContext;
    45 import org.springframework.scripting.Messenger;
    46 
    47 public final class Boot {
    48 
    49   public static void main(final String[] args) throws Exception {
    50       ApplicationContext ctx = new ClassPathXmlApplicationContext("scripting/beans.xml");
    51       Messenger messenger = (Messenger) ctx.getBean("messenger");
    52       System.out.println(messenger);
    53   }
    54 }
    55 
    56 //The output of the preceding application resembles the following:
    57 
    58 Bean 'messenger' created : org.springframework.scripting.groovy.GroovyMessenger@272961
    59 org.springframework.scripting.groovy.GroovyMessenger@272961

    4.8.1.2 Example: The PropertyPlaceholderConfigurer

     1 <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
     2   <property name="locations" value="classpath:com/foo/jdbc.properties"/>
     3 </bean>
     4 
     5 <bean id="dataSource" destroy-method="close"
     6     class="org.apache.commons.dbcp.BasicDataSource">
     7   <property name="driverClassName" value="${jdbc.driverClassName}"/>
     8   <property name="url" value="${jdbc.url}"/>
     9   <property name="username" value="${jdbc.username}"/>
    10   <property name="password" value="${jdbc.password}"/>
    11 </bean>
    12 
    13 classpath:com/foo/jdbc.properties
    14 jdbc.driverClassName=org.hsqldb.jdbcDriver
    15 jdbc.url=jdbc:hsqldb:hsql://production:9002
    16 jdbc.username=sa
    17 jdbc.password=root

    从Spring 2.5开始,it is possible to configure property placeholders with a dedicated configuration element. 多个地址用逗号分隔。

    1 <context:property-placeholder location="classpath:com/foo/jdbc.properties"/>

     

  • 相关阅读:
    深度学习与Pytorch入门实战(二)Pytorch张量高阶操作
    深度学习与Pytorch入门实战(一)Pytorch张量操作
    【邱希鹏】nndl-chap2-linear_regression
    【邱希鹏】nndl-chap1-warmup
    【Deeplearning.ai 】吴恩达深度学习笔记及课后作业目录
    Sequence Model-week3编程题2-Trigger Word Detection
    Sequence Model-week3编程题1-Neural Machine Translation with Attention
    Coursera Deep Learning笔记 序列模型(三)Sequence models & Attention mechanism(序列模型和注意力机制)
    LSTM的参数return_sequences含义
    conda离线安装
  • 原文地址:https://www.cnblogs.com/chuanheng/p/2762659.html
Copyright © 2011-2022 走看看