对于实现了org.springframework.beans.factory.BeanFactoryAware接口的类, 当它被BeanFactory创建后,它会拥有一个指向创建它的BeanFactory的引用。 也就是说光实现BeanFactoryAware接口还是不够得,那个实现BeanFactoryAware接口的类必须是由spring管理的,不然在你那个类里面创建BeanFactory是不会成功的.(spring容器在解析配置文件的时候,会实例化那些由他管理的bean,当然其中肯定有一个就是我们的那个实现了beanFactoryWare接口的类,在实例化的过程中,会把beanfactory 注入到setBeanFactory(BeanFactory beanFactory)方法里面去)
public interface BeanFactoryAware {
/**
* Callback that supplies the owning factory to a bean instance.
* <p>Invoked after the population of normal bean properties
* but before an initialization callback such as
* {@link InitializingBean#afterPropertiesSet()} or a custom init-method.
* @param beanFactory owning BeanFactory (never <code>null</code>).
* The bean can immediately call methods on the factory.
* @throws BeansException in case of initialization errors
* @see BeanInitializationException
*/
void setBeanFactory(BeanFactory beanFactory) throws BeansException;
}
具体代码: package ch5.BeanAware;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import ch5.destroy.ShutDownHook;
public class SimpleBean implements InitializingBean,DisposableBean,BeanNameAware,BeanFactoryAware,Runnable ...{
private ConfigurableListableBeanFactory configurableListableBeanFactory;
public void setBeanName(String beanName) ...{
System.out.println("this is info from BeanNameAware and beanName is:"+beanName);
}
public void setBeanFactory(BeanFactory factory) throws BeansException ...{
//自动注册成线程
if(factory instanceof ConfigurableListableBeanFactory)...{
System.out.println("this is info from BeanNameAware and beanName");
this.configurableListableBeanFactory=(ConfigurableListableBeanFactory)factory;
Runtime.getRuntime().addShutdownHook(new Thread(this));
}
}
public void afterPropertiesSet() throws Exception ...{
System.out.println("this is info from afterpropertiesSet from SimpleBean");
}
public void run() ...{
//线程调用destroySingletons
configurableListableBeanFactory.destroySingletons();
}
private String name;
private String sex;
private String age;
public void destroyMethod()...{
System.out.println("this is info from customer destroy method");
}
public void destroy()...{
System.out.println("this is info from destroy method");
}
public String getAge() ...{
return age;
}
public void setAge(String age) ...{
this.age = age;
}
public String getName() ...{
return name;
}
public void setName(String name) ...{
this.name = name;
}
public String getSex() ...{
return sex;
}
public void setSex(String sex) ...{
this.sex = sex;
}
}
package ch5.BeanAware;
import java.io.File;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.FileSystemResource;
public class TestSpring...{
public static void main(String args[]) throws Exception...{
//获取bean factory
ConfigurableListableBeanFactory factory=(ConfigurableListableBeanFactory)getBeanFactory(); //使用子beanFactory
factory.preInstantiateSingletons();
SimpleBean bean1=(SimpleBean)factory.getBean("SimpleBean");
}
public static BeanFactory getBeanFactory()...{
//获取bean factory
String realpath="";
//加载配置项
realpath=System.getProperty("user.dir")+File.separator+"src"+File.separator+"ch5/destroy"+File.separator+"applicationContext.xml";
ConfigurableListableBeanFactory factory=new XmlBeanFactory(new FileSystemResource(realpath));
return factory;
}
}
配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="SimpleBean" class="ch5.destroy.SimpleBean" destroy-method="destroyMethod">
<property name="name">
<value>gaoxiang</value>
</property>
<property name="sex">
<value>male</value>
</property>
<property name="age">
<value>26</value>
</property>
</bean>
</beans>
分享到:
相关推荐
com-spring-ioc-demo:源码主要是学习Spring IOC的原理,以及对Bean的注册及控制,主要运用以下类对Spring进行扩展学习:BeanPostProcessor,BeanFactoryAware,BeanNameAware,ApplicationContextAware,FactoryBean...
获取 Spring 里注册的 Bean 对象可以使用四种方法:继承 BaseDispatchAction、实现 BeanFactoryAware、使用 ApplicationContext、使用 @Autowired。每种方法都有其特点和应用场景,开发者可以根据实际情况选择合适的...
`BeanFactoryAware`接口使得Bean可以被注入`BeanFactory`,而`ApplicationListener`接口则可以让Bean监听到应用上下文的刷新事件,从而在合适的时机执行动态注册逻辑。 ```java public class BeanFactoryAwareBean ...
另一种获取Bean的方法是让类实现`BeanFactoryAware`接口。Spring容器会自动调用该接口的`setBeanFactory(BeanFactory beanFactory)`方法,将整个`BeanFactory`实例注入到实现了该接口的类中。这样就可以通过`...
BeanFactoryAware接口提供了一个方法`setBeanFactory(BeanFactory beanFactory)`,允许bean获取到当前的BeanFactory实例。这样,bean就能在运行时动态地查找和依赖其他bean,或者利用BeanFactory提供的功能,如工厂...
10. **Aware接口**:Spring提供了一系列的Aware接口,如BeanNameAware、BeanFactoryAware和ApplicationContextAware,这些接口使得Bean可以在运行时获取自身的一些信息,例如Bean的名字、所处的Bean工厂或...
- 如果Bean实现了特定的Spring接口,如`BeanNameAware`,`BeanFactoryAware`,`ApplicationContextAware`,Spring会在适当的时间调用相应的方法。 - `BeanNameAware`的`setBeanName()`方法用于将Bean的ID传递给...
`BeanFactoryAware`接口包含`setBeanFactory(BeanFactory)`方法。实现这个接口的类可以获取到装载当前Bean的`BeanFactory`,这允许类直接访问容器和其中的其他Bean,提供了更大的灵活性。 5. **FactoryBean接口**...
3. 处理BeanNameAware和BeanFactoryAware接口:如果Bean实现了BeanNameAware或BeanFactoryAware接口,Spring会调用相应的方法,将Bean的ID和BeanFactory设置到Bean中。 4. BeanPostProcessor的前置处理:在Bean的...
- Spring容器的生命周期包括对象实例化、属性设置、接口回调(如`BeanNameAware`、`BeanFactoryAware`等)以及初始化和销毁方法的调用。 6. Spring生命周期的11步: - 对象实例化:创建Bean对象。 - 属性封装:...
5. **BeanFactoryAware 接口回调**:如果该 Bean 实现了 `BeanFactoryAware` 接口,Spring 将 BeanFactory 传递给 `setBeanFactory()` 方法。 6. **调用 BeanPostProcessor.postProcessBeforeInitialization 方法**...
2. **XxxAware接口**:如ApplicationContextAware和BeanFactoryAware,可以让我们获取到ApplicationContext或BeanFactory,从而在Bean中直接访问到整个应用上下文或Bean工厂,进行更深入的定制。 3. **@Autowired...
2. BeanFactoryAware:在`setBeanName`之后,Spring会调用`setBeanFactory`方法,将BeanFactory对象注入到Bean中,使Bean能够访问容器。 3. InitializingBean:这个接口提供了一个`afterPropertiesSet`方法,在所有...
④ 如果 Bean 实现 BeanFactoryAware 或者 ApplicationContextAware 设置工厂 setBeanFactory 或者上下文对象 setApplicationContext ⑤ 如果存在类实现 BeanPostProcessor(后处理 Bean),执行 ...
3. **Bean接口回调**:如果Bean实现了特定的接口,如`BeanNameAware`、`BeanFactoryAware`或`ApplicationContextAware`,Spring会在适当的时候调用对应的回调方法。这些接口允许Bean获取其ID、BeanFactory引用或...
6. **BeanFactoryAware**:此接口使得Bean可以在运行时显式查找其依赖的其他对象。BeanFactory通过`setBeanFactory(BeanFactory beanFactory)`方法传递自身,使得Bean可以直接使用BeanFactory来获取依赖。然而,这种...
- **BeanFactoryAware**:该接口的`setBeanFactory`方法会被Spring容器回调,将当前的`BeanFactory`实例传递给bean,便于bean访问容器内的其他bean。 - **ApplicationContextAware**:此接口的`...
5. 调用`initializeBean(...)`,其中包括执行`aware`接口方法,如`BeanFactoryAware`, `ApplicationContextAware`等,以设置属性值。 6. 应用`BeanPostProcessorBefore`和`BeanPostProcessorAfter`的前置和后置增强...
- 实现 `BeanFactoryAware` 的 Bean 会通过 `setBeanFactory` 方法得到 `BeanFactory`,以便于访问容器服务。 - 实现 `ApplicationContextAware` 的 Bean 可以通过 `setApplicationContext` 获取 `...