Aware

I’m not Aware of that. She was not aware of having done wrong. She was unconscious of having done wrong. Spring provides a series of interfaces that extend Aware from the top-level interface. After implementing this interface, developers can access Spring resources in methods provided by the interface. Simply put, it is to implement the interface, is to get the container key. The Spring framework will detect beans that implement such interfaces and pass container resources to method parameters for developers to extend, so implementing this interface requires synchronization of information, otherwise other developers will wonder – why is my Bean behaving differently? .

public interface Aware {}Copy the code

UML

  • ApplicationContextAware:Access the container context itself, i.eApplicationContext.
  • BeanNameAware:Access BeanName. For example, a userService, and you want to know its runtimeBeanName, which can be accessed in this way.
  • MessageSourceAware: Access the MessageSource in the container.
  • EnvironmentAware: Access the Environment in the container to set Environment variables for the current component.
  • ApplicationEventPublisherAware: access to the object in the event of push objects in the container.
  • Visit the BeanFactory BeanFactoryAware:. In filling regular initialization callback after the bean properties (such as InitializingBean. AfterPropertiesSet () or custom init – method) before the call.
  • ResourceLoaderAware:You can get the ResourceLoader for container loading resources. Different containers may use different ResourceLoader. So it’s best to use it when you use itinstanceofLet’s judge.

This is just a list of common Aware interfaces, and there are many more. For example :BookstrapContextAware, LoadTimeWeaverAware, NotificationPublishAware, PortletConfigAware, ServletConfigAware, and so on. Readers can expand on this for themselves.

Code sample

Due to the plethora of interfaces, only BeanNameAware and ApplicationContextAware are demonstrated here

Write a simple SpringContextUtil by implementing ApplicationContextAware

During the development process, it is often necessary to write some common operations into utility classes, such as Redis, RabbitMQ, Http, etc.

If you need Spring beans, such as RedisTemplate, RabbitMQTemplate, RestTemplate. In addition to assigning variables to static variables using construct injection, these beans can also be obtained via getBean() via the Aware interface.

  • SpringContextUtil

You don’t need to inject SpringContextUtil, but you do need to add @Component to make Spring aware of your request.

package com.xjm.bean.aware;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/ * * *@author jaymin
 * 2021/1/10 16:22
 */
@Component
public class SpringContextUtil implements ApplicationContextAware {

	private static ApplicationContext applicationContext;

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

	public static  <T> T getBean(String name, Class<T> requiredType) throws BeansException {
		returnapplicationContext.getBean(name, requiredType); }}Copy the code
  • AwareDemo
package com.xjm.bean.aware;

import com.AnnotationContextDemo;
import com.xjm.model.Person;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/ * * *@author jaymin
 * 2021/1/10 16:45
 */
public class AwareDemo {
	public static void main(String[] args) {
		// The applicationContext is not used directly, but only for component scanning
		ApplicationContext applicationContext = new AnnotationConfigApplicationContext(AnnotationContextDemo.class);
		Person person = SpringContextUtil.getBean("person", Person.class);
		System.out.println("Get the Person object from the Aware interface :"+ person.toString()); }}Copy the code
  • Result

We access the entire container context by implementing the ApplicationContextAware interface and turn the getBean into a static method call with a simple delegate.

Implement BeanNameAware access to BeanName

  • Person
package com.xjm.model;

import org.springframework.beans.factory.BeanNameAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

/ * * *@author jaymin
 * 2020/11/28 0:02
 */
@Repository
public class Person implements BeanNameAware {

	private String name;

	private Integer age;

	public Person(String name) {
		this.name = name;
	}

	public Person(a) {}public String getName(a) {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Integer getAge(a) {
		return age;
	}

	public void setAge(Integer age) {
		this.age = age;
	}

	@Override
	public String toString(a) {
		return "Person{" +
				"name='" + name + '\' ' +
				", age=" + age +
				'} ';
	}

	@Override
	public void setBeanName(String name) {
		System.out.println("Person Instance's Name is "+ name); }}Copy the code
  • Result

conclusion

  • For extensibility, Spring provides not only a post-processor that can be processed when the container is refreshed, but also run-time Aware that provides an interface to access container resources. Give developers access to container resources on demand.
  • There is one for each individual resourceAwareinterface
  • Classes that implement the Aware interface also need to be registered as beans for Spring to be Aware of their existence.