欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2019年12月17日 09:26:41   作者:cakincqm  
這篇文章主要介紹了Spring實戰(zhàn)之容器后處理器操作,結(jié)合實例形式分析了spring容器后處理器配置、使用操作技巧與相關注意事項,需要的朋友可以參考下

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

一 配置文件

<?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">
   <!-- 配置兩個簡單Bean實例 -->
   <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實例化主調(diào)bean:Chinese實例...");
  }
  public void setAxe(Axe axe)
  {
    this.axe = axe;
  }
  public void setName(String name)
  {
    System.out.println("Spring執(zhí)行setName()方法注入依賴關系...");
    this.name = name;
  }
  public void useAxe()
  {
    System.out.println(name + axe.chop());
  }
  // 下面是兩個生命周期方法
  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實例化依賴bean:SteelAxe實例...");
   }
   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
{
  /**
   * 重寫該方法,對Spring進行后處理。
   * @param beanFactory Spring容器本身
   */
  public void postProcessBeanFactory(
    ConfigurableListableBeanFactory beanFactory)
    throws BeansException
  {
    System.out.println("程序?qū)pring所做的BeanFactory的初始化沒有改變...");
    System.out.println("Spring容器是:" + beanFactory);
  }
}

五 測試類

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容器
    // 它會自動注冊容器后處理器、Bean后處理器
    ApplicationContext ctx = new
      ClassPathXmlApplicationContext("beans.xml");
    Person p = (Person)ctx.getBean("chinese");
    p.useAxe();
  }
}

六 測試結(jié)果

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

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

希望本文所述對大家java程序設計有所幫助。

相關文章

  • Java 深入分析鏈表面試實例題目

    Java 深入分析鏈表面試實例題目

    鏈表是一種物理存儲單元上非連續(xù)、非順序的存儲結(jié)構(gòu),數(shù)據(jù)元素的邏輯順序是通過鏈表中的指針鏈接次序?qū)崿F(xiàn)的,本篇帶你通過兩個實例題目來深入探索
    2022-03-03
  • 基于springboot搭建的web系統(tǒng)架構(gòu)的方法步驟

    基于springboot搭建的web系統(tǒng)架構(gòu)的方法步驟

    這篇文章主要介紹了基于springboot搭建的web系統(tǒng)架構(gòu)的方法步驟,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-04-04
  • jdk動態(tài)代理使用實例詳解

    jdk動態(tài)代理使用實例詳解

    JDK動態(tài)代理是代理模式的一種實現(xiàn)方式,因為它是基于接口來做代理的,所以也常被稱為接口代理,下面這篇文章主要給大家介紹了關于jdk動態(tài)代理使用的相關資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2022-06-06
  • Spring中的自定義NamespaceHandler詳解

    Spring中的自定義NamespaceHandler詳解

    這篇文章主要介紹了Spring中的自定義NamespaceHandler詳解,通常情況下,Spring生態(tài)圈提供的功能已足夠使用,但不排除特殊情況下,需要匹配特殊及復雜的業(yè)務情況,Spring提供了可擴展Schema支持,可以自定義命名空間進行配置及解析,需要的朋友可以參考下
    2023-11-11
  • Spring Boot結(jié)合ECharts案例演示示例

    Spring Boot結(jié)合ECharts案例演示示例

    本文主要主要介紹了Spring Boot結(jié)合ECharts案例演示示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-06-06
  • CGLIB代理的使用與原理解析

    CGLIB代理的使用與原理解析

    這篇文章主要介紹了CGLIB代理的使用與原理解析,靜態(tài)代理和JDK 代理模式都要求目標對象是實現(xiàn)一個接口,但是有時候目標對象只是一個單獨的對象,并沒有實現(xiàn)任何的接口,這個時候可使用目標對象子類來實現(xiàn)代理,這就是Cglib代理,需要的朋友可以參考下
    2023-09-09
  • Spring data jpa的使用與詳解(復雜動態(tài)查詢及分頁,排序)

    Spring data jpa的使用與詳解(復雜動態(tài)查詢及分頁,排序)

    這篇文章主要介紹了Spring data jpa的使用與詳解(復雜動態(tài)查詢及分頁,排序),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-11-11
  • 關于spring boot中幾種注入方法的一些個人看法

    關于spring boot中幾種注入方法的一些個人看法

    這篇文章主要給大家介紹了關于spring boot中幾種注入方法的一些個人看法,文中通過示例代碼介紹的非常詳細,對大家學習或者使用spring boot具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧
    2019-07-07
  • java final 和instanceof 關鍵字的區(qū)別

    java final 和instanceof 關鍵字的區(qū)別

    這篇文章介紹了java final 和instanceof 關鍵字的區(qū)別,有需要的朋友可以參考一下
    2013-09-09
  • mybatis-plus分頁插件失效探究解決

    mybatis-plus分頁插件失效探究解決

    這篇文章主要為大家介紹了mybatis-plus分頁插件失效探究解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-07-07

最新評論