Using spring annotations to do a similar Java reflection mechanism

Source: Internet
Author: User

Preface : Always feel that spring provides the annotation assembly bean mechanism is very good, especially when doing similar to the Java reflection mechanism is too convenient, the following is based on the project requirements for a record, convenient for later use.

Demand sentence: Automatically load the designated clearing bank for each exchange.

Demand analysis: The project needs to adapt to a number of exchanges, and each exchange has its own designated clearing bank, or one or more, then the project will be able to automatically load a Exchange designated clearing bank A1, A2, when the project on the B exchange run, Be able to automatically load the clearing bank B1 designated by the B Exchange.

Solution: 1. Determine the clearing bank of an exchange for a project through a configuration file

#Abanks =A1,A2#B#BANKS=B1

2. Load the configuration information into the manager management class when the project is loaded

Load configuration file string path = Manager.class.getClass (). GetResource ("/"). GetPath () + "conf/bank.properties"; Properties P = new properties ();p. Load (new FileInputStream (path)); list = new arraylist<string> (); String banks = P.getproperty ("Banks"), if (Banks.indexof (",")! =-1) {string[] bankstr = Banks.split (","); for (String Bank : bankstr) {List.add (Bank.touppercase ());}} else {List.add (banks.touppercase ());}
3. After the preparation is done, the key point is to know that the Bank settlement is aware that the daily exchange needs to check in the clearing bank, then each bank check-in method is not the same, now we for A1, A2, B1 Bank first Good registration method
public class A1checkinthread implements Runnable {private static Logger Logger = Logger.getlogger (A1checkinthread.class) ; @Overridepublic void Run () {try {logger.info ("A1 sign in ...");
A2, B3 registration method I will not write, respectively, for
A2checkinthread, <pre name= "code" class= "Java" >b1checkinthread

Now the question, how to use the configuration information A1, A2, B3 to run the corresponding check-in method, you may first think of is the Java reflection mechanism, the use of A1, A2, B1 string to find the corresponding class, to start. I will not say this, in general, our project is to use the spring, then there is spring, he gives us more choices, spring at the start, will be throughApplicationcontext.xmlIn the
<context:component-scan base-package= "Com.honzh" >
element to automatically assemble classes with spring annotations, OK, let's change the A1checkinthread class
@Componentpublic class A1checkinthread implements Runnable {
Adding this annotation indicates that the class is a spring component, and then we use a spring management class for the Bean class to get work
public class Springutils {private static ApplicationContext factory;private springutils () {};p ublic synchronized static ApplicationContext Getspringcontext () throws Beansexception {if (factory = = null) {factor y = new Classpathxmlapplicationcontext ("Applicationcontext.xml");} return factory;} <span style= "White-space:pre" ></span>/** <span style= "White-space:pre" ></span>* @ Title:getrunnablebyname <span style= "White-space:pre" ></span>* @Description: Get the corresponding assembly Bean.<span style= "White-space:pre" ></span>* @param runnablename<span style= According to the Runnable class name White-space:pre "></span>* @return <span style=" White-space:pre "></span>*/<span style=" White-space:pre "></span>public static Runnable Getrunnablebyname (String runnablename) {<span style=" White-space:pre "></span>return Factory.getbean (Runnablename, runnable.class); <span style=" White-space :p re "></SPAN>} 
While the project is running, we
Spring initializes the Springutils.getspringcontext ();

Then by specifying "A1checkinthread", the method Getrunnablebyname gets the corresponding instantiated A1checkinthread class, so we can do this when we check in.
For (String bank:getlist ()) {logger.info (bank + lastrunnablename + "start start"); Runnable Runnable = springutils.getrunnablebyname (bank + lastrunnablename); new Thread (Runnable). Start ();
One of the lastrunnablename is "Checkinthread".

Summary : This mechanism is often used, but there is no way to find a more tangled, and so even if a exchange has A3 clearing Bank, we only need to modify the configuration file, load the corresponding A3 registration method, OK.








Using spring annotations to do a similar Java reflection mechanism

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.