Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What's the difference between BeanFactory and ApplicationContext?

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "what is the difference between BeanFactory and ApplicationContext". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what is the difference between BeanFactory and ApplicationContext".

What is Spring Bean?

This is a very simple and complex problem. Generally speaking, Spring beans is the Java object managed by the Spring container. Let's look at a simple example.

Package com.zoltanraffai; public class HelloWorld {private String message; public void setMessage (String message) {this.message = message;} public void getMessage () {System.out.println ("My Message:" + message);}}

In XML-based configurations, beans.xml provides metadata for Spring container-managed bean

What is a Spring container

The Spring container is responsible for instantiating, configuring and assembling Spring beans. Let's see how to configure our HelloWorld POJO for the IoC container.

Now that it is managed by the Spring container, the next question is: how do we get it?

Differences of BeanFactory Interface between BeanFactory and ApplicationContext

This is a root interface for accessing the Spring container. To access the Spring container, we will use the Spring dependency injection feature, the BeanFactory interface and its subinterface features:

Instantiation / concatenation of Bean is usually the case. The implementation of BeanFactory uses lazy loading, which means that beans instantiates them only when we call them directly through the getBean () method. The most common API to implement BeanFactory is XMLBeanFactory. Here is an example of how to get a bean through BeanFactory:

Package com.zoltanraffai; import org.springframework.core.io.ClassPathResource; import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.xml.XmlBeanFactory; public class HelloWorldApp {public static void main (String [] args) {XmlBeanFactory factory = new XmlBeanFactory (new ClassPathResource ("beans.xml")); HelloWorld obj = (HelloWorld) factory.getBean ("helloWorld"); obj.getMessage ();}} ApplicationContext interface

ApplicationContext is the central interface in the Spring application to provide configuration information to the application. It inherits the BeanFactory interface, so ApplicationContext contains all the features of BeanFactory and more! Its main function is to support the creation features of large business applications:

Bean instantiation/wiring

Instantiation / concatenation of Bean

Automatic BeanPostProcessor registration

Automatic BeanFactoryPostProcessor registration

Convenient MessageSource access (i18n)

The release of ApplicationEvent is different from the way BeanFactory loads lazily, it is preloaded, so every bean is instantiated after ApplicationContext starts. Here is the use example of ApplicationContext:

Package com.zoltanraffai; import org.springframework.core.io.ClassPathResource; import org.springframework.beans.factory.InitializingBean; import org.springframework.beans.factory.xml.XmlBeanFactory; public class HelloWorldApp {public static void main (String [] args) {ApplicationContext context=new ClassPathXmlApplicationContext ("beans.xml"); HelloWorld obj = (HelloWorld) context.getBean ("helloWorld"); obj.getMessage ();}} Summary

ApplicationContext contains all the features of BeanFactory, and the former is generally recommended. But there are some limitations, such as the memory consumption of mobile applications is more stringent, and in those scenarios, it is more reasonable to use a lighter BeanFactory. However, in most enterprise applications, ApplicationContext is your first choice.

Thank you for your reading, the above is the content of "what is the difference between BeanFactory and ApplicationContext". After the study of this article, I believe you have a deeper understanding of what is the difference between BeanFactory and ApplicationContext, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report