I understand the difference between a BeanFactory
and an ApplicationContext
.
I am also aware that a BeanFactory instance can be created from xml files, which reside in the classpath or anywhere else in the file system. So, an XMLBeanFactory instance is created in such cases.
At the same time, I was digging into BeanFactory
documentation and stumbled upon this.
Normally a BeanFactory will load bean definitions stored in a configuration source (such as an XML document), and use the org.springframework.beans package to configure the beans. However, an implementation could simply return Java objects it creates as necessary directly in Java code. There are no constraints on how the definitions could be stored: LDAP, RDBMS, XML, properties file, etc. Implementations are encouraged to support references amongst beans (Dependency Injection).
So, does this mean that the bean definition can be in non-XML format as well? viz, LDAP, RDBMS, properties file etc? If yes, please provide an snippet of it. I am exclusively looking for BeanFactory only and not any ApplicationContext implementations.
CodePudding user response:
How to load an ApplicationContext
which is a BeanFactory
on steriods is explained in this blog.
public class SpringContextsApplication {
public static void main(String[] args) throws Exception {
GenericApplicationContext contextFromProperties =
new GenericApplicationContext();
BeanDefinitionReader reader =
new PropertiesBeanDefinitionReader(contextFromProperties);
reader.loadBeanDefinitions("classpath:application-context.properties");
contextFromProperties.refresh();
doGreeting(contextFromProperties);
contextFromProperties.stop();
}
private static void doGreeting(ApplicationContext ctx) {
Greeter greeter = ctx.getBean(Greeter.class);
Person person = ctx.getBean(Person.class);
greeter.greet(person);
}
}
Where there is a GenericApplicationContext
one could also use a DefaultListableBeanFactory
instead and achieve the same result.
public class SpringContextsApplication {
public static void main(String[] args) throws Exception {
GenericApplicationContext contextFromProperties =
new DefaultListableBeanFactory();
BeanDefinitionReader reader =
new PropertiesBeanDefinitionReader(contextFromProperties);
reader.loadBeanDefinitions("classpath:application-context.properties");
doGreeting(contextFromProperties);
contextFromProperties.stop();
}
private static void doGreeting(BeanFactory ctx) {
Greeter greeter = ctx.getBean(Greeter.class);
Person person = ctx.getBean(Person.class);
greeter.greet(person);
}
}
To load bean definitions one needs a BeanDefinitionReader
for that specific implementation you want to use. Here that is property files but you could write a BeanDefinitionReader
for LDAP, JDBC, YAML etc. as well.
Spring supports out-of-the-box
- XML
- Properties (Deprecated as of Spring 5.3, propably removed in Spring 6)
- Groovy
- Java Classes
- JDBC (Purely as an exampled, deprecated in 5.3 and removed in Spring 6)
However you can create whatever implementation you like if you need.