Spring實(shí)戰(zhàn)之容器后處理器操作示例

本文實(shí)例講述了Spring實(shí)戰(zhàn)之容器后處理器。分享給大家供大家參考,具體如下:

創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比冷水江網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫(kù),直接使用。一站式冷水江網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋冷水江地區(qū)。費(fèi)用合理售后完善,10余年實(shí)體公司更值得信賴。

一 配置文件

<?xml version="1.0" encoding="GBK"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns="http://www.springframework.org/schema/beans"
   xmlns:p="http://www.springframework.org/schema/p"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
   <!-- 配置兩個(gè)簡(jiǎn)單Bean實(shí)例 -->
   <bean id="steelAxe" class="org.crazyit.app.service.impl.SteelAxe"/>
   <bean id="chinese" class="org.crazyit.app.service.impl.Chinese"
      init-method="init" p:name="孫悟空" p:axe-ref="steelAxe"/>
   <!-- 配置容器后處理器 -->
   <bean id="beanFactoryPostProcessor"
      class="org.crazyit.app.util.MyBeanFactoryPostProcessor"/>
</beans>

二 接口

Axe

package org.crazyit.app.service;
public interface Axe
{
   public String chop();
}

Person

package org.crazyit.app.service;
public interface Person
{
   public void useAxe();
}

三 Bean

Chinese

package org.crazyit.app.service.impl;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.crazyit.app.service.*;
public class Chinese
  implements Person,InitializingBean
{
  private Axe axe;
  private String name;
  public Chinese()
  {
    System.out.println("Spring實(shí)例化主調(diào)bean:Chinese實(shí)例...");
  }
  public void setAxe(Axe axe)
  {
    this.axe = axe;
  }
  public void setName(String name)
  {
    System.out.println("Spring執(zhí)行setName()方法注入依賴關(guān)系...");
    this.name = name;
  }
  public void useAxe()
  {
    System.out.println(name + axe.chop());
  }
  // 下面是兩個(gè)生命周期方法
  public void init()
  {
    System.out.println("正在執(zhí)行初始化方法 init...");
  }
  public void afterPropertiesSet() throws Exception
  {
    System.out.println("正在執(zhí)行初始化方法 afterPropertiesSet...");
  }
}

SteelAxe

package org.crazyit.app.service.impl;
import org.crazyit.app.service.*;
public class SteelAxe
   implements Axe
{
   public SteelAxe()
   {
      System.out.println("Spring實(shí)例化依賴bean:SteelAxe實(shí)例...");
   }
   public String chop()
   {
      return "鋼斧砍柴真快";
   }
}

四 容器后處理器

package org.crazyit.app.util;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
public class MyBeanFactoryPostProcessor
  implements BeanFactoryPostProcessor
{
  /**
   * 重寫該方法,對(duì)Spring進(jìn)行后處理。
   * @param beanFactory Spring容器本身
   */
  public void postProcessBeanFactory(
    ConfigurableListableBeanFactory beanFactory)
    throws BeansException
  {
    System.out.println("程序?qū)pring所做的BeanFactory的初始化沒有改變...");
    System.out.println("Spring容器是:" + beanFactory);
  }
}

五 測(cè)試類

package lee;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.crazyit.app.service.*;
public class BeanTest
{
  public static void main(String[] args)
  {
    // 以ApplicationContex作為Spring容器
    // 它會(huì)自動(dòng)注冊(cè)容器后處理器、Bean后處理器
    ApplicationContext ctx = new
      ClassPathXmlApplicationContext("beans.xml");
    Person p = (Person)ctx.getBean("chinese");
    p.useAxe();
  }
}

六 測(cè)試結(jié)果

程序?qū)pring所做的BeanFactory的初始化沒有改變...
Spring容器是:org.springframework.beans.factory.support.DefaultListableBeanFactory@6a024a67: defining beans  [steelAxe,chinese,beanFactoryPostProcessor]; root of factory  hierarchy
Spring實(shí)例化依賴bean:SteelAxe實(shí)例...
Spring實(shí)例化主調(diào)bean:Chinese實(shí)例...
Spring執(zhí)行setName()方法注入依賴關(guān)系...
正在執(zhí)行初始化方法  afterPropertiesSet...
正在執(zhí)行初始化方法   init...
孫悟空鋼斧砍柴真快

更多關(guān)于java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Spring框架入門與進(jìn)階教程》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》

希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。

文章標(biāo)題:Spring實(shí)戰(zhàn)之容器后處理器操作示例
分享路徑:http://bm7419.com/article20/igisjo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供Google、外貿(mào)建站、網(wǎng)站營(yíng)銷、網(wǎng)站維護(hù)、網(wǎng)站設(shè)計(jì)、定制網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站托管運(yùn)營(yíng)