使用SpringBoot实现在运行时动态创建Spring Bean

栏目: Java · 发布时间: 6年前

内容简介:是否可以动态创建一个对象(Spring Bean)?这样可以选择要在运行时选择要使用的实现。在编译时你不知道究竟应该创建什么对象。应用程序应根据属性文件确定要创建的对象。1.我们创建一个注释,这样就可以标记应该能够动态创建对象的方法:2. 在方法中使用新创建的注释,该注释应该能够动态创建对象:

是否可以动态创建一个对象(Spring Bean)?这样可以选择要在运行时选择要使用的实现。在编译时你不知道究竟应该创建什么对象。应用程序应根据属性文件确定要创建的对象。

1.我们创建一个注释,这样就可以标记应该能够动态创建对象的方法:

<b>package</b> your.<b>package</b>;

@Retention(RetentionPolicy.RUNTIME)
<b>public</b> @<b>interface</b> InjectDynamicObject {
}

2. 在方法中使用新创建的注释,该注释应该能够动态创建对象:

@Service

<b>public</b> <b>class</b> CustomerServiceImpl {
    <b>private</b> Customer dynamicCustomerWithAspect;
    
    @InjectDynamicObject
    <b>public</b> Customer getDynamicCustomerWithAspect() {
        <b>return</b> <b>this</b>.dynamicCustomerWithAspect;
    }
}

3. 使用Pointcut和Advise编写面向方面编程AOP的一个方面,更改步骤2中方法返回的对象:

@Component
@Aspect
<b>public</b> <b>class</b> DynamicObjectAspect {
    <font><i>// This comes from the property file</i></font><font>
    @Value(</font><font>"${dynamic.object.name}"</font><font>)
    <b>private</b> String object;
    @Autowired
    <b>private</b> ApplicationContext applicationContext;
    
    @Pointcut(</font><font>"execution(@com.lofi.springbean.dynamic.
        InjectDynamicObject * *(..))</font><font>")
    <b>public</b> <b>void</b> beanAnnotatedWithInjectDynamicObject() {
    }
    @Around(</font><font>"beanAnnotatedWithInjectDynamicObject()"</font><font>)
    <b>public</b> Object adviceBeanAnnotatedWithInjectDynamicObject(
        ProceedingJoinPoint pjp) throws Throwable {   
        pjp.proceed();
        
        </font><font><i>// Create the bean or object depends on the property file  </i></font><font>
        Object createdObject = applicationContext.getBean(object);
        <b>return</b> createdObject;
    }
}
</font>

4.编写从@InjectDynamicObject返回的你自己的类。名称在属性文件中配置。在这个例子中,我写了两个不同的Customer实现:CustomerOneImpl和CustomerTwoImpl:

@Component(<font>"customerOne"</font><font>)
<b>public</b> <b>class</b> CustomerOneImpl implements Customer {
    @Override
    <b>public</b> String getName() {
        <b>return</b> </font><font>"Customer One"</font><font>;
    }
}
application.properties
dynamic.object.name=customerOne
</font>

5.编写单元测试的测试方法:

@RunWith(SpringRunner.<b>class</b>)
@SpringBootTest
<b>public</b> <b>class</b> CustomerServiceImplTest {
    @Autowired
    <b>private</b> CustomerServiceImpl customerService;
    @Test
    <b>public</b> <b>void</b> testGetDynamicCustomerWithAspect() {
        <font><i>// Dynamic object creation</i></font><font>
        logger.info(</font><font>"Dynamic Customer with Aspect: "</font><font> +
            customerService.getDynamicCustomerWithAspect()
            .getName());
}
</font>

好的,有另一种更简单的方法可以做到这一点 没有Aspects和AspectJ,只需要纯粹的Spring:

无AOP

只需将所有组件实现注入Map中,然后从中获取正确的实现。就像我们在eXTra客户端应用程序中所做的一样。请以我们的 PluginsLocatorManager 实现为例。Spring将Bean的Bean名称作为String注入,而Bean本身则自动注入。

“......只要预期的键类型为String,即使是键入的Map也可以自动装配。Map值将包含所需类型的所有bean,并且键将包含相应的bean名称“ ( 有关详细信息, 请参阅 Spring文档)

@Service
<b>public</b> <b>class</b> CustomerServiceImpl {
    
    <font><i>// We inject the customer implementations into a Map</i></font><font>
    @Autowired
    <b>private</b> Map<String, Customer> dynamicCustomerWithMap;
    
    </font><font><i>// This comes from the property file as a key for the Map</i></font><font>
    @Value(</font><font>"${dynamic.object.name}"</font><font>)
    <b>private</b> String object;
    <b>public</b> Customer getDynamicCustomerWithMap() {
        <b>return</b> <b>this</b>.dynamicCustomerWithMap.get(object);
    }
}
</font>

Github 


以上所述就是小编给大家介绍的《使用SpringBoot实现在运行时动态创建Spring Bean》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

疯传:让你的产品、思想、行为像病毒一样入侵(全新修订版)

疯传:让你的产品、思想、行为像病毒一样入侵(全新修订版)

[美] 乔纳•伯杰(Jonah Berger) / 乔迪、王晋 / 电子工业出版社 / 2016-6 / 68.00

是什么让事物变得流行? 从买轿车、买衣服、吃三明治,到给孩子取名字,你是否知道为什么某些产品会大卖,某些故事被人们口口相传,某些电子邮件更易被转发,或者某些视频链接被疯狂地点击,某些谣言更具传播力,某些思想和行为像病毒一样入侵你的大脑……这本书将为你揭示这些口口相传和社会传播背后的科学秘密,并且告诉你如何将产品、思想、行为设计成具有感染力和传播力的内容。 无论你是大公司的管理者,还是努......一起来看看 《疯传:让你的产品、思想、行为像病毒一样入侵(全新修订版)》 这本书的介绍吧!

随机密码生成器
随机密码生成器

多种字符组合密码

html转js在线工具
html转js在线工具

html转js在线工具

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换