-1

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.

https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/beans/factory/BeanFactory.html

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.

Sara
  • 603
  • 8
  • 19
  • 2
    If you have a `BeanFactory` implementation for LDAP it can come from there. You can pull them from anywhere if you provide the correct implementation. Spring only provides implementation to work with propertie files, xml and java config (or groovy/ruby). – M. Deinum Nov 30 '21 at 14:44
  • @M.Deinum When you say properties file, are you referring to spring boot application.properties? – Sara Nov 30 '21 at 14:47
  • @M.Deinum are there other implementations of BeanFactory apart from Spring framework itself? – Sara Nov 30 '21 at 14:48
  • Any property file which contains the correct format (not the `application.properties` from Spring Boot that is a regular property file containing configuration parameters. – M. Deinum Nov 30 '21 at 14:48
  • I wrote one for JDBC a very long time ago. I suggest Google for different implementations than the ones provided by Spring. – M. Deinum Nov 30 '21 at 14:49
  • @M.Deinum I'd be interested in this bean creation from property file. Please provide an example – Sara Nov 30 '21 at 14:50
  • 1
    https://deinum.biz/2018-04-12-on-spring-applicationcontext-and-bean-creation/ – M. Deinum Nov 30 '21 at 14:53
  • @M.Deinum Thank you for the example. Also, I understand that this is an ApplicationContext. I was curious to know if an exclusive BeanFactory instance can be created using any way other than the .xml files, in Spring, using its own implementations. – Sara Nov 30 '21 at 14:58
  • 1
    Read the blog. Also an `ApplicationContext` is a `BeanFactory`. In the sample using properties you could use the `DefaultListableBeanFactory` instead of the `GenericApplicationContext` and achieve the exact same result. – M. Deinum Nov 30 '21 at 15:04
  • @M.Deinum I will try that out. Thank you for all the inputs. – Sara Nov 30 '21 at 15:08
  • @M.Deinum Please provide this as a reply so that I can accept it as an answer. – Sara Nov 30 '21 at 15:09

1 Answers1

1

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

However you can create whatever implementation you like if you need.

M. Deinum
  • 115,695
  • 22
  • 220
  • 224