spring中注册bean(通过代码动态注册)

  1. //将applicationContext转换为ConfigurableApplicationContext  
  2. ConfigurableApplicationContext configurableApplicationContext = (ConfigurableApplicationContext) applicationContext;  
  3.   
  4. // 获取bean工厂并转换为DefaultListableBeanFactory  
  5. DefaultListableBeanFactory defaultListableBeanFactory = (DefaultListableBeanFactory) configurableApplicationContext  
  6.         .getBeanFactory();  
  7.   
  8. // 通过BeanDefinitionBuilder创建bean定义  
  9. BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder  
  10.         .genericBeanDefinition(UserService.class);  
  11. // 设置属性userAcctDAO,此属性引用已经定义的bean:userAcctDAO  
  12. beanDefinitionBuilder  
  13.         .addPropertyReference("userAcctDAO""UserAcctDAO");  
  14.   
  15. // 注册bean  
  16. defaultListableBeanFactory.registerBeanDefinition("sdfds",  
  17.         beanDefinitionBuilder.getRawBeanDefinition());  

User.Java

  1. public class User {  
  2.   
  3.     private String username;  
  4.     private String password;  
  5.   
  6.     public String getUsername() {  
  7.         return username;  
  8.     }  
  9.   
  10.     public void setUsername(String username) {  
  11.         this.username = username;  
  12.     }  
  13.   
  14.     public String getPassword() {  
  15.         return password;  
  16.     }  
  17.   
  18.     public void setPassword(String password) {  
  19.         this.password = password;  
  20.     }  
  21.   
  22. }  

UserService.java
  1. public class UserService {  
  2.   
  3.     private User user;  
  4.       
  5.       
  6.     public void test(){  
  7.         System.out.println(user.getUsername()+"--------------------");  
  8.     }  
  9.   
  10.     public User getUser() {  
  11.         return user;  
  12.     }  
  13.       
  14.       
  15.   
  16.     public void setUser(User user) {  
  17.         this.user = user;  
  18.     }  
  19. }  


示例:

BeanFactoryAwareBean.java

  1. import org.springframework.beans.BeansException;  
  2. import org.springframework.beans.factory.BeanFactory;  
  3. import org.springframework.beans.factory.BeanFactoryAware;  
  4. import org.springframework.beans.factory.support.BeanDefinitionBuilder;  
  5. import org.springframework.beans.factory.support.DefaultListableBeanFactory;  
  6. import org.springframework.context.ApplicationListener;  
  7. import org.springframework.context.event.ContextRefreshedEvent;  
  8.   
  9. public class BeanFactoryAwareBean implements BeanFactoryAware,  
  10.         ApplicationListener<ContextRefreshedEvent> {  
  11.     private DefaultListableBeanFactory beanFactory;  
  12.   
  13.     public void setBeanFactory(BeanFactory beanFactory) throws BeansException {  
  14.         System.out.println("setBeanFactory.........................");  
  15.         this.beanFactory = (DefaultListableBeanFactory) beanFactory;  
  16.     }  
  17.   
  18.     public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {  
  19.         System.out.println("ContextRefreshed...................");  
  20.   
  21.         BeanDefinitionBuilder userBeanDefinitionBuilder = BeanDefinitionBuilder  
  22.                 .genericBeanDefinition(User.class);  
  23.         userBeanDefinitionBuilder.addPropertyValue("username""chinfeng");  
  24.         userBeanDefinitionBuilder.addPropertyValue("password""123456");  
  25.         beanFactory.registerBeanDefinition("user",  
  26.                 userBeanDefinitionBuilder.getRawBeanDefinition());  
  27.   
  28.         BeanDefinitionBuilder usersBeanDefinitionBuilder = BeanDefinitionBuilder  
  29.                 .genericBeanDefinition(UserService.class);  
  30.         usersBeanDefinitionBuilder.addPropertyReference("user""user");  
  31.         beanFactory.registerBeanDefinition("userService",  
  32.                 usersBeanDefinitionBuilder.getRawBeanDefinition());  
  33.     }  
  34. }  

applicationContext.xml
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"  
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">  
  5.     <bean id="beanFactoryAwareBean" class="BeanFactoryAwareBean"></bean>  
  6. </beans>  

结果:setBeanFactory.........................
ContextRefreshed...................
chinfeng--------------------
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值