Get a bean instance using spring's context in a completely spring-managed environment

Source: Internet
Author: User

In large-scale applications, there are often very flexible requirements, and after the use of the framework, although it can greatly improve the efficiency of development, but at the same time, we also frame to a shelf.
Now let's talk about the problems I have, and here's the thing:

@Component@Scope("Prototype") Public  class Action1 implements Action{.....   }@Component@Scope("Prototype") Public  class Action2implements Action{  .....   }@Component@Scope("Prototype") Public  class Action3implements 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}}  
based on condition
 @Componment  public  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, and when the app starts, I need to start the server thread, which holds a actionfactory instance, and in the Run method, gets an instance of the action with the Actionfactory instance every time. That's right. But careful analysis, the entire application will only have a server instance, that is, although the Actionfactory annotated scope is prototype, but in fact, the entire life cycle is only one actionfactory instance, then there is a problem, Because this actionfactory maintains an instance of each action as a domain, when calling Actionfactory#getaction in the server, the same action instance is returned for the same action name. This is troublesome, the thread is not safe. Think about, there is no other way (there is a better way to leave a message), you can only manually get the action in the Actionfactory, or simply manually get actionfactory, because the actionfactory added scope= Prototype, so once Actionfactory is acquired, all action objects held by Actionfactory are retrieved, and each action is also added Scope=prototype, so each action object is also new. Obviously it's not a good way to manually get individual actions in actionfactory. You can also get other action instances without having to get an instance of the action every time.

Business:
How do I get a bean instance using spring's context in a completely spring-managed environment? How do I refer to another non-singleton bean in one singleton bean?
This section comes from the contents of the blog in the reference.
Look at it. Spring offers two ways, in fact, in the same way that Struts2 gets the context

    • Implementing the Applicationcontextaware Interface
@Service Public  class springcontexthelper implements applicationcontextaware {    PrivateApplicationContext context;//provides an interface to get the bean instance in the container, which is obtained by name     PublicObjectGetbean(String beanname) {returnContext.getbean (Beanname); }@Override     Public void Setapplicationcontext(ApplicationContext context)throwsbeansexception { This. Context = Context; }}

Just inject this class into the class where you want to use the context to get the bean.

    • Inherits the Applicationobjectsupport abstract class that implements the Applicationcontextaware interface

Part of this class has been implemented, and it's easy to use.

 @Service  public  class  springcontexthelper2  extends  applicationobjectsupport  { //provides an interface to get the bean instance in the container, gets  public  Object getbean  (String beanname) {return  Getap    Plicationcontext (). Getbean (Beanname); }}

Note:
In spring, there are many sub-interfaces that implement the aware interface and the corresponding abstract classes, and the classes that implement these interfaces can fetch the various resources of spring. For example, our bean implementation, beanfactoryaware,spring, will callback the Setbeanfactory method during the bean instantiation (getbean) phase, injecting beanfactory instances. In the example above, the implementation of the Applicationcontextaware interface can obtain an instance of ApplicationContext.
Over.
Reference:
1.http://my.oschina.net/lilw/blog/172738
2.http://www.blogjava.net/syniii/archive/2010/11/24/338906.html
3.http://blog.csdn.net/java2000_wl/article/details/7378025
4.http://blog.csdn.net/luosijin123/article/details/8467654

Get a bean instance using spring's context in a completely spring-managed environment

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.