0.2, Spring Source learn: * Aware from BeanNameAware to recognize a Spring bean

Copyright Notice: Welcome to reprint exchange, to declare the source. Performance status prior to the state of mind, habits prior to determination, focus first on preferences --Bestcxx https://blog.csdn.net/bestcxx/article/details/90521143

Foreword

Performance status prior to the state of mind, habits prior to determination, focus first on preferences

Aware: sensing

Spring org.springframework.beans.factory.Aware an interface is provided
such comment on this interface are described:
a super interface which represents a bean able to be qualified for a particular frame object --Spring container by callback- style method of notification.
this interface implementation is done by subclasses, is part of a typical void return type of the set method
for this interface does not provide any default abstract methods, so the subclass must explicitly do something , such as in the org.springframework.beans.factory.config.BeanPostProcessor
can be achieved with reference to two classes, org.springframework.context.support.ApplicationContextAwareProcessor and org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory BeanPostProcessor, which has to * Aware of application examples
here * Aware Aware indicate there are many sub-interfaces, such as BeanNameAware, BeanFactoryAware, BeanClassLoaderAware, EnvironmentAware

Aware of the sub-interfaces inherit

Many, such as BeanNameAware interface is perceived Spring Bean's Name of
BeanClassLoaderAware the interface class loader is the perception of Spring Bean of
these interfaces need to be implemented to write your own Spring bean, and then overwrites the set method
In addition, there are some global property can be obtained by way of injection, such EnvironmentContextAware

Here Insert Picture Description

Speaking from BeanNameAware

org.springframework.beans.factory.BeanNameAware
content from annotation: a interface implemented by beans, bean used to obtain these names in the bean factory.
But generally speaking, do not recommend the use of an object depend on the bean name , but also on the Spring API adds a potential vulnerability configuration, it is not necessary
similar to the interface as shown in FIG.

Usage example

Write a Spring Bean implement several * Aware Interface

These interfaces really are only a set method, we can get the Spring container based on information related to this bean, here I just print it out, of course, you can also have other operations
where we get to be loaded this Spring bean bean name in the Spring container , class loader, manage Spring bean of Spring factory- most amazing classic DefaultListableBeanFactory

package com.bestcxx.stu.test;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanClassLoaderAware;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.stereotype.Component;

/**
 *  Aware 感知
 *  测试类加载时 BeanNameAware、BeanFactoryAware、BeanClassLoaderAware 被调用的set 方法
 * @author jie.wu
 *
 */
@Component
public class AInterfaceImpl implements BeanNameAware,BeanFactoryAware,BeanClassLoaderAware {
	
	@Override
	public void setBeanClassLoader(ClassLoader classLoader) {
		System.out.println("我的类加载器是"+classLoader);
	}

	@Override
	public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
		System.out.println("我的 beanFactory 是"+beanFactory);
	}

	@Override
	public void setBeanName(String name) {
		System.out.println("我的 BeanName 是"+name);
	}
}
xml
<?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:context="http://www.springframework.org/schema/context"    
    xmlns:tx="http://www.springframework.org/schema/tx"    
    xmlns:aop="http://www.springframework.org/schema/aop"    
    xmlns:util="http://www.springframework.org/schema/util"    
    xmlns:orm="http://www.springframework.org/schema/orm"     
    xmlns:mvc="http://www.springframework.org/schema/mvc"    
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd    
                        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd    
                        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd    
                        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd    
                        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd    
                        http://www.springframework.org/schema/orm http://www.springframework.org/schema/orm/spring-orm-4.3.xsd    
                        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd    
    "
    >  
    <context:component-scan base-package="com.bestcxx.stu.test"/>
</beans>  
Test Methods
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class AInterfaceTest {
	
	/**
	 * 只要加载配置文件就会自动加载 bean
	 */
	@Test
	public void test() {
		ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:test/applicationcontext-test.xml");
	}
}

Test Results
我的 BeanName 是AInterfaceImpl
我的类加载器是sun.misc.Launcher$AppClassLoader@4b1210ee
我的 beanFactory 是org.springframework.beans.factory.support.DefaultListableBeanFactory@76b10754: ...

I see three special class in the source code of * Aware

org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory
, there is the following piece of code, a display BeanNameAware, BeanFactoryAware BeanClassLoaderAware and, dependent on the meaning of these three interfaces are ignored in the

public AbstractAutowireCapableBeanFactory() {
	super();
	ignoreDependencyInterface(BeanNameAware.class);
	ignoreDependencyInterface(BeanFactoryAware.class);
	ignoreDependencyInterface(BeanClassLoaderAware.class);
}

Guess you like

Origin blog.csdn.net/bestcxx/article/details/90521143