1. 程式人生 > >spring中aware介面(5)

spring中aware介面(5)

2016/1/16 12:45:20

1.Aware

  • spring中提供了一些以Aware結尾的介面,實現了Aware介面的bean在被初始化之後,可以獲取相應資源
  • 通過Aware介面,可以對Spring相應資源進行操作(一定要慎重)
  • 為對Spring進行簡單的擴充套件提供了方便的入口
<?xml version="1.0" encoding="UTF-8"?>
<beans  xmlns="http://www.springframework.org/schema/beans" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xmlns:p="http://www.springframework.org/schema/p" 
        xmlns:aop="http://www.springframework.org/schema/aop" 
        xmlns:tx="http://www.springframework.org/schema/tx" 
        xmlns:mvc="http://www.springframework.org/schema/mvc" 
        xmlns:context="http://www.springframework.org/schema/context" 
        xsi:schemaLocation="http://www.springframework.org/schema/beans
                            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
                            http://www.springframework.org/schema/context
                            http://www.springframework.org/schema/context/spring-context-3.0.xsd 
                            http://www.springframework.org/schema/aop
                            http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
                            http://www.springframework.org/schema/tx
                            http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
                            http://www.springframework.org/schema/mvc
                            http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> 

        <bean id="applicationContext" class="com.zjx.aware.ApplicationContext"></bean>

</beans>

bean類中:

package com.zjx.aware;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContextAware;

public class ApplicationContext implements ApplicationContextAware{

    @Override
    public void setApplicationContext(
            org.springframework.context.ApplicationContext arg0)
            throws BeansException {
        System.out.println("ApplicationContext:"+arg0.getBean("applicationContext").hashCode());        
    }

}

測試類:

package com.zjx.interfaces.test;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;

@RunWith(BlockJUnit4ClassRunner.class)
public class TestAware extends UnitTestBase {
    public TestAware() {
        super("classpath*:spring-aware.xml");
    }

    @Test
    public void test() {
        System.out.println("test方法中的ApplicationContext:"
                + super.getBean("applicationContext").hashCode());
    }
}

結果打印出的兩個類的雜湊值一致,這說明實現了XXXAware介面,得到了輸出

對於BeanName實現BeanNameAware,ApplicationContextAware介面的方式

<?xml version="1.0" encoding="UTF-8"?>
<beans  xmlns="http://www.springframework.org/schema/beans" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xmlns:p="http://www.springframework.org/schema/p" 
        xmlns:aop="http://www.springframework.org/schema/aop" 
        xmlns:tx="http://www.springframework.org/schema/tx" 
        xmlns:mvc="http://www.springframework.org/schema/mvc" 
        xmlns:context="http://www.springframework.org/schema/context" 
        xsi:schemaLocation="http://www.springframework.org/schema/beans
                            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
                            http://www.springframework.org/schema/context
                            http://www.springframework.org/schema/context/spring-context-3.0.xsd 
                            http://www.springframework.org/schema/aop
                            http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
                            http://www.springframework.org/schema/tx
                            http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
                            http://www.springframework.org/schema/mvc
                            http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> 

        <bean id="beanName" class="com.zjx.aware.BeanName"></bean>

</beans>

bean類

package com.zjx.aware;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public class BeanName implements BeanNameAware,ApplicationContextAware{
    private String beanName;

    @Override
    public void setApplicationContext(ApplicationContext arg0)
            throws BeansException {
        System.out.println("setApplicationContext中的例項雜湊值:"+arg0.getBean(this.beanName).hashCode());
    }

    @Override
    public void setBeanName(String arg0) {
        this.beanName = arg0;
        System.out.println("BeanName:"+arg0);   
    }

}

測試

package com.zjx.interfaces.test;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.BlockJUnit4ClassRunner;

@RunWith(BlockJUnit4ClassRunner.class)
public class TestAware extends UnitTestBase {
    public TestAware() {
        super("classpath*:spring-aware.xml");
    }

    @Test
    public void test() {
        System.out.println("test方法中的例項雜湊值:"
                + super.getBean("beanName").hashCode());
    }
}

不管是通過test基類獲取的applicationContext還是通過XXXaware介面獲取的applicationContext的雜湊值一致,說明兩種方式都可以訪問到容器上下文物件

相關推薦

springaware介面(5)

2016/1/16 12:45:20 1.Aware spring中提供了一些以Aware結尾的介面,實現了Aware介面的bean在被初始化之後,可以獲取相應資源 通過Aware介面,可以對Spring相應資源進行操作(一定要慎重) 為對Spri

Spring整理系列(06)——springAware結尾介面

一、關於spring中Aware結尾介面介紹: Spring中提供一些Aware結尾相關介面,像是BeanFactoryAware、 BeanNameAware、ApplicationContextAware、ResourceLoaderAware、Servl

springaware接口的

ram 對象 直接 loader number source clas spring servlet 一、關於spring中Aware結尾接口介紹: Spring中提供一些Aware結尾相關接口,像是BeanFactoryAware、 BeanNameAware、Appl

關於 SpringAware 介面

一、Spring所提供的Aware介面: BeanNameAware:在Bean中得到它在IOC容器中的Bean的例項的名字。 BeanFactoryAware:在Bean中得到Bean所在的IOC容器 ApplicationContextAware:在Bean中得到Bean所在的應

SpringAware和Capable的區別和作用

Aware XXXAware在Spring裡表示對XXX可以感知,通俗點解釋就是:如果在某個類裡邊想要使用spring的一些東西,就可以通過實現XXXAware介面告訴Spring, Spring看到後就會給你送過來,而接收的方式是通過實現介面唯一的方法setXXX。 栗子: 比如一個類需要使用當前

【記錄】spring一個介面多個實現類

重構遇到個小問題,記錄下: 錯誤資訊: *************************** APPLICATION FAILED TO START *************************** Description: Field xxxService in com.ali

SpringOrdered介面簡介

目錄 前言 Spring中提供了一個Ordered介面。Ordered介面,顧名思義,就是用來排序的。 Spring是一個大量使用策略設計模式的框架,這意味著有很多相同介面的實現類,那麼必定會有優先順序的問題。 於是,Spring就提供了Ordered這個介面,來

springApplicationContextAware介面的應用

為什麼使用ApplicationContextAware介面: 在spring專案中,類之間的關係是spring容器來管理的,但是一個專案中有些類不受spring容器管理缺需要使用受spring管理的bean,這時候不能通過正常的方式注入bean,這時候spri

SpringApplicationContextAware介面用法

載入Spring配置檔案時,如果Spring配置檔案中所定義的Bean類,如果該類實現了ApplicationContextAware介面,那麼在載入Spring配置檔案時,會自動呼叫ApplicationContextAware介面中的 public void setAp

關於SpringAware介面和後置處理器介面(BeanPostProcessor和BeanFactoryPostProcessor)的一些想法

看了關於Aware介面和後置處理器介面(BeanPostProcessor和BeanFactoryPostProcessor),感覺他們之間的功能有點亂,說一下他們之間的區別: (1)從呼叫時間點上看,Aware介面的方法(都是setXXX方法)是在Bean的屬性被設定之後

SpringServletContextAware介面使用理解

在Spring中,凡是實現ServletContextAware介面的類,都可以取得ServletContext。實現如下:  1 2 3 4 private ServletContext application; public void setSer

springInitializingBean介面使用理解

InitializingBean介面為bean提供了初始化方法的方式,它只包括afterPropertiesSet方法,凡是繼承該介面的類,在初始化bean的時候會執行該方法。 測試程式如下: import org.springframework.beans

springaware介面

實現org.springframework.beans.factory.aware子介面的Bean類在建立時會在呼叫該bean的init方法之前呼叫aware子介面的相應方法,舉幾個類來說明下: org.springframework.context.Application

(轉載)Spring框架的各種*Aware介面

Aware介面 作用: Spring框架提供了多個*Aware介面,用於輔助Spring Bean以程式設計的方式呼叫Spring容器。 通過實現這些介面,可以增強Spring Bean的功能,但是也會造成對Spring容器的繫結。 Aware介面 使用指導:

Spring InitializingBean 介面以及Aware介面實現的原理

     關於Spring InitializingBean 介面以及Aware介面實現的其實都在  第11步中; finishBeanFactoryInitialization() 方法中完成了3部分的內容: 1.完成對單例的非懶載入的bean 進

易學筆記--第2章:spring的Bean/2.5 Bean的週期回撥

  第2章:spring中的Bean/2.5 Bean的週期回撥/2.5.1  概念 概念   這裡的宣告週期指的是Bean在建立完成後和銷燬時這兩個時間點,對於不同的作用域這兩個時間點有所不同

spring原始碼學習(5.1.0版本)——Bean的初始化(

目錄   前言 createBean 有自定義TargetSource代理類的生成 resolveBeforeInstantiation applyBeanPostProcessorsBeforeInstantiation postProcessBeforeIn

spring事務管理,基於xml配置完成事務回滾;spring資料庫表中欄位名和pojo屬性名不一致時候,實現RowMapper介面手動封裝

宣告使用JDK8,spring5.0.7, 測試說明: service 層 宣告介面進行轉賬,從A轉賬B ,然後對AB 進行更新操作,在事務中對find方法開啟 只讀許可權,無法進行更新操作,造成事務回滾進行測試事務; 主要測試方法:* void tra

Spring的InitializingBean介面的使用

  眾所周知Spring框架主要的一個功能是IOC容器,IOC就是控制反轉將本來需要我們手動例項化bean的過程,交給了Spring去完成。讓Spring幫我們去例項化和維護bean。如果我們要在bean例項完成後執行自定義初始化方法怎麼辦呢?在Spring框架中提供了兩種方式,一種是在XML配置bean時指

SpringAware和Capable

spring nvi 實現 方法 lca ntc cap context 需要 Aware XXXAware在Spring裏表示對XXX可以感知,通俗點解釋就是:如果在某個類裏邊想要使用spring的一些東西,就可以通過實現XXXAware接口告訴Spring, Spri