Skip to content

Commit 7879b08

Browse files
author
jossc
committed
添加模板隔离
1 parent 5fa4592 commit 7879b08

13 files changed

Lines changed: 277 additions & 53 deletions

File tree

.idea/compiler.xml

Lines changed: 4 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

moduleA/pom.xml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
6+
<groupId>moduleA</groupId>
7+
<artifactId>moduleTest</artifactId>
8+
<packaging>jar</packaging>
9+
<modelVersion>4.0.0</modelVersion>
10+
<version>1.0-SNAPSHOT</version>
11+
<build>
12+
<plugins>
13+
<plugin>
14+
<groupId>org.apache.maven.plugins</groupId>
15+
<artifactId>maven-compiler-plugin</artifactId>
16+
<configuration>
17+
<source>8</source>
18+
<target>8</target>
19+
</configuration>
20+
</plugin>
21+
</plugins>
22+
</build>
23+
24+
25+
</project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package org.moudleA;
2+
3+
/**
4+
* @ClassName MoudleServiceA
5+
* @Author chenzhuo
6+
* @Version 1.0
7+
* @Date 2019-06-30 14:41
8+
**/
9+
public class MoudleServiceA {
10+
public String sayHelloServiceA(){
11+
return "---sayHelloServiceA---";
12+
}
13+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package org;

moduleB/pom.xml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
6+
<groupId>moduleB</groupId>
7+
<artifactId>moduleBTest</artifactId>
8+
<packaging>jar</packaging>
9+
<modelVersion>4.0.0</modelVersion>
10+
<version>1.0-SNAPSHOT</version>
11+
<build>
12+
<plugins>
13+
<plugin>
14+
<groupId>org.apache.maven.plugins</groupId>
15+
<artifactId>maven-compiler-plugin</artifactId>
16+
<configuration>
17+
<source>8</source>
18+
<target>8</target>
19+
</configuration>
20+
</plugin>
21+
</plugins>
22+
</build>
23+
24+
</project>
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.moudleB;
2+
3+
/**
4+
* @ClassName MoudleServiceB
5+
* @Author chenzhuo
6+
* @Version 1.0
7+
* @Date 2019-06-30 14:42
8+
**/
9+
public class MoudleServiceB {
10+
11+
public String sayHelloServiceB(){
12+
return "---sayHelloServiceB---";
13+
}
14+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package org;

pom.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
<groupId>com.cz</groupId>
88
<artifactId>basics.code</artifactId>
9+
<packaging>pom</packaging>
910
<version>1.0-SNAPSHOT</version>
1011
<build>
1112
<plugins>
@@ -20,6 +21,7 @@
2021
</plugins>
2122
</build>
2223

24+
2325
<dependencies>
2426
<!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
2527
<dependency>

src/main/java/com/basics/classloadertest/ClassLoaderManager.java

Lines changed: 0 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -52,56 +52,4 @@ private String getClassName(JarEntry entry) {
5252
System.out.println("entryName: " +entryName);
5353
return entryName.substring(0, entryName.length() - 6);
5454
}
55-
56-
public void init(){
57-
58-
System.out.println("---- load all ----");
59-
List<Future<String>> futureList = new ArrayList<>();
60-
for(String moudle:moudleList){
61-
Future<String> future = executor.submit(() -> {
62-
try{
63-
URL[] moduleUrl = new URL[]{new URL("file://"+ moudle)};
64-
@SuppressWarnings("resource")
65-
URLClassLoader classLoader = new URLClassLoader(moduleUrl );
66-
@SuppressWarnings("resource")
67-
JarFile jar = new JarFile(new File(moudle));
68-
Enumeration<JarEntry> entries = jar.entries();
69-
while (entries.hasMoreElements()) {
70-
JarEntry entry = entries.nextElement();
71-
String className = getClassName(entry);
72-
if (className == null ) {
73-
continue;
74-
}
75-
76-
try {
77-
Class<?> clazz = classLoader.loadClass(className);
78-
//放到内存里
79-
cache.put(className, clazz);
80-
} catch (Throwable t) {
81-
//System.out.println(t.getLocalizedMessage());
82-
}
83-
}
84-
}catch(Exception e){
85-
System.out.println(e.getLocalizedMessage());
86-
}
87-
88-
return moudle;
89-
});
90-
futureList.add(future);
91-
}
92-
93-
for(Future<String> future:futureList){
94-
try {
95-
String moduleName = future.get();
96-
System.out.println("---load moudle " + moduleName + " ok" );
97-
} catch (InterruptedException e) {
98-
e.printStackTrace();
99-
} catch (ExecutionException e) {
100-
e.printStackTrace();
101-
}
102-
}
103-
executor.shutdown();
104-
System.out.println("----end load All module----");
105-
106-
}
10755
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.share.classloader;
2+
3+
4+
import java.sql.Driver;
5+
import java.util.Iterator;
6+
import java.util.ServiceLoader;
7+
8+
/**
9+
* @ClassName DriverTest
10+
* @Author chenzhuo
11+
* @Version 1.0
12+
* @Date 2019-06-30 14:16
13+
**/
14+
public class DriverTest {
15+
16+
public static void main(String[] args) {
17+
ServiceLoader<Driver> loader = ServiceLoader.load(Driver.class);
18+
//(2)
19+
Iterator<Driver> iterator = loader.iterator();
20+
while (iterator.hasNext()) {
21+
Driver driver = (Driver) iterator.next();
22+
System.out.println("driver:" + driver.getClass() + ",loader:" + driver.getClass().getClassLoader());
23+
}
24+
//(3)
25+
System.out.println("current thread contextloader:" + Thread.currentThread().getContextClassLoader());
26+
//(4)
27+
System.out.println("ServiceLoader loader:" + ServiceLoader.class.getClassLoader());
28+
}
29+
}

0 commit comments

Comments
 (0)