对Spring aware理解

2022-10-10,,

 

 

 aware翻译过来时就是意识到,我对他的理解就是spring的感知器。是不是很诡异这个名字起得^_^

先来看看aware接口的结构

 

 spring提供了许多的aware,aware.java也只是做一个标志,他并没有定义任何的方法

 

 spring提供的这些aware只展示红框框起来的额三个

一、beannameaware

从名字的定义上来看他就是bean对象名字的感知器,他可以获取到bean对象的名字的,目前我只知道在记录日志的时候确实好用,其他的功能有待挖掘。。。。

package com.lhf.aware;

import org.springframework.beans.factory.beannameaware;

public class domebeannameaware implements beannameaware {
    private string beanname;
    @override
    public void setbeanname(string name) {
        this.beanname = name;
    }
    public void test(){
        system.out.println("bean的名称:"+beanname);
    }
}

》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》


package com.lhf.aware;

import org.springframework.context.annotation.bean;
import org.springframework.context.annotation.componentscan;
import org.springframework.context.annotation.configuration;

@configuration
@componentscan("com.lhf.aware")
public class config {

    @bean
    public domebeannameaware domebeannameaware(){
        return new domebeannameaware();
    }
}

》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》


package com.lhf.aware;

import org.springframework.context.annotation.annotationconfigapplicationcontext;

public class app {
    public static void main(string[] args) {
        annotationconfigapplicationcontext context = new annotationconfigapplicationcontext(config.class);
        domebeannameaware bean = context.getbean(domebeannameaware.class);
        bean.test();
        context.close();
    }
}

使用很简单,正如上边代码所示,只需要实现beannameaware接口并重写setbeanname方法即可,该方法的入参就是bean对象的名称。

二、applicationcontextarare

spring容器的感知器,可以获取到spring的容器对象

package com.lhf.aware;

import org.springframework.beans.beansexception;
import org.springframework.beans.factory.disposablebean;
import org.springframework.beans.factory.initializingbean;
import org.springframework.context.applicationcontext;
import org.springframework.context.applicationcontextaware;
import org.springframework.context.support.genericapplicationcontext;
import org.springframework.stereotype.component;

@component
public class domeapplicationcontextaware<t> implements applicationcontextaware, initializingbean, disposablebean {

    private applicationcontext context;

    @override
    public void setapplicationcontext(applicationcontext applicationcontext) throws beansexception {
        this.context = applicationcontext;
        if (context instanceof genericapplicationcontext) {
//            注销spring容器
            ((genericapplicationcontext) applicationcontext).registershutdownhook();
        }
    }


    @override
    public void afterpropertiesset() throws exception {
        system.out.println("bean对象被创建了");
    }

    @override
    public void destroy() throws exception {
        system.out.println("bean对象被销毁了");
    }
}

》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》

package com.lhf.aware;

import org.springframework.context.annotation.componentscan;
import org.springframework.context.annotation.configuration;

@configuration
@componentscan("com.lhf.aware")
public class config {

    
}

》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》

package com.lhf.aware;

import org.springframework.context.annotation.annotationconfigapplicationcontext;

public class app {
    public static void main(string[] args) {
        annotationconfigapplicationcontext context = new annotationconfigapplicationcontext(config.class);
        context.getbean(domeapplicationcontextaware.class);

//        为了证明不是这里关闭的吧下边的一行注释
//        context.close();
    }
}

同样的使用applicationcontextaware同样是实现该接口,并重写set方法。在这个东东很秀,要知道,非spring的对象是不能直接使用spring的bean对象的,而通过这个就可以用了。

三、beanfactoryaware

类似的,beanfactory的感知器用法和上边两种一样,都是实现beanfactoryaware,重写set方法,代码就不贴了。

 

 

总结aware的功能还是很方便的,而且使用起来也很简单粗暴。博客写的不好,大佬勿喷,欢迎吐槽

《对Spring aware理解.doc》

下载本文的Word格式文档,以方便收藏与打印。