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

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

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

本文實(shí)例講述了Spring實(shí)戰(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">
   <!-- 配置兩個(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()方法注入依賴(lài)關(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í)例化依賴(lài)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
{
  /**
   * 重寫(xiě)該方法,對(duì)Spring進(jìn)行后處理。
   * @param beanFactory Spring容器本身
   */
  public void postProcessBeanFactory(
    ConfigurableListableBeanFactory beanFactory)
    throws BeansException
  {
    System.out.println("程序?qū)pring所做的BeanFactory的初始化沒(méi)有改變...");
    System.out.println("Spring容器是:" + beanFactory);
  }
}

五 測(cè)試類(lèi)

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的初始化沒(méi)有改變...
Spring容器是:org.springframework.beans.factory.support.DefaultListableBeanFactory@6a024a67: defining beans  [steelAxe,chinese,beanFactoryPostProcessor]; root of factory  hierarchy
Spring實(shí)例化依賴(lài)bean:SteelAxe實(shí)例...
Spring實(shí)例化主調(diào)bean:Chinese實(shí)例...
Spring執(zhí)行setName()方法注入依賴(lài)關(guān)系...
正在執(zhí)行初始化方法  afterPropertiesSet...
正在執(zhí)行初始化方法   init...
孫悟空鋼斧砍柴真快

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

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

相關(guān)文章

  • Java 深入分析鏈表面試實(shí)例題目

    Java 深入分析鏈表面試實(shí)例題目

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

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

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

    jdk動(dòng)態(tài)代理使用實(shí)例詳解

    JDK動(dòng)態(tài)代理是代理模式的一種實(shí)現(xiàn)方式,因?yàn)樗腔诮涌趤?lái)做代理的,所以也常被稱(chēng)為接口代理,下面這篇文章主要給大家介紹了關(guān)于jdk動(dòng)態(tài)代理使用的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-06-06
  • Spring中的自定義NamespaceHandler詳解

    Spring中的自定義NamespaceHandler詳解

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

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

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

    CGLIB代理的使用與原理解析

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

    Spring data jpa的使用與詳解(復(fù)雜動(dòng)態(tài)查詢(xún)及分頁(yè),排序)

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

    關(guān)于spring boot中幾種注入方法的一些個(gè)人看法

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

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

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

    mybatis-plus分頁(yè)插件失效探究解決

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

最新評(píng)論