Some important differences between BeanFactory & ApplicationContext are as below :-
Aspect | BeanFactory | ApplicationContext |
---|---|---|
1. Definition | It is "basic container" that manages beans with minimal features. | Its is "advanced container" extending BeanFactory with extra features. |
2. Usage | It is suitable for small, standalone applications. | It is suitable for web applications, AOP, ORM and large enterprise applications. |
3. Resource Usage | It requires less memory, suitable for lightweight or memory-critical applications. | It uses more memory, providing additional features for enterprise applications. |
4. Initialization | It uses lazy initialization i.e. it creates the beans only when needed. | It uses eager initialization i.e. it loads and creates the beans at container startup. |
5. Bean Scopes Supported | It supports only Singleton and Prototype scopes. | It supports all scopes, including Request and Session, for web contexts. |
6. Annotation Support | It doesn’t support annotations; requires XML configuration. | It supports annotations for simpler configuration. |
7. Internationalization | It does not provide support for internationalization (i18n). | It provides the i18n support for multilingual applications. |
8. Event Handling | It does not support event handling. | It supports event handling via ApplicationEvent and ApplicationListener. |
Different ways to provide Spring Configurations are as below :-
The @Configuration and @Bean annotations in Spring are "key to Java-Based Configuration". They allow us to define and manage beans in a configuration class, providing an alternative to XML-based configuration.
import org.springframework.context.annotation.Configuration;
@Configuration
public class AppConfig
{
/*
Here we provide the different configurations for example....
Bean Configurations, Database Configurations, MVC Configurations, Security Configurations etc
*/
}
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class AppConfig
{
// Below @Bean annotation is used to create Student class Bean
@Bean
public Student stdId() // Here method name is the bean-id / bean-name
{
// It will return the Student object
return new Student();
}
}
import org.springframework.stereotype.Component;
@Component
public class MyService
{
// class implementation
}
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan(basePackages = "in.sp.package")
public class AppConfig
{
// Application configuration
}
Your feedback helps us grow! If there's anything we can fix or improve, please let us know.
We’re here to make our tutorials better based on your thoughts and suggestions.