spring 动态bean注册

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

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

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6
标签: Spring
返回列表

上一篇:MySQL 统计信息

下一篇:__file__

“spring 动态bean注册” 的相关文章

isinstance判断对象

>>> isinstance(u'\0xAB',str) False >>> isinstance(u'\0xAB',int) False >>> isinstance(u'\0xAB',unicode) True...

spark读取hdfs文件的路径使用正则表达式

spark.read.json("/flume/product/160/raw1/2017/05/23/*[1-9]")读取文件,文件以数字结尾....

Java

1.不可变不变的对象绝对是线程安全的,不需要线程同步,如String、Long、BigInteger2.无条件的线程安全对象自身做了 足够的内部同步,也不需要外部同步,如 Random 、ConcurrentHashMap、Concurrent集合、atomic3.有条件的线程安全对象的部分方法可以...

linux

反复进入登录界面,和系统的环境变量配置有关解决:1.重新启动,将光标停留在 recovery mode,然后按E 进入grub 编辑。2.在编辑的最后一行输入 rw init=/bin/bash ,然后按Ctrl+X 重启3.进入GRUB菜单,选择root ...(可能是resume ...) 进入...

ABPvNext-微服务框架基础入门

ABPvNext-微服务框架基础入门 本文使用的是ABPvNext商业版 最新稳定版本7.0.2为演示基础的,后续如果更新,会单独写一篇最新版本的,此文为零基础入门教程,后续相关代码会同步更新到gitee仓库中。 准备工作: 1.登录ABPvNext官网 网址 http://abp.io 2.跳...

Golang reflect反射如何使用 - 开发技术

这篇文章主要介绍“Golang reflect反射如何使用”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“Golang reflect反射如何使用”文章能帮助大家解决问题。首先有一段以下结构体的定义type&...