内容简介:是否可以动态创建一个对象(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>
以上所述就是小编给大家介绍的《使用SpringBoot实现在运行时动态创建Spring Bean》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!
猜你喜欢:- PyCharm入门第一步:创建并运行第一个Python项目
- 运行过程+运行效率
- iOS App创建证书,添加Appid,创建配置文件流程
- dotnet 判断程序当前使用管理员运行降低权使用普通权限运行
- Flink 集群运行原理兼部署及Yarn运行模式深入剖析-Flink牛刀小试
- RabbitMQ集群创建
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
新媒体革命——在线时代的媒体、公关与传播
仇勇 / 电子工业出版社 / 2016-2-1 / CNY 50.00
这既是传统媒体的大裂变年代,也是在线媒体开启的新闻业的黄金时代。 信息流动的新法则不仅改变了媒体业,也在重塑公关、传播和商业的面貌。总之,这个世界的连接方式不仅不再相同,而且这一改变不可逆转。在这个全新重启的在线时代里,无论是信息的获取还是商业本身,信任都变得比以往更重要。 从告别传统媒体的那一刻起,我就有着两个小小的“野心”:一是探寻适合在线时代的媒体生产方式;二是让优质内容有权获得......一起来看看 《新媒体革命——在线时代的媒体、公关与传播》 这本书的介绍吧!