How to inject Spring Bean in static method or non-Spring Bean

       In a project, sometimes you need to create a new object yourself, or obtain a Spring Bean object in some util methods or properties, so as to complete some work, but because your new objects and util methods are not managed by Spring, If it is used directly on the property it depends on, @Autowiredan error that cannot be injected will be reported, or no error will be reported, but a null pointer exception will be reported when it is used. All in all, since it is not managed by the IoC container, it cannot be injected.

        Spring provides two interfaces: BeanFactoryAware and ApplicationContextAware, both of which inherit from the Aware interface. The following are the declarations of these two interfaces:

public interface BeanFactoryAware extends Aware {
	void setBeanFactory(BeanFactory beanFactory) throws BeansException;
}
public interface ApplicationContextAware extends Aware {
	void setApplicationContext(ApplicationContext applicationContext) throws BeansException;
}

       As described in the official Spring documentation, when a Spring bean is initialized, if it is detected that a bean implements one of these two interfaces, the interface method implemented by the bean will be automatically invoked. As you can see here, both methods pass the factory object of the IoC container-managed bean to the current bean, that is to say, if we save the factory object to a static property in the current bean, then we can pass the factory The object gets the bean we need. The following is a SpringBeanUtil implemented using ApplicationContextAware:

public class SpringBeanUtil implements ApplicationContextAware {

    private static ApplicationContext applicationContext;

    public void setApplicationContext(ApplicationContext applicationContext) 
      throws BeansException {
        SpringBeanUtil.applicationContext = applicationContext;
    }

    public static <T> T getBean(Class<T> clazz) {
        return (T) applicationContext.getBean(clazz);
    }

    public static Object getBean(String name) throws BeansException {

        return applicationContext.getBean(name);
    }
}

       Here you also need to specify in the configuration file to create an instance of the current class:

<bean id="springBeanFactory" class="com.business.util.SpringBeanUtil"/>

       As you can see, we declare a static property of type ApplicationContext in SpringBeanUtil, and assign the obtained ApplicationContext to the static property in the setApplicationContext() method, so that we can pass in the other two declared static methods. The ApplicationContext gets the beans managed by the IoC container. Here is a test example:

public class ClassRoom {
  public void describeStudent() {
    Student student = SpringBeanUtil.getBean(Student.class);
    System.out.println(student);
  }

  public static void describeClassRoomCapacity() {
    Student student = SpringBeanUtil.getBean(Student.class);
    System.out.println("Is it not empty? " + (null != student));
  }
}
public class Student {
  @Override
  public String toString() {
    return "I am a student.";
  }
}
<bean id="springBeanFactory" class="com.util.SpringBeanUtil"/>
<bean id="student" class="com.domain.Student"/>

The following is the driver class:

public class BeanApp {
  public static void main(String[] args) {
    BeanFactory beanFactory = new ClassPathXmlApplicationContext("com/resources/application.xml");
    ClassRoom.describeClassRoomCapacity();
    ClassRoom classRoom = new ClassRoom();
    classRoom.describeStudent();
  }
}

       In the driver class, we first use the ClassPathXmlApplicationContext to load the beans in the configuration file. As you can see, we created a SpringBeanUtil and a Student bean. We first obtained the Student instance in the static method and printed it. We also obtained the Student instance through SpringBeanUtil in the new ClassRoom instance and output it. Here is the output:

Is it not empty? true
I am a student.

        It can be seen that whether in the static method or in the instance of manual new, we have successfully obtained the bean managed by the IoC container. If we want to get SpringBean in the static property, it is actually very simple, just assign the property directly, as shown below:

private static Student student = SpringBeanUtil.getBean(Student.class);

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325474487&siteId=291194637