In the process of non WEB project (Springboot) In development , Use classLoader Load dynamically jar, And using the interface for strong type conversion is no problem , They use JVM Under the URLClassLoader To implement , And on the basis of tomcat When you use it in your container , There’s a problem with types that can’t be found , Here’s why :
tomcat There’s a man named webApp It loads first WEB-INF/classes After loading WEB-INF/lib, But its father loader is its common loader ,comon The parent loader is system loader ( and JVM The Application Loader of the application is similar in function , But other things are specified tomcat Loading under Directory , You can look at the English documents on the official website ), But the loader in the source code is URLClassLoader Subclasses of , and URLClassLoader Default parent load tomcat Next is its system The loader is so designed and tomcat Of
Configure the , The default is inaction false, It will be entrusted directly to tomcat Of system Loader loading system Delegate to the top Bootstrap loader ( Is almost JVM The combination of the start loader and the extended loader ), But anyway , The project in tomcat Custom or URLClassLoader Loading the default parent loader will not be tomcat Of webApp The loader is system loader , Or custom loaders or URLClassLoader and tomcat Of webApp The loader has no relationship , So when dynamically creating a class, it will definitely report when designing other classes CNF abnormal .
Reference resources
https://blog.csdn.net/chenleixing/article/details/50042795
https://blog.csdn.net/chenleixing/article/details/47121661
http://tomcat.apache.org/tomcat-7.0-doc/class-loader-howto.html
Tomcat ClassLoader
resolvent
One The solution is to get the current class first Class, Then get the loader of the current class , In custom loaders or URLClassLoader Loaders are specified as their parent when they are created , In this way, the problem will be solved easily , Maybe we don’t encounter this problem when we write a simple example , Because we were URLClassLoader Or the parent loader of a custom loader is JVM The third loader of is the application load , It’s dedicated to loading classpath The following or specified class or jar Of , According to the parental delegation model , You’ll definitely find the class that introduced the path or jar Of .
Two Use Class.forName() To dynamically load the specified class , There would be no such problem , Because this way, on the one hand, is able to initialize the static things of the class , And then it’s important , Is to use the loader that loads the current class to load the class you specify , So you’re here tomcat That’s it webApp Loaders , It’s definitely not going to happen again , It may be found directly from the cache .
Dynamic loading code
URL url = new URL(packageUrl);
// IDEA Debug with java Runtime ClassLoader Is different , So we need to use the current environment ClassLoader
ClassLoader loader = new URLClassLoader(new URL[]{url}, clazz.getClassLoader()) {
@Override
public Class<?> loadClass(String name) throws ClassNotFoundException {
try {
String fileName = name.substring(name.lastIndexOf(".") + 1) + ".class";
InputStream is = getClass().getResourceAsStream(fileName);
if (is == null) {
return super.loadClass(name);
}
byte[] b = new byte[is.available()];
is.read(b);
return defineClass(name, b, 0, b.length);
} catch (IOException e) {
e.printStackTrace();
throw new ClassNotFoundException(name);
}
}
};
hordeolum keflex dosage can i drink on keflex is keflex and cephalexin the same thing
keflex and rash cephalexin teaching para que sirve el cephalexin 500 mg
cephalexin tooth infection keflex cross reactivity with penicillin keflex dosing for cellulitis
is amoxicillin good for sinus infection how much is amoxicillin at walgreens can i take amoxicillin and ibuprofen at the same time
how much is amoxicillin without insurance how to take amoxicillin and metronidazole together 1000 mg amoxicillin
875 mg of amoxicillin where can i get amoxicillin for dogs without vet prescription how much amoxicillin for uti
ciprofloxacin and tylenol ciprofloxacin ear drops perforated eardrum is ciprofloxacin ear drops safe in pregnancy
ciprofloxacin sinus infection ciprofloxacin beipackzettel ciprofloxacin and omeprazole
cialis reviews cialis and nitrates order tadalafil 20mg
canada prescription viagra generic viagra soft tabs 100mg viagra mail order
cheap viagra online in india online pharmacy viagra cheap real viagra pills for sale
example of 200 word essay custom essay writing reviews best essay writer
steps to writing an essay essays about service common app essay
hire someone to write my essay argumentative essay topics essays on the movie the help
best essay writing how to start a reflective essay literary essay
online pharmacy no prescription klonopin online pharmacy viagra uk australian pharmacy viagra
Brand Levitra the pharmacy store apopka nile rx pharmacy
best online pharmacy to buy hydrocodone target pharmacy amlodipine best online pharmacy that does not require a prescription
tadalafil troche (lozenge) max dose tadalafil rats army tadalafil
highest dose of tadalafil tadalafil accord 20 mg sildalis sildenafil tadalafil
goodrx tadalafil coupon tadalafil 30mgx30ml liquid dosage tadalafil 2.5
cialis tadalafil 5mg tadalafil 20mg la thuốc gì cialis o tadalafil
how long does tadalafil take to work tadalafil for erectile dysfunction tadalafil workout
hims tadalafil review tadalafil peptide reviews tadalafil oxytocin pt-141
lapela tadalafil 20 mg tadalafil workout how long is tadalafil effective
tadalafil for women can you drink wine or liquor if you took in tadalafil how long to wait after taking tadalafil
tadalafil apomorphine troche can tadalafil be crushed does tadalafil work with alcohol
no prescription tadalafil what does tadalafil 20 mg look like tadalafil 5 mg tablet uses