Spring: Make an Externally Created Object Available to Beans in applicationConte...
source link: https://blog.jakubholy.net/2011/10/11/spring-make-an-externally-created-object-available-to-beans-in-applicationcontext-xml/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Spring: Make an Externally Created Object Available to Beans in applicationContext.xml
Java: Configure ApplicationContext with an Injected Bean
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.context.support.StaticApplicationContext;
Object externalyDefinedBean = ...;
GenericApplicationContext parentContext = new StaticApplicationContext();
parentContext.getBeanFactory().registerSingleton("injectedBean", externalyDefinedBean);
parentContext.refresh(); // seems to be required sometimes
ApplicationContext context = new FileSystemXmlApplicationContext(springConfigs, parentContext);
Xml: Make Use of It
<bean id="springBean" class="your.SpringBeanType">
<!-- Note: The injectedBean is defined outside of Spring config -->
<property name="someProperty" ref="injectedBean" />
</bean>
Voila!
Are you benefitting from my writing? Consider buying me a coffee or supporting my work via GitHub Sponsors. Thank you! You can also book me for a mentoring / pair-programming session via Codementor or (cheaper) email.
Allow me to write to you!
Let's get in touch! I will occasionally send you a short email with a few links to interesting stuff I found and with summaries of my new blog posts. Max 1-2 emails per month. I read and answer to all replies.
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK