Spring Advanced Tutorial Redefining beans after ApplicationContext initialization is complete

Source: Internet
Author: User

Before I had a very interesting question: I needed to bulk redefine a particular type of bean hosted by the spring container. Specifically, I have a lot of controller classes (controllers) and validator classes (Validator), and I want them to be multiple (Prototype), while spring creates instances by default as singleton (Singleton). A friend may ask: Why not add parameters to the bean definition @scope ("prototype")? My answer is simple: lazy ... Because my bean declaration is this:

  

  

Then I can't be very precise. Set the class instance of the controller and the validator as multiple cases, because I am very general here.

Next we explain how to use code implementation to set specific bean definition modifications.

applicationlistener-contextrefreshedevent

We can listen to a spring ApplicationContext event to let the Spring Bean container configuration complete and notify us to handle it.

<bean id= "Beandefineconfigue" class= "Com.xx.yy.zz.BeanDefineConfigue" ></bean>
1 public class Beandefineconfigue implements applicationlistener<contextrefreshedevent> {2     3     @ Override4 public     void Onapplicationevent (Contextrefreshedevent event) {5         6     }7}

Contextrefreshedevent is "Event raised when an applicationcontext gets initialized or refreshed. (Events generated when ApplicationContext initialization is complete or refreshed) "

Of course, we can "do something" within the onapplicationevent function!

beanfactory-beandefinition-registerbeandefinition
 1 public void Onapplicationevent (Contextrefreshedevent event) {2 Configurableapplicationcontext context = (confi Gurableapplicationcontext) Event.getapplicationcontext (); 3 Defaultlistablebeanfactory factory = (defaultlistablebeanfactory) context.getbeanfactory (); 4//Controller 5 string[] controllers = Factory.getbeannamesforannotation (Controller.class); 6 if (controllers! = NULL) {7 for (String controllerbeanname:controllers) {8 beandef Inition beandefine = factory.getbeandefinition (controllerbeanname); 9 String scope = Beandefine.getscope (); if (scope = = NULL | |!scope.equals (configurablebe                     Anfactory.scope_prototype)) {beandefine.setscope (configurablebeanfactory.scope_prototype); 12         Factory.registerbeandefinition (Controllerbeanname, beandefine); 13}14}15 }16//Checker object[] validators = factorY.getbeannamesfortype (Validator.class); if (validators! = null) {A for (Object _validatorbeanname:v alidators) {String validatorbeanname = string.valueof (_validatorbeanname); Beandefini                 tion Beandefine = factory.getbeandefinition (validatorbeanname); String scope = Beandefine.getscope (); 23 if (scope = = NULL | |!scope.equals (configurablebeanfactory.scope_prototype)) {Beande Fine.setscope (Configurablebeanfactory.scope_prototype); Factory.registerbeandefinition (ValidatorBean Name, Beandefine); 26}27}28}29}

You can see that the core code is very small, but also easy to understand! I've defined all the beans for the controller class and the validator class (using the Getbeannamesfortype function to get all the bean definitions for a given type and its subtypes; the controller type detection above is using spring's @controller. This is because my personal business needs are different, everyone notice, beanfactory in the various methods of viewing the API flexible use, detected that they are not prototype when the mandatory reset!

say at the end

Spring as a Hashtable, it has a lot of key values, that is, the bean definition (including the bean relationship, etc.), followed by spring will not be produced in thin air, and not just for you to host objects, we use Spring is ultimately {new Xxyyzzapplicationcontext (). Getbean (XXYYZZ)}, the contextloaderlistener you defined in Web. XML, or other middleware (struts, etc.).

"Everything has its origins. "So if a friend watching this blog is doing unit testing and discovering that auto-injection is not implemented, see if you created an object for the spring container.

Spring Advanced Tutorial Redefining beans after ApplicationContext initialization is complete

Related Article

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.