Aware interfaces are a set of callback interfaces that allow beans to interact with the Spring container. By implementing these interfaces, a bean can gain access to certain infrastructure objects or be informed about certain container events. This feature is particularly useful when you need to interact with the Spring framework's core infrastructure from within your bean.
Common Aware Interfaces
ApplicationContextAware: Allows a bean to access the ApplicationContext that it belongs to.
BeanFactoryAware: Allows a bean to access the BeanFactory that created it.
EnvironmentAware: Allows a bean to access the Environment (such as properties and profiles).
MessageSourceAware: Allows a bean to access the MessageSource for resolving messages, supporting internationalization.
ResourceLoaderAware: Allows a bean to access the ResourceLoader to load resources like files.
ServletContextAware: Allows a bean to access the ServletContext in a web application context.
BeanNameAware: Allows a bean to be aware of its own bean name in the Spring container.
How They Work
When a bean implements one of these interfaces, Spring injects the required object into the bean before any initialization callbacks (like @PostConstruct or the InitializingBean's afterPropertiesSet() method) are invoked. This allows the bean to interact with or retrieve information from the Spring container.
*Examples *
1. ApplicationContextAware: Accessing the ApplicationContext
The ApplicationContextAware interface allows a bean to get a reference to its ApplicationContext, which can be used to look up other beans, get resources, publish events, etc.
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
@Component
public class MyApplicationContextAwareBean implements ApplicationContextAware {
private ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
public void displayAllBeanNames() {
String[] beanNames = applicationContext.getBeanDefinitionNames();
System.out.println("All bean names in ApplicationContext:");
for (String beanName : beanNames) {
System.out.println(beanName);
}
}
}
Explanation:
The MyApplicationContextAwareBean implements ApplicationContextAware, which means Spring will call the setApplicationContext() method during bean initialization, passing the ApplicationContext to it.
The displayAllBeanNames() method can then be used to list all beans defined in the current ApplicationContext.
2. BeanFactoryAware: Accessing the BeanFactory
The BeanFactoryAware interface provides access to the BeanFactory that created the bean. The BeanFactory is the root container responsible for creating and managing beans.
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.stereotype.Component;
@Component
public class MyBeanFactoryAwareBean implements BeanFactoryAware {
private BeanFactory beanFactory;
@Override
public void setBeanFactory(BeanFactory beanFactory) {
this.beanFactory = beanFactory;
}
public void checkBeanExistence(String beanName) {
if (beanFactory.containsBean(beanName)) {
System.out.println("Bean with name " + beanName + " exists.");
} else {
System.out.println("Bean with name " + beanName + " does not exist.");
}
}
}
Explanation:
The MyBeanFactoryAwareBean implements BeanFactoryAware, so Spring injects the BeanFactory into the bean using the setBeanFactory() method.
The checkBeanExistence() method can be used to check whether a specific bean exists in the BeanFactory.
3. EnvironmentAware: Accessing Environment Properties
The EnvironmentAware interface allows a bean to access the Environment object, which provides access to properties, profiles, and other environment-related settings.
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
@Component
public class MyEnvironmentAwareBean implements EnvironmentAware {
private Environment environment;
@Override
public void setEnvironment(Environment environment) {
this.environment = environment;
}
public void printActiveProfiles() {
String[] activeProfiles = environment.getActiveProfiles();
System.out.println("Active profiles:");
for (String profile : activeProfiles) {
System.out.println(profile);
}
}
public void printProperty(String propertyName) {
String propertyValue = environment.getProperty(propertyName);
System.out.println("Value of property " + propertyName + ": " + propertyValue);
}
}
Explanation:
The MyEnvironmentAwareBean implements EnvironmentAware, so Spring injects the Environment object into the bean using the setEnvironment() method.
The printActiveProfiles() method displays all active profiles.
The printProperty() method retrieves the value of a specific environment property.
4. ResourceLoaderAware: Loading Resources
The ResourceLoaderAware interface allows a bean to load resources (such as files) from various locations (classpath, filesystem, etc.).
import org.springframework.context.ResourceLoaderAware;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Component;
import java.io.BufferedReader;
import java.io.InputStreamReader;
@Component
public class MyResourceLoaderAwareBean implements ResourceLoaderAware {
private ResourceLoader resourceLoader;
@Override
public void setResourceLoader(ResourceLoader resourceLoader) {
this.resourceLoader = resourceLoader;
}
public void loadResource(String location) {
try {
Resource resource = resourceLoader.getResource(location);
BufferedReader reader = new BufferedReader(new InputStreamReader(resource.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Explanation:
The MyResourceLoaderAwareBean implements ResourceLoaderAware, so Spring injects the ResourceLoader into the bean using the setResourceLoader() method.
The loadResource() method demonstrates how to load and read a resource from a given location (e.g., a file on the classpath).
5. BeanNameAware: Accessing the Bean Name
The BeanNameAware interface allows a bean to be aware of its own bean name in the Spring container.
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.stereotype.Component;
@Component
public class MyBeanNameAwareBean implements BeanNameAware {
private String beanName;
@Override
public void setBeanName(String name) {
this.beanName = name;
}
public void printBeanName() {
System.out.println("This bean's name is: " + beanName);
}
}
Explanation:
The MyBeanNameAwareBean implements BeanNameAware, so Spring injects the bean's name into the bean using the setBeanName() method.
The printBeanName() method prints the bean's name.
Top comments (0)