共计 3209 个字符,预计需要花费 9 分钟才能阅读完成。
Aware 主动注入
- BeanNameAware
- BeanFactoryAware
- ApplicationContextAware
- MessageSourceAware
- ApplicationEventPublisherAware
- ResourceLoaderAware
Aware 接口 | 形容 |
---|---|
BeanNameAware | 能够在 Bean 中失去它在 IOC 容器中的 Bean 的实例的名字 |
BeanFactoryAware | 能够在 Bean 中失去 Bean 所在的 IOC 容器,从而间接在 Bean 中应用 IOC 容器的服务。 |
ApplicationContextAware | 能够在 Bean 中失去 Bean 所在的利用上下文,从而间接在 Bean 中应用上下文的服务。 |
MessageSourceAware | 在 Bean 中能够失去音讯源。 |
ApplicationEventPublisherAware | 在 bean 中能够失去利用上下文的事件公布器,从而能够在 Bean 中公布利用上下文的事件。 |
ResourceLoaderAware | 在 Bean 中能够失去 ResourceLoader,从而在 bean 中应用 ResourceLoader 加载内部对应的 Resource 资源。在设置 Bean 的属性之后,调用初始化回调办法之前,Spring 会调用 aware 接口中的 setter 办法。 |
实例类 Holder/Rumenz
package com.rumenz;
import org.springframework.beans.factory.BeanNameAware;
public class Holder implements BeanNameAware {
private Rumenz rumenz;
public Holder(Rumenz rumenz) {this.rumenz = rumenz;}
public Rumenz getRumenz() {return rumenz;}
public void setRumenz(Rumenz rumenz) {this.rumenz = rumenz;}
@Override
public void setBeanName(String name) {System.out.println("BeanName:"+name);
}
}
// Rumenz.java
package com.rumenz;
public class Rumenz {
private Integer id;
private String name;
public Integer getId() {return id;}
public void setId(Integer id) {this.id = id;}
public String getName() {return name;}
public void setName(String name) {this.name = name;}
}
配置文件 Beans.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:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util https://www.springframework.org/schema/util/spring-util.xsd">
<bean id="rumenz" class="com.rumenz.Rumenz">
<property name="id" value="123"/>
<property name="name" value="入门小站"/>
</bean>
调用
package com.rumenz;
public class DemoApplication implements BeanNameAware, BeanFactoryAware, ApplicationContextAware, MessageSourceAware, ApplicationEventPublisher,ResourceLoaderAware {
@Autowired
private Holder holder;
private static BeanFactory beanFactory;
private static ApplicationContext applicationContext;
public static void main(String[] args) {AnnotationConfigApplicationContext ac=new AnnotationConfigApplicationContext();
XmlBeanDefinitionReader xr=new XmlBeanDefinitionReader(ac);
xr.loadBeanDefinitions("Beans.xml");
ac.register(DemoApplication.class);
ac.refresh();
DemoApplication bean = ac.getBean(DemoApplication.class);
System.out.println(bean.holder.getRumenz().getName());
System.out.println(applicationContext==ac);
System.out.println(ac.getBeanFactory()==beanFactory);
ac.close();}
@Bean
public Holder holder(Rumenz rumenz){return new Holder(rumenz);
}
@Override
public void setBeanName(String name) {System.out.println("BeanName =:"+name);
}
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {this.beanFactory=beanFactory;}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {this.applicationContext=applicationContext;}
@Override
public void setMessageSource(MessageSource messageSource) {System.out.println("messageSource"+messageSource);
}
@Override
public void publishEvent(ApplicationEvent event) { }
@Override
public void publishEvent(Object event) { }
@Override
public void setResourceLoader(ResourceLoader resourceLoader) {}}
原文: https://rumenz.com/rumenbiji/Spring-Aware-Injection.html
正文完