Understanding of Spring aware

 

 

When aware translated it, I realized that my understanding of him was spring's perceptron. Isn't that a weird name^_^

Let's first look at the structure of aware interface

 

spring provides a lot of aware. Aware.java just makes a flag. It doesn't define any methods

 

These are provided by spring only show the amount of three in red frame

1, BeanNameAware

From the definition of the name, he is the perceptron of the bean object name. He can get the bean object name. At present, I only know that it is easy to use when logging, and other functions need to be explored....

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 Name:"+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();
    }
}

It is easy to use. As shown in the above code, you only need to implement the BeanNameAware interface and override the setBeanName method, whose input parameter is the name of the bean object.

2, ApplicationContextArare

The sensor of spring container, which can get the container object of 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) {
//            Cancellation spring container
            ((GenericApplicationContext) applicationContext).registerShutdownHook();
        }
    }


    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("bean Object created");
    }

    @Override
    public void destroy() throws Exception {
        System.out.println("bean Object destroyed");
    }
}

>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

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);

//        To prove it's not closed here, the line below
//        context.close();
    }
}

Using ApplicationContextAware also implements the interface and rewrites the set method. It's very popular here. You should know that non spring objects can't directly use spring bean objects, and you can use it through this.

3, BeanFactoryAware

Similarly, the perceptron usage of beanFactory is the same as the above two. It implements BeanFactoryAware, rewrites the set method, and the code will not be pasted.

 

 

It is very convenient to summarize the functions of aware, and it is also very simple and rough to use. Blog writing is not good.

Keywords: Java Spring

Added by ali_2kool2002 on Tue, 14 Jan 2020 05:40:31 +0200