In my training someone asked me whether it is possible to create an object (a Spring Bean) dynamically so you can choose which implementation you want to have in the runtime. So at the compile time you don't know what object actually should be created yet. The application should decide what object to be created based on a property file.
1. We create an annotation so we can mark the method which should be able to create the object dynamically:
...
package your.package;
...
@Retention(RetentionPolicy.RUNTIME)
public @interface InjectDynamicObject {
}
1. We create an annotation so we can mark the method which should be able to create the object dynamically:
...
package your.package;
...
@Retention(RetentionPolicy.RUNTIME)
public @interface InjectDynamicObject {
}
...
2. Use the new created annotation in your method which should be able to create the object dynamically:
...
@Named("customerBo")
public class CustomerBoImpl implements CustomerBo {
...
@Override
@InjectDynamicObject
public Customer getDynamicCustomer() {
return this.dynamicCustomer;
}
...
3. Write an aspect with Pointcut and Advise which change the object returned by the method in the step 2:
...
@Named
@Aspect
public class DynamicObjectAspect {
// This comes from the property file
@Value("#{objects.object}")
private String object;
@Inject
private ApplicationContext applicationContext;
@Pointcut("execution(@your.package.InjectDynamicObject * *(..))")
public void beanAnnotatedWithInjectDynamicObject() {
}
@Around("beanAnnotatedWithInjectDynamicObject()")
public Object adviceBeanAnnotatedWithInjectDynamicObject(
ProceedingJoinPoint pjp) throws Throwable {
Object returnResult = pjp.proceed();
// Create the bean or object depends on the property file
Object createdObject = applicationContext.getBean(object);
return createdObject;
}
}
...
4. Write your unit test to test the method:
...
@Test
public void testCustomerOnlineOrOffline() {
// Dynamic object creation
System.out.println("DYNAMIC CUSTOMER: "
+ customerBo.getDynamicCustomer().getName());
}
...
OK, there is another easier way to do this ;-) Without Aspects and AspectJ, just pure Spring:
Just inject all your component implementations into a Map and get the correct implementation out of it. Just like what we have done in eXTra Client application. Please take a look at our implementation of PluginsLocatorManager as an example: http://goo.gl/itpcb. Spring injects the Map with Bean name as String and the Bean itself automagically. "... Even typed Maps can be autowired as long as the expected key type is String. The Map values will contain all beans of the expected type, and the keys will contain the corresponding bean names" (see Spring documentation for details).
@Override
Have fun!
Just inject all your component implementations into a Map and get the correct implementation out of it. Just like what we have done in eXTra Client application. Please take a look at our implementation of PluginsLocatorManager as an example: http://goo.gl/itpcb. Spring injects the Map with Bean name as String and the Bean itself automagically. "... Even typed Maps can be autowired as long as the expected key type is String. The Map values will contain all beans of the expected type, and the keys will contain the corresponding bean names" (see Spring documentation for details).
...
@Named("customerBo")
public class CustomerBoImpl implements CustomerBo {
...
// We inject the customer implementations into a Map
@Inject
private Map<String, Customer> customerDynamicMap;
// We inject the customer implementations into a Map
@Inject
private Map<String, Customer>
// This comes from the property file as a key for the Map
@Value("#{objects.object}")
private String object;
@Override
public Customer getDynamicCustomer() {
return this.customerDynamicMap.get(object);
}
...
Have fun!
Lofi
Comments
image annotation technique which outlines the object in the image with a box, for object classification and localization models.
Software consultant Company Frisco
Sales Tracking development company
Hire Apple iTv Developers
Hire Certified Hadoop Developers
Village Talkies a top-quality professional corporate video production company in Bangalore and also best explainer video company in Bangalore & animation video makers in Bangalore, Chennai, India & Maryland, Baltimore, USA provides Corporate & Brand films, Promotional, Marketing videos & Training videos, Product demo videos, Employee videos, Product video explainers, eLearning videos, 2d Animation, 3d Animation, Motion Graphics, Whiteboard Explainer videos Client Testimonial Videos, Video Presentation and more for all start-ups, industries, and corporate companies. From scripting to corporate video production services, explainer & 3d, 2d animation video production , our solutions are customized to your budget, timeline, and to meet the company goals and objectives.
As a best video production company in Bangalore, we produce quality and creative videos to our clients.
Cryptocurrency exchange app development cost
Ride sharing app development cost
Insurance app development cost
Cost to Hire Metaverse Developers in Mumbai
Cost to Hire Metaverse Developers in Bangalore
Moj app development cost
App Development Cost in India