---
title: "Spring --- IOC III"
author: "thaIm"
date: "2012-12-04T15:34:37Z"
tags: []
categories: ["编程语言"]
slug: "spring-ioc-iii"
draft: false
lastmod: "2012-12-06T15:48:16Z"
id: 1740883
url: "https://thaim.iteye.com/blog/1740883"
---

承接上两篇IOC的介绍，我们继续...
9)ApplicationContextAware 和 BeanNameAware
    加载Spring配置文件时，如果Spring配置文件中所定义的Bean类，如果该类实现了ApplicationContextAware接口，那么在加载Spring配置文件时，会自动调用ApplicationContextAware接口中的
    public void setApplicationContext(ApplicationContext context) throws BeansException 
    方法，并且自动可获得ApplicationContext 对象。前提必须在Spring配置文件中指定改类。
 
一个Demo程序如下： 
Spring配置文件中配置：

```java
<bean id="springContext" class="com.shine.spring.SpringContextHelper"></bean>
/**
* ApplicationContext的帮助类
* 自动装载ApplicationContext
*/
public class SpringContextHelper implements ApplicationContextAware{
  private static ApplicationContext context ;
  //注入ApplicationContext
  @Override
  public void setApplicationContext(ApplicationContext context)throws BeansException {
    //在加载Spring时自动获得context
    SpringContextHelper.context = context;
    System.out.println(SpringContextHelper.context);
  }
  public static Object getBean(String beanName){
    return context.getBean(beanName);
  }
}
```


    继承了BeanNameAware的类会在初始化函数之前，普通bean属性加载之后调用setBeanName()方法：

```java
public interface BeanNameAware {
  void setBeanName(string name) throws BeansException;
}
```


    类似的Aware接口spring还提供了很多，功能也是个不相同。这里就不一一例举了。以后碰到在介绍吧~~

10)bean定义的“继承”
注意一个属性设置：abstract="true"

```java
<bean id="inheritedTestBean" abstract="true" class="org.springframework.beans.TestBean">
  <property name="name" value="parent"/>
  <property name="age" value="1"/>
</bean>
<bean id="inheritsWithDifferentClass" class="org.springframework.beans.DerivedTestBean" parent="inheritedTestBean" init-method="initialize">
  <property name="name" value="override"/>
  <!-- the age property value of 1 will be inherited from parent -->
</bean>
```

并且：depends on, autowire mode,dependencycheck,singleton,scope,lazyinit.这些属性不具有“继承”性，仍然保持子bean的默认设置。

11)BeanPostProcessor
    BeanPostProcessor的作用域是容器级的，它只和所在容器有关。如果你在容器中定义了BeanPostProcessor,它仅仅对此容器中的bean进行作用。它不会对定义在另一个容器中的bean进行任何处理。该接口作用是：如果我们需要在Spring容器完成Bean的实例化，配置和其他的初始化后添加一些自己的逻辑处理，我们就可以定义一个或者多个BeanPostProcessor接口的实现。以下我们看个实例：

```java
<bean class="com.spring.test.di.BeanPostPrcessorImpl"/>  
package com.spring.test.di;
 
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
 
public class BeanPostPrcessorImpl implements BeanPostProcessor {
    // Bean 实例化之前进行的处理
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
       System.out.println("对象" + beanName + "开始实例化");
       return bean;
    }
    // Bean 实例化之后进行的处理
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
       System.out.println("对象" + beanName + "实例化完成");
       return bean;
    }
}
```

  测试代码如下：

```java
 // 得到ApplicationContext对象  
 ApplicationContext ctx = new FileSystemXmlApplicationContext("applicationContext.xml");  
 // 得到Bean  
 ctx.getBean("logic");  
```

  控制台打印结果将会是：
 
对象logic开始实例化
对象logic实例化完成

12) BeanFactoryPostProcessor
  在BeanFactory加载Bean定义档的所有内容，但还没正式产生Bean实例之前，您可以对该BeanFactory进行一些处理，您只要实例化一个继承了org.springframework.beans.factory.config.BeanFactoryPostProcessor接口的类。

```java
package org.springframework.beans.factory.config;
public interface BeanFactoryPostProcessor {
	public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException;
}
```

  Spring已经定义了几种实例化BeanFactoryPostProcessor接口的类。它们的功能也各不相同。下面是一些实例介绍：
  **1'PropertyPlaceholderConfigurer**

```java
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
  <property name="locations" value="classpath:com/foo/jdbc.properties"/>
</bean>
<bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource">
  <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
  Spring加载相应的bean之前，${jdbc.username}等将会被properties里相应的配置sa等值所取代。

  **2'PropertyOverrideConfigurer**
  调用PropertyOverrideConfigurer之后，xml配置文件的格式将可以转换为property的配置方式来实现。

```java
<context:property-override location="classpath:override.properties"/>

//override.properties里的内容是
beanName.property=value //beanName属性值为value
dataSource.driverClassName=com.mysql.jdbc.Driver //一个叫dataSource的bean，它的driverClassName属性值是com.mysql.jdbc.Driver 
dataSource.url=jdbc:mysql:mydb
foo.fred.bob.sammy=123 //foo bean的fred属性里的bob属性里的sammy属性的值是123
```


13) Annotation
    当你在你的配置文件中加入以下配置：

```java
<context:annotation-config/>
```

    你就可以在你的代码里使用annotation了，但你需要知道，这个而配置的背后是，你已通过此配置加载了AutowiredAnnotationBeanPostProcessor,CommonAnnotationBeanPostProcessor,PersistenceAnnotationBeanPostProcessor以及RequiredAnnotationBeanPostProcessor.通过它们来最终实现了annotation的功能。
    至于annotation的具体使用，其内容丰富，我们将在以后以专门的篇幅来介绍。