Use Spring Context to retrieve Bean instances in an environment fully managed by Spring

Source: Internet
Author: User

Use Spring Context to retrieve Bean instances in an environment fully managed by Spring

In large applications, there are often flexible requirements. After the framework is used, although the development efficiency can be greatly improved, at the same time, we also framed in a shelf.
Let's first talk about the problem I encountered, which is probably like this:

@Component@Scope(prototype)public class Action1 implements Action{.....   }@Component@Scope(prototype)public class Action2  implements Action{.....   }@Component@Scope(prototype)public class Action3  implements Action{.....   }
@ Component @ Scope (prototype) public class ActionFactory {@ Autowired public Action1 action1; @ Autowired public Action2 action2; @ Autowired public Action3 action3 ;.... public Action getAction (String action ){... return actionx according to the condition }}
@Componmentpublic Server implements Runnable{    @Autowired    public ActionFactory actionFactory;    public void run(){        ....        Action action = actionFactory.getAction(actionx's name);        new Thread(new ThreadTool(action)).start();        ....    }}

This is part of the entire application. When the application starts, I need to start this Server thread. This Server thread holds an ActionFactory instance. In the run method, each time an ActionFactory instance is used to obtain an instance of an Action. That's right. However, after careful analysis, there is only one Server instance in the entire application, that is, although Scope is annotated as Prototype on ActionFactory, there is only one ActionFactory instance throughout the lifecycle, then there is a problem. Because one instance of each Action is kept in the ActionFactory as a domain, when ActionFactory # getAction is called in the server, the same Action instance is returned. This is troublesome and the thread is not secure. After thinking about it, there are no other methods (please leave a message if there are better methods). You can only manually obtain each Action in ActionFactory, or simply manually obtain ActionFactory, because Scope = Prototype is added to ActionFactory, after an ActionFactory is obtained, all Action objects held by ActionFactory are retrieved again, and Scope = Prototype is added to each Action, therefore, each Action object is new. Obviously, it is not good to manually obtain each Action in ActionFactory. You do not need to obtain other Action instances every time you want to obtain an Action instance.

Question:
In an environment fully managed by Spring, how does one use Spring Context to obtain Bean instances? How to reference another non-singleton bean in a singleton bean?
This part is from the reference blog.
I have read that Spring provides two methods, which are actually the same as Struts2's method for obtaining the Context.

Implement the ApplicationContextAware Interface
@ Servicepublic class SpringContextHelper implements ApplicationContextAware {private ApplicationContext context; // provides an interface to obtain the Bean instance in the container and obtain the public Object getBean (String beanName) {return context according to the name. getBean (beanName) ;}@ Override public void setApplicationContext (ApplicationContext context) throws BeansException {this. context = context ;}}

Directly inject this class into the class that wants to use Context to obtain Bean.

Inherits the ApplicationObjectSupport abstract class that implements the ApplicationContextAware Interface

This class has been implemented in part, and it is easy to use.

@ Servicepublic class SpringContextHelper2 extends ApplicationObjectSupport {// provides an interface to obtain the Bean instance in the container and get the public Object getBean (String beanName) {return getApplicationContext () according to the name (). getBean (beanName );}}

Note:
Spring has many sub-interfaces that implement the Aware interface and their corresponding abstract classes. classes that implement these interfaces can obtain various resources of the corresponding Spring. For example, if our bean implements BeanFactoryAware, spring will call back the setBeanFactory method in the bean instantiation (getBean) phase and inject the BeanFactory instance. The implementation of the ApplicationContextAware interface in the preceding example can obtain the ApplicationContext instance.
Over.
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.