Ordinary objects using the object spring container

Quote:

    Sometimes need to work in ordinary objects to call the object spring management, but when ordinary java object directly or using @Autowired @Resource will find an object to be injected is null, null pointer will be reported. We can simply understood as the spring is a company that manages the object is its employees, and ordinary java object are employees of other companies, to work with other companies to find spring if the company's employees without the consent of the company is definitely spring No way.

Solution:

Method One: If this ordinary object can be, it is best to directly manage spring spring management, then this spring managed bean in other bean injection is not a problem.

Method Two: When our ordinary objects there is no way to spring management, we can create a common springBeanUtil specializes in employee spring for ordinary objects (a bit like spring's outsourcing sector, the object is outsourced to other companies, ha ha ).

@Service
public class SpringBeanUtil implements ApplicationContextAware {

    public static ApplicationContext applicationContext;

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

    // 这里使用的是根据class类型来获取bean 当然你可以根据名称或者其他之类的方法 主要是有applicationContext你想怎么弄都可以
    public static Object getBeanByClass(Class clazz) {
        return applicationContext.getBean(clazz);
    }
}
复制代码

This util does, in fact, is to achieve a ApplicationContextAware interfaces, a small partner to ask this interface is doing? Here is the link address, ApplicationContextAware references . Then I will explain to excerpts of the document came

public interface ApplicationContextAware extends Aware
Interface to be implemented by any object that wishes to be notified of the ApplicationContext that it runs in.
Implementing this interface makes sense for example when an object requires access to a set of collaborating beans. Note that configuration via bean references is preferable to implementing this interface just for bean lookup purposes.
This interface can also be implemented if an object needs access to file resources, i.e. wants to call getResource, wants to publish an application event, or requires access to the MessageSource. However, it is preferable to implement the more specific ResourceLoaderAware, ApplicationEventPublisherAware or MessageSourceAware interface in such a specific scenario.
Note that file resource dependencies can also be exposed as bean properties of type Resource, populated via Strings with automatic type conversion by the bean factory. This removes the need for implementing any callback interface just for the purpose of accessing a specific file resource.
ApplicationObjectSupport is a convenience base class for application objects, implementing this interface.
复制代码

Probably meant to say as long as the class that implements the interface ApplicationContextAware, expect to be told what applicationContext currently running Yes. Then he said that if you want access to resources is best to use ResourceLoaderAware, ApplicationEventPublisherAware or MessageSourceAware these interfaces, and finally to the sentence we know you want to use these interfaces, so we can help you get a realization of these abstract interfaces class ApplicationObjectSupport (in spring-context in a jar). Here it very clear that if you want to use bean to achieve ApplicationContextAware, because here we do not need to use so static resources like we do not have ApplicationObjectSupport spring offers us, are interested can study at their own.

Here we simply have to look at what's inside ApplicationContextAware class?

void setApplicationContext(ApplicationContext applicationContext) throws BeansException;
复制代码

One way to find it, spring will be initialized when the current passed setApplicationContext applicationContext method ApplicationContextAware, so long as implementation class applicationContext get this, you can get to the spring in the bean class by class name or type of. Principle is very simple. We can use the time to call in the spring of bean. as follows:

Test test = (Test) SpringBeanUtil.getBeanByClass(Test.class);
复制代码

Guess you like

Origin juejin.im/post/5cf3d1625188254c5726a3c9