📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (176K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
ApplicationContext Overview
ApplicationContext provides the following:
- Bean factory methods for accessing application components
- The ability to load file resources in a generic way
- The ability to publish events to registered listeners
- The ability to resolve messages, supporting internationalization
How to Get ApplicationContext in Spring Boot
1. Implementing ApplicationContextAware Interface
package net.javaguides.spring;
import org.springframework.stereotype.Component;
@Component
public class Message {
public String getMessage() {
return "Hello World!";
}
}
package net.javaguides.spring;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
@Component
public class ApplicationContextProvider implements ApplicationContextAware {
private ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
ApplicationContext getApplicationContext() {
return this.applicationContext;
}
}
package net.javaguides.spring;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringGetApplicationContextApplication implements CommandLineRunner {
@Autowired
ApplicationContextProvider applicationContextProvider;
public static void main(String[] args) {
SpringApplication.run(SpringGetApplicationContextApplication.class, args);
}
@Override
public void run(String...args) throws Exception {
Message message = applicationContextProvider.getApplicationContext().getBean(Message.class);
System.out.println(message.getMessage());
}
}
Hello World!
Field injection ApplicationContext (@Autowired and Field injection)
@Autowired
private ApplicationContext applicationContext;
package net.javaguides.spring;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
@SpringBootApplication
public class SpringGetApplicationContextApplication implements CommandLineRunner {
@Autowired
private ApplicationContext applicationContext;
public static void main(String[] args) {
SpringApplication.run(SpringGetApplicationContextApplication.class, args);
}
@Override
public void run(String...args) throws Exception {
Message message = applicationContext.getBean(Message.class);
System.out.println(message.getMessage());
}
}
Hello World!
Comments
Post a Comment
Leave Comment