Spring annotations Complete the Java reflection mechanism

Source: Internet
Author: User

It has always been felt 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, easy to use later.
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
#A
Banks=a1,a2
#B
#banks =B1

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

//Load configuration fileString Path = Manager.class. GetClass (). GetResource ("/"). GetPath () + "Conf/bank.properties"; Properties P=NewProperties ();p. Load (NewFileInputStream (NewFile (path)); List=NewArraylist<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 Implements Runnable {privatestatic Logger Logger = Logger.getlogger (a1checkinthread.  Class), @Overridepublicvoid  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 projects are used in spring, then there is spring, if you do not spring, it is recommended to find some Java video tutorials to see. He gives us more options, and spring starts with the Applicationcontext.xml
<context:component-scan base-package= "Com.honzh" >
element to automatically assemble classes with spring annotations, OK, let's change the A1checkinthread class
@Component
public 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 classSpringutils {Private StaticApplicationContext Factory;Privatespringutils () {}; Public synchronized StaticApplicationContext Getspringcontext ()throwsbeansexception {if(Factory = =NULL) {Factory=NewClasspathxmlapplicationcontext ("Applicationcontext.xml");}returnFactory;}<span style= "White-space:pre" > </span>/**<span style= "White-space:pre" > </span>* @Title: Getrunnablebyname <span style= "White-space:pre" &    Gt </span>* @Description: Get the corresponding assembly Bean.<span style= White-space:pre > </span>* According to the Runnable class name@paramrunnablename<span style= "White-space:pre" > </span>*@return<span style= "White-space:pre" > </span>*/<span style= "White-space:pre" > </span> Public StaticRunnable getrunnablebyname (String runnablename) {<span style= "White-space:pre" > </span>returnFactory.getbean (Runnablename, Runnable.class);<span style= "White-space:pre" > </span>}

While the project is running, we
Spring initialization
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 + lastrunnablename + "start Start"= 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.

Spring annotations Complete the Java reflection mechanism

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.