大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
本篇内容主要讲解“如何自动装配-Aware注入Spring底层组件及原理”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“如何自动装配-Aware注入Spring底层组件及原理”吧!
创新互联公司长期为上千余家客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为库尔勒企业提供专业的成都网站设计、网站制作,库尔勒网站改版等技术服务。拥有十多年丰富建站经验和众多成功案例,为您定制开发。
Aware 接口,提供了类似回调函数的功能
自定义组件想要使用Spring 容器底层的一些组件(Application Context,Bean Factory);自定义组件需要实现xxxAware接口;在创建对象的时候,会调用接口规定的方法注入相关组件
package org.springframework.beans.factory; public interface Aware { }
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; }
package org.springframework.context; import org.springframework.beans.factory.Aware; public interface ApplicationEventPublisherAware extends Aware { void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher); }
package org.springframework.beans.factory; public interface BeanClassLoaderAware extends Aware { void setBeanClassLoader(ClassLoader classLoader); }
package org.springframework.beans.factory; import org.springframework.beans.BeansException; public interface BeanFactoryAware extends Aware { void setBeanFactory(BeanFactory beanFactory) throws BeansException; }
package org.springframework.beans.factory; public interface BeanNameAware extends Aware { void setBeanName(String name); }
package org.springframework.context; import org.springframework.beans.factory.Aware; import org.springframework.util.StringValueResolver; public interface EmbeddedValueResolverAware extends Aware { void setEmbeddedValueResolver(StringValueResolver resolver); }
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); }
package org.springframework.context.annotation; import org.springframework.beans.factory.Aware; import org.springframework.core.type.AnnotationMetadata; public interface ImportAware extends Aware { void setImportMetadata(AnnotationMetadata importMetadata); }
package org.springframework.context.weaving; import org.springframework.beans.factory.Aware; import org.springframework.instrument.classloading.LoadTimeWeaver; public interface LoadTimeWeaverAware extends Aware { void setLoadTimeWeaver(LoadTimeWeaver loadTimeWeaver); }
package org.springframework.context; import org.springframework.beans.factory.Aware; public interface MessageSourceAware extends Aware { void setMessageSource(MessageSource messageSource); }
package org.springframework.jmx.export.notification; import org.springframework.beans.factory.Aware; public interface NotificationPublisherAware extends Aware { void setNotificationPublisher(NotificationPublisher notificationPublisher); }
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); }
package com.hw.springannotation.beans; 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; /** * @Description TODO * @Author hw * @Date 2018/11/28 15:44 * @Version 1.0 */ @Component public class Red implements ApplicationContextAware, BeanNameAware, EmbeddedValueResolverAware { private ApplicationContext applicationContext; public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { System.out.println("传入的IOC " + applicationContext); this.applicationContext = applicationContext; } public void setBeanName(String name) { System.out.println("当前bean的名字" + name); } public void setEmbeddedValueResolver(StringValueResolver resolver) { String value = resolver.resolveStringValue("你好${os.name} 我是#{20*20}"); System.out.println("解析的字符串:" + value); } }
运行:
说白了 Aware接口类,就是一个回调类的抽象
到此,相信大家对“如何自动装配-Aware注入Spring底层组件及原理”有了更深的了解,不妨来实际操作一番吧!这里是创新互联网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!