Aware自动装配

作用:自定义组件实现xxxAware来使用Spring容器底层的一些组件

  • Aware 接口,提供了类似回调函数的功能
  • 自定义组件想要使用Spring 容器底层的一些组件(Application Context,Bean Factory);自定义组件需要实现xxxAware接口;在创建对象的时候,会调用接口规定的方法注入相关组件
package org.springframework.beans.factory;
public interface Aware {
}

1、ApplicationContextAware 自动注入IOC容器

package org.springframework.context;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.Aware;

public interface ApplicationContextAware extends Aware {
    void setApplicationContext(ApplicationContext applicationContext) throws BeansException;
}

2、ApplicationEventPublisherAware 注入事件派发器

package org.springframework.context;

import org.springframework.beans.factory.Aware;

public interface ApplicationEventPublisherAware extends Aware {
    void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher);
}

3、BeanClassLoaderAware 类加载器

package org.springframework.beans.factory;

public interface BeanClassLoaderAware extends Aware {
    void setBeanClassLoader(ClassLoader classLoader);
}

4、BeanFactoryAware Bean工厂

package org.springframework.beans.factory;

import org.springframework.beans.BeansException;
public interface BeanFactoryAware extends Aware {
    void setBeanFactory(BeanFactory beanFactory) throws BeansException;
}

5、BeanNameAware Bean名字

package org.springframework.beans.factory;

public interface BeanNameAware extends Aware {
    void setBeanName(String name);
}

6、EmbeddedValueResolverAware Embedded值解析器

package org.springframework.context;

import org.springframework.beans.factory.Aware;
import org.springframework.util.StringValueResolver;

public interface EmbeddedValueResolverAware extends Aware {
    void setEmbeddedValueResolver(StringValueResolver resolver);
}

7、EnvironmentAware 环境

package org.springframework.context;

import org.springframework.beans.factory.Aware;
import org.springframework.core.env.Environment;

public interface EnvironmentAware extends Aware {
    void setEnvironment(Environment environment);
}

8、ResourceLoaderAware 资源加载器

package org.springframework.context;

import org.springframework.beans.factory.Aware;
import org.springframework.core.io.ResourceLoader;

public interface ResourceLoaderAware extends Aware {
    void setResourceLoader(ResourceLoader resourceLoader);
}

实例:

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.EmbeddedValueResolverAware;
import org.springframework.stereotype.Component;
import org.springframework.util.StringValueResolver;

//实现 BeanNameAware 与 ApplicationContextAware接口
@Component
public class Light implements ApplicationContextAware, BeanNameAware, EmbeddedValueResolverAware {
	private ApplicationContext applicationContext;
	
	@Override
	public void setBeanName(String name) {
		System.out.println("当前bean的名字:"+name);
	}

	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
			System.out.println("传入的IOC容器: "+applicationContext);
			this.applicationContext = applicationContext;
	}

	@Override
	public void setEmbeddedValueResolver(StringValueResolver resolver) {
		String result = resolver.resolveStringValue("你好${os.name}, 计算#{3*8}");
		System.out.println("解析的字符串为---"+result);		
	}	
}

测试类

public class Cap9Test {
	@Test
	public void test01(){
		AnnotationConfigApplicationContext app = new AnnotationConfigApplicationContext(Cap9MainConfig.class);
		System.out.println(app);
		app.close();	
	}
}

Aware自动装配

转载:

https://www.cnblogs.com/Grand-Jon/p/10040199.html

上一篇:如果你使用 true 和 false,会出现以下错误:我ever和


下一篇:Spring 中的 Aware 相关接口