1、

import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.GenericBeanDefinition; import java.util.Date; /**
* @author www.gomepay.com
* @date 2019/11/14
*/
public class GenericBeanDefinitionExample {
public static void main (String[] args) {
DefaultListableBeanFactory context = new DefaultListableBeanFactory(); GenericBeanDefinition gbd = new GenericBeanDefinition();
gbd.setBeanClass(MyBean.class); MutablePropertyValues mpv = new MutablePropertyValues();
mpv.add("date", new Date()); //alternatively we can use:
// gbd.getPropertyValues().addPropertyValue("date", new Date());
gbd.setPropertyValues(mpv); context.registerBeanDefinition("myBeanName", gbd); MyBean bean = context.getBean(MyBean.class);
bean.doSomething();
}
}public class MyBean {
private Date date; public void doSomething () {
System.out.println("from my bean, date: " + date);
} public void setDate (Date date) {
this.date = date;
}
}

输出:from my bean, date: Fri Nov 15 14:16:37 GMT+08:00 2019

2、

import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.DefaultListableBeanFactory; /**
* @author www.gomepay.com
* @date 2019/11/15
*/
public class BeanDefinitionBuilderExample {
public static void main (String[] args) {
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
BeanDefinitionBuilder b =
BeanDefinitionBuilder.rootBeanDefinition(MyBean.class)
.addPropertyValue("str", "myStringValue");
beanFactory.registerBeanDefinition("myBean", b.getBeanDefinition()); MyBean bean = beanFactory.getBean(MyBean.class);
bean.doSomething();
}
}
public class MyBean {
private String str; public void setStr (String str) {
this.str = str;
} public void doSomething () {
System.out.println("from MyBean " + str);
}
}

输出:from MyBean myStringValue

3、

import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.GenericBeanDefinition; /**
* @author www.gomepay.com
* @date 2019/11/15
*/
public class InjectingOtherBeans {
public static void main(String[] args) {
DefaultListableBeanFactory context = new DefaultListableBeanFactory();
//define and register MyOtherBean
GenericBeanDefinition beanOtherDef = new GenericBeanDefinition();
beanOtherDef.setBeanClass(MyOtherBean.class);
context.registerBeanDefinition("other", beanOtherDef); //definine and register myBean
GenericBeanDefinition beanDef = new GenericBeanDefinition();
beanDef.setBeanClass(MyBean.class);
MutablePropertyValues mpv = new MutablePropertyValues();
mpv.addPropertyValue("otherBean", context.getBean("other"));
beanDef.setPropertyValues(mpv);
context.registerBeanDefinition("myBean", beanDef); //using MyBean instance
MyBean bean = context.getBean(MyBean.class);
bean.doSomething();
}
}
class MyBean {
private MyOtherBean otherBean; public void setOtherBean (MyOtherBean otherBean) {
this.otherBean = otherBean;
} public void doSomething () {
otherBean.doSomething();
}
}
class MyOtherBean {
public void doSomething () {
System.out.println("from other bean ");
}
}

输出:from other bean

4、

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.beans.factory.support.GenericBeanDefinition; public class MyConfigBean implements BeanFactoryPostProcessor { @Override
public void postProcessBeanFactory (
ConfigurableListableBeanFactory beanFactory)
throws BeansException {
GenericBeanDefinition bd = new GenericBeanDefinition();
bd.setBeanClass(MyBean.class);
bd.getPropertyValues().add("strProp", "my string property");
((DefaultListableBeanFactory) beanFactory).registerBeanDefinition("myBeanName", bd);
}
}
@Configuration
class MyConfig {
@Bean
MyConfigBean myConfigBean () {
return new MyConfigBean();
}
}
class MyBean {
private String strProp; public void setStrProp (String strProp) {
this.strProp = strProp;
} public void doSomething () {
System.out.println("from MyBean: " + strProp);
}
}
public class BeanFactoryPostProcessorExample {
public static void main (String[] args) {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(MyConfig.class);
MyBean bean = context.getBean(MyBean.class);
bean.doSomething();
}
}

输出:from MyBean:  my string property

5、

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.beans.factory.support.GenericBeanDefinition; public class MyConfigBean implements BeanDefinitionRegistryPostProcessor {
@Override
public void postProcessBeanDefinitionRegistry (BeanDefinitionRegistry registry)
throws BeansException {
GenericBeanDefinition bd = new GenericBeanDefinition();
bd.setBeanClass(MyBean.class);
bd.getPropertyValues().add("strProp", "my string property");
registry.registerBeanDefinition("myBeanName", bd);
}
@Override
public void postProcessBeanFactory (ConfigurableListableBeanFactory beanFactory)
throws BeansException {
//no op
}
}
@Configuration
public class MyConfig {
@Bean
MyConfigBean myConfigBean () {
return new MyConfigBean();
}
}
public class MyBean {
private String strProp;
public void setStrProp (String strProp) {
this.strProp = strProp;
}
public void doSomething () {
System.out.println("from MyBean: " + strProp);
}
}
public class BeanDefinitionRegistryPostProcessorExample {
public static void main (String[] args) {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(MyConfig.class);
MyBean bean = (MyBean) context.getBean("myBeanName");
bean.doSomething();
}
}

输出:from MyBean:  my string property

spring 动态bean注册的更多相关文章

  1. Spring动态注册bean实现动态多数据源

    Spring动态注册bean实现动态多数据源 http://blog.csdn.net/littlechang/article/details/8071882

  2. Spring Bean注册解析(二)

           在上文Spring Bean注册解析(一)中,我们讲解了Spring在注册Bean之前进行了哪些前期工作,以及Spring是如何存储注册的Bean的,并且详细介绍了Spring是如何解析 ...

  3. Spring Bean注册解析(一)

           Spring是通过IoC容器对Bean进行管理的,而Bean的初始化主要分为两个过程:Bean的注册和Bean实例化.Bean的注册主要是指Spring通过读取配置文件获取各个bean的 ...

  4. Spring Bean注册和加载

    Spring解密 - XML解析 与 Bean注册 Spring解密 - 默认标签的解析 Spring解密 - 自定义标签与解析 Spring解密 - Bean的加载流程

  5. spring动态修改bean

    spring动态修改bean @RequestMapping("ok") public Object test2(){ ApplicationContext application ...

  6. 【String注解驱动开发】如何按照条件向Spring容器中注册bean?这次我懂了!!

    写在前面 当bean是单实例,并且没有设置懒加载时,Spring容器启动时,就会实例化bean,并将bean注册到IOC容器中,以后每次从IOC容器中获取bean时,直接返回IOC容器中的bean,不 ...

  7. 【String注解驱动开发】面试官让我说说:如何使用FactoryBean向Spring容器中注册bean?

    写在前面 在前面的文章中,我们知道可以通过多种方式向Spring容器中注册bean.可以使用@Configuration结合@Bean向Spring容器中注册bean:可以按照条件向Spring容器中 ...

  8. 【Spring】详解Spring中Bean的加载

    之前写过bean的解析,这篇来讲讲bean的加载,加载要比bean的解析复杂些,该文之前在小编原文中有发表过,要看原文的可以直接点击原文查看,从之前的例子开始,Spring中加载一个bean的方式: ...

  9. 品Spring:bean定义上梁山

    认真阅读,收获满满,向智慧又迈进一步... 技术不枯燥,先来点闲聊 先说点好事高兴一下.前段时间看新闻说,我国正式的空间站建设已在进行当中.下半年,长征五号B运载火箭将在海南文昌航天发射场择机将空间站 ...

随机推荐

  1. DTS_E_CANNOTACQUIRECONNECTIONFROMCONNECTIONMANAGER. The AcquireConnection method call to the connection manager "XXX" failed with error code 0xC0209303.

    问题: 今天写了一个新的SSIS的ETL包,运行报如下错误. DTS_E_CANNOTACQUIRECONNECTIONFROMCONNECTIONMANAGER. The AcquireConnec ...

  2. tomcat重载web项目,debug

    Reloading Context with name [/testCookie] is completed 加载上下文名称[ / ]完成testcookie //start九月 05, 2017 9 ...

  3. Linux CentOS7 VMware find命令、文件名后缀

    一.find命令 Linux系统中的 find 命令在查找文件时非常有用而且方便.它可以根据不同的条件来查找文件,例如权限.拥有者.修改日期/时间.文件大小等等.在这篇文章中,我们将学习如何使用 fi ...

  4. java#tostring

    通常使用apache-commons 来生成tostring方法,但是对于类型为java.util.Date的字段打印效果并不是我们想要的. @Override public String toStr ...

  5. boost::timer demo

    #include <iostream> #include <boost/timer.hpp> //timer的头文件 using namespace boost; //打开bo ...

  6. 「NOIP2009」靶形数独

    传送门 Luogu 解题思路 这题其实挺简单的. 首先要熟悉数独,我们应该要优先搜索限制条件多的行,也就是可能方案少的行,显然这样可以剪枝,然后再发挥一下dfs的基本功就可以了. 细节注意事项 爆搜题 ...

  7. LoadRunner监控Linux系统

    需要下载3个包:  地址链接:链接:https://pan.baidu.com/s/1lltAa6JnjJ7Mr88duixUSQ 密码:5yiw(1)rsh-0.17-14.i386.rpm (2) ...

  8. pip 安装源

    pip 安装源 介绍 1.采用国内源,加速下载模块的速度 2.常用pip源: -- 豆瓣:https://pypi.douban.com/simple -- 阿里:https://mirrors.al ...

  9. Stm32CubeMx lwip+freeRTOS TCP 服务

    如何添加lwip参照上一篇   stm32CubeMx lwip + freeRTOS 今天讲一下,如何添加TCP服务 LwIP 提供了三种编程接口,分别为 RAW/Callback API.NETC ...

  10. haproxy+keepalive双主高可用实现负载均衡

    转载自https://blog.51cto.com/3381847248/1977073 前面我已经介绍了haproxy结合keepalive做简单的双主高可用,如果不清楚的话,可以去我的上一 篇博客 ...