dineshonjava

What is meant by “application-context” in spring and how do you create one?

"application-context" in spring means nothing but it is core component of spring container in spring framework. Ideally we can say "application-context" one of the Spring Container in Spring Framework and other container is "bean-factory". The configuration for "application-context" is loaded by the one of concrete implementation of ApplicationContext interface.
The ApplicationContext is the central interface within a Spring Application for providing configuration information to the application. It is read-only at run time , but can be reloaded if necessary and supported by the application.


Major Responsibilities of "application-context" container
application-context in Spring
  • It provides bean factory methods for accessing application components.
  • It provides the ability to load file resources in a generic fashion.
  • It provides the ability to publish event to register listeners
  • It provide the ability to resolve to support internationalization.


Creating one "application-context"

  • AnnotationConfigApplicationContext. Loads a Spring application context from one or moro Java-Based configuration classes.
    ApplicationContext context = new AnnotationConfigApplicationContext(JavaConfig.class;
    
  • AnnotationConfigWebApplicationContext. Loads a Spring web application context from one or more Java-Based configuration classes.
    ApplicationContext context = new AnnotationConfigWebApplicationContext(JavaWebConfig.class;
    
  • ClassPathXMLApplicationContext. Loads a context definition from one or more XML files located in the classpath
    ApplicationContext context = new ClassPathXMLApplicationContext("spingConfig.xml");
    
  • FileSystemXMLApplicationContext. Loads a context definition from one or more XML files located in the filesystem.
    ApplicationContext context = new FileSystemXMLApplicationContext("c:/spingConfig.xml");
    
  • XMLWebApplicationContext. Loads a context definition from one or more XML files contained in the web application.
    ApplicationContext context = new XMLWebApplicationContext("/WEB-INF/config/spingConfig.xml");
    

Accessing Application Context in the other classes:
You could also access the "application-context" container into other classes. You can implement ApplicationContextAware as in the following example:
public class AnotherClass implements ApplicationContextAware {

    private ApplicationContext applicationContext;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }

}





What is an interface and what are the advantages of making use of them in Java?

"Programming for interfaces than implementation" is very popular principle in java programming and design pattern. Here I am going to explain some interested facts about interface in java in term of Spring Framework.

interface in java

Here I am not going to explain that interface is a keyword in java we will focus on beyond the keyword. Actually interface in java is the core part of programming, it is not for hello world type of application, it is using for abstraction and decoupling concept in java. It is simple object oriented term to define contract or rules and abstraction between producer and consumer for applications.


Advantages or key facts of making use of them in Java 
  • In very basic it allows us for multiple inheritance in java.
  • In Spring Dependency Injection interface is very powerful to run time injection of various concrete implementations of an interface in the application. By using references to interfaces instead of their concrete implementation classes help to minimize ripple effects, as the user of interface reference doesn't have to worry about the changes in the underlying concrete implementation.
  • Interfaces are a way to declare a contract for implementing classes to fulfill, it's the primary tool to create abstraction and decoupled designs between consumers and producers.
  • In an example we have a service to implement to save employee data to RDBMS and NoSQL database. If we were not using interface, the EmployeeRegistrationService may be implemented with two functions saveToRDBMS() and saveToNoSQL().
  • public class EmployeeRegistrationService {
        public void saveToRDBMS(Employee employee ) {
            //save to RDBMS
        }
        public void saveToNoSQL(Employee employee ) {
            //save to NoSQL DB
        }
    }
    
    In this case, the EmplyeeRegistrationController should be aware of the concrete implementation of these two functions in EmployeeRegistrationService to use them. Suppose we want to add additional functionality to save the information as JSON is required then you will have to add a new function saveToJson() in the Service class as well as make changes in the Controller. This adds lots of complication to maintenance of our huge application with hundreds of controllers and services. To avoid these complications we could use interface instead of implementation of registration service.
    interface EmployeeRegistrationService {
        void save(Employee employee );
      }
    
    Now controller doesn't care about the concrete implementation of service, it is only aware of this interface, which has a save method.
    public class EmployeeServiceRDS implements EmployeeRegistrationService {
       @Override 
       public void saveToRDBMS(Employee employee ) {
            //save to RDBMS
        }
    }
    public class EmployeeServiceNoSQL implements EmployeeRegistrationService {
        @Override
        public void saveToNoSQL(Employee employee ) {
            //save to NoSQL DB
        }
    }
    
    @Controller
    Class EmployeeController {
       
       @Resource(name="employeeServiceRDS ") 
       EmployeeRegistrationService  registrationService ;
    
        @RequestMapping("/emp-save")
        public void saveEmployee(Employee employee) {
            registrationService.save(employee);
        }
      }
    
    This highly reduces the software modification and extension cost. As changes in one layer does not effect other layer and new functionalities are made available to other layer immediately. Thus using interface gives you more power over extending and maintaining your application, utilize abstraction and implement good software development practices.

Spring Related Topics you may like
  1. Spring Interview Questions and Answers
  2. Spring AOP Interview Questions and Answers
  3. Spring MVC Interview Questions
  4. Spring Security Interview Questions and Answers
  5. Spring REST Interview Questions and Answers
  6. Spring Boot Interview Questions and Answers
  7. Spring Boot Microservices Interview Questions and Answers
  8. Dependency Injection (DI) in Spring
  9. Spring IoC Container
  10. What is Bean Factory in Spring
  11. ApplicationContext in Spring
  12. Bean Autowiring in Spring
  13. Spring Bean Scopes
  14. Create Custom Bean Scope in Spring Example
  15. Using ApplicationContextAware in Spring
  16. Spring Bean Life Cycle and Callbacks
  17. BeanPostProcessor in Spring
  18. BeanFactoryPostProcessor in Spring
  19. Annotations in Spring and Based Configuration
  20. Spring JSR-250 Annotations
  21. JSR 330 Annotations in Spring
  22. Spring @Component, @Repository, @Service and @Controller Stereotype Annotations
  23. Method injection with Spring using Lookup method property
  24. Spring AOP-Introduction to Aspect Oriented Programming
  25. @Aspect Annotation in Spring
  26. Spring AOP AspectJ @Before Annotation Advice Example
  27. Spring AOP Before Advice Example using XML Config
  28. Spring AOP AspectJ @After Annotation Advice Example
  29. Spring AOP After Advice Example using XML Config
  30. Spring AOP AspectJ @AfterReturning Annotation Advice Example
  31. Spring AOP After-Returning Advice Example using XML Config
  32. Spring AOP AspectJ @AfterThrowing Annotation Advice Example
  33. Spring AOP After Throwing Advice Example using XML Config
  34. Spring AOP AspectJ @Around Annotation Advice Example
  35. Spring AOP Around Advice Example using XML Config
  36. Spring AOP Proxies in Spring
  37. Spring AOP Transaction Management in Hibernate
  38. Spring Transaction Management
  39. Spring Declarative Transaction Management Example
  40. Spring AOP-Ordering of Aspects with Example
  41. Spring Security Java Based Configuration with Example
  42. Spring Security XML Namespace Configuration Example