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

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

内容简介:是否可以动态创建一个对象(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》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

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

离散数学及其应用

离散数学及其应用

SusannaS.Epp / 高等教育出版社 / 2005-3-1 / 63.0

离散数学及其应用:英文本,ISBN:9787040162301,作者:( )Susanna S.Epp著一起来看看 《离散数学及其应用》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

MD5 加密
MD5 加密

MD5 加密工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具