- //将applicationContext转换为ConfigurableApplicationContext
- ConfigurableApplicationContext configurableApplicationContext = (ConfigurableApplicationContext) applicationContext;
- // 获取bean工厂并转换为DefaultListableBeanFactory
- DefaultListableBeanFactory defaultListableBeanFactory = (DefaultListableBeanFactory) configurableApplicationContext
- .getBeanFactory();
- // 通过BeanDefinitionBuilder创建bean定义
- BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder
- .genericBeanDefinition(UserService.class);
- // 设置属性userAcctDAO,此属性引用已经定义的bean:userAcctDAO
- beanDefinitionBuilder
- .addPropertyReference("userAcctDAO", "UserAcctDAO");
- // 注册bean
- defaultListableBeanFactory.registerBeanDefinition("sdfds",
- beanDefinitionBuilder.getRawBeanDefinition());
User.Java
- public class User {
- private String username;
- private String password;
- public String getUsername() {
- return username;
- }
- public void setUsername(String username) {
- this.username = username;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- }
UserService.java
- public class UserService {
- private User user;
- public void test(){
- System.out.println(user.getUsername()+"--------------------");
- }
- public User getUser() {
- return user;
- }
- public void setUser(User user) {
- this.user = user;
- }
- }
示例:
BeanFactoryAwareBean.java
- import org.springframework.beans.BeansException;
- import org.springframework.beans.factory.BeanFactory;
- import org.springframework.beans.factory.BeanFactoryAware;
- import org.springframework.beans.factory.support.BeanDefinitionBuilder;
- import org.springframework.beans.factory.support.DefaultListableBeanFactory;
- import org.springframework.context.ApplicationListener;
- import org.springframework.context.event.ContextRefreshedEvent;
- public class BeanFactoryAwareBean implements BeanFactoryAware,
- ApplicationListener<ContextRefreshedEvent> {
- private DefaultListableBeanFactory beanFactory;
- public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
- System.out.println("setBeanFactory.........................");
- this.beanFactory = (DefaultListableBeanFactory) beanFactory;
- }
- public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
- System.out.println("ContextRefreshed...................");
- BeanDefinitionBuilder userBeanDefinitionBuilder = BeanDefinitionBuilder
- .genericBeanDefinition(User.class);
- userBeanDefinitionBuilder.addPropertyValue("username", "chinfeng");
- userBeanDefinitionBuilder.addPropertyValue("password", "123456");
- beanFactory.registerBeanDefinition("user",
- userBeanDefinitionBuilder.getRawBeanDefinition());
- BeanDefinitionBuilder usersBeanDefinitionBuilder = BeanDefinitionBuilder
- .genericBeanDefinition(UserService.class);
- usersBeanDefinitionBuilder.addPropertyReference("user", "user");
- beanFactory.registerBeanDefinition("userService",
- usersBeanDefinitionBuilder.getRawBeanDefinition());
- }
- }
applicationContext.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:p="http://www.springframework.org/schema/p"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
- <bean id="beanFactoryAwareBean" class="BeanFactoryAwareBean"></bean>
- </beans>
结果:setBeanFactory.........................
ContextRefreshed...................
chinfeng--------------------