Java uses spring annotation as reflection mechanism

Foreword: I have always felt that the annotation assembly bean mechanism provided by spring is very good, especially when doing something similar to the Java reflection mechanism.

 

Need one sentence: automatically load the designated settlement bank for each exchange.

 

Demand analysis: The project needs to adapt to many exchanges, and each exchange has its own designated settlement bank, or one or more, so when the project is running on the A exchange, it can automatically load the specified exchange of the A exchange. Settlement banks A1 and A2, when the project is running on the B exchange, can automatically load the settlement bank B1 designated by the B exchange.

 

Solution: 1. Determine the settlement bank of the exchange for the project through the configuration file

#A

banks=A1,A2

#B

#banks=B1

 

2. When the project is loaded, load the configuration information into the manager management class

// load configuration file

String path = Manager.class.getClass().getResource("/").getPath() + "conf/bank.properties";

Properties p = new Properties();

p.load(new FileInputStream(new File(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 preparations are done, the key point is here. Anyone who knows bank settlement knows that the daily exchange needs to sign in to the settlement bank, so the sign-in method of each bank is different. Now we are A1, A2, B1 Bank first do the sign-in method

public class A1CheckInThread implements Runnable {

private static Logger logger = Logger.getLogger(A1CheckInThread.class);

 

@Override

public void run() {

try {

logger.info("Sign in to A1...");

I will not write the sign-in methods of A2 and B3, respectively.

A2CheckInThread、<pre name="code" class="java">B1CheckInThread

 

Now the question is, how to use the configuration information A1, A2, B3 to run the corresponding check-in method, the first thing you may think of is the reflection mechanism of Java, use the A1, A2, B1 strings to find the corresponding class and start it . I won't talk about this. In general, our projects use spring, so if there is spring, it provides us with more choices. When spring starts, it will pass the "<context: The component-scan base-package="com.honzh">" element automatically assembles the classes with spring annotations, OK, let's transform the A1CheckInThread class

@Component

public class A1CheckInThread implements Runnable {

After adding this annotation, it indicates that the class is a spring component, and then we use a spring management class to obtain the bean class

public class SpringUtils {

private static ApplicationContext factory;

 

private SpringUtils() {

};

 

public synchronized static ApplicationContext getSpringContext() throws BeansException {

if (factory == null) {

factory = new ClassPathXmlApplicationContext("applicationContext.xml");

}

return factory;

}

 

/**

* @Title: startRunnable

* @Description: start the specified runnable

* @param lastRunnableName

*/

public static void startRunnable(String lastRunnableName) {

for (String bank : getList()) {

logger.info(bank + lastRunnableName + "Start running");

Runnable runnable = SpringUtils.getRunnableByName(bank + lastRunnableName);

new Thread(runnable).start();

}

}

When the project runs, we pass

// spring initialization

SpringUtils.getSpringContext();

 

Then by specifying "A1CheckInThread", the method getRunnableByName will get the corresponding instantiated A1CheckInThread class, so when we check in, we can do this

for (String bank : getList()) {

logger.info(bank + lastRunnableName + "Start running");

Runnable runnable = SpringUtils.getRunnableByName(bank + lastRunnableName);

new Thread(runnable).start();

}

Where lastRunnableName is "CheckInThread".

 

Summary: This mechanism is often used, but if there is no way to find it, it will be tangled. In addition, even if exchange A has A3 settlement bank, we only need to modify the configuration file, load the check-in method corresponding to A3, and it will be OK .

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326403534&siteId=291194637