Java 9 和 11 通常需要使用自定义类加载器。所以我创建了一个BootstrapClassloader
预加载我的类路径,并使用Thread.currentThread().setContextClassLoader(classloader)
它来使其成为默认值。
我很快发现 Spring 在使用new ClassPathXmlApplicationContext()
.
我想没问题,并写了一个我调用的变体BootstrapClassLoader
来尝试强制 Spring 使用我的类加载器。但是 Spring 仍然忽略了我的类加载器。
例如,我验证了 aopalliance-1.0.jar 在类加载器的类路径中,并且可以手动加载MethodInterceptor
. 但是,如果我有一个标准 DBCP2 数据源 bean 的上下文(取决于 aopalliance),它会抛出一个ClassNotFoundException
for org.aopalliance.intercept.MethodInterceptor
.
任何人都可以在下面的代码中看到一个缺陷,或者是否有一些特殊的方法来注册类加载器,以便 Spring 始终在应用程序中使用它?
数据源片段:
<bean id="msSqlDataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.microsoft.sqlserver.jdbc.SQLServerDriver"/>
<property name="validationQuery" value="select 1" />
</bean>
<bean id="dbEmailDataSource" parent="msSqlDataSource" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close" lazy-init="true">
<property name="url" value="" />
<property name="username" value="" />
<property name="password" value="" />
</bean>
测试应用代码:
public static void main(String[] args) {
try {
Bootstrap.init();
for(String urlName : Bootstrap.getClasspathUrlNames()) {
if(StringUtils.containsIgnoreCase(urlName, "aopalliance")) {
System.out.println(urlName);
}
}
ConfigurableApplicationContext ctx = new BootstrappedApplicationContext("DbEmailContext.xml");
ctx.close();
}catch(Throwable t) {
t.printStackTrace();
}
try {
Class clazz = Bootstrap.getClassLoader().loadClass("org.aopalliance.intercept.MethodInterceptor");
System.out.println("manually loaded: "+clazz.getName());
}catch(Throwable t) {
t.printStackTrace();
}
}
BootstrappedApplicationContext(类似于 ClassPathXmlApplicationContext):
public class BootstrappedApplicationContext extends AbstractXmlApplicationContext {
private URLClassLoader classloader;
private Resource[] configResources;
public BootstrappedApplicationContext() {
super();
this.classloader = Bootstrap.getClassLoader();
super.setClassLoader(this.classloader);
}
public BootstrappedApplicationContext(ApplicationContext parent) {
this();
setParent(parent);
}
public BootstrappedApplicationContext(String configLocation)
throws BeansException {
this(new String[] {configLocation}, true, null);
}
public BootstrappedApplicationContext(String... configLocations)
throws BeansException {
this(configLocations, true, null);
}
public BootstrappedApplicationContext(String[] configLocations, ApplicationContext parent)
throws BeansException {
this(configLocations, true, parent);
}
public BootstrappedApplicationContext(String[] configLocations, boolean refresh)
throws BeansException {
this(configLocations, refresh, null);
}
public BootstrappedApplicationContext(String[] configLocations, boolean refresh, ApplicationContext parent)
throws BeansException {
this(parent);
setConfigLocations(configLocations);
if(refresh) {
refresh();
}
}
public BootstrappedApplicationContext(String path, Class<?>clazz)
throws BeansException {
this(new String[] {path}, clazz);
}
public BootstrappedApplicationContext(String[] paths, Class<?> clazz) throws BeansException {
this(paths, clazz, null);
}
public BootstrappedApplicationContext(String[] paths, Class<?> clazz, ApplicationContext parent)
throws BeansException {
this(parent);
Assert.notNull(paths, "Path array must not be null");
Assert.notNull(clazz, "Class argument must not be null");
this.configResources = new Resource[paths.length];
for (int i = 0; i < paths.length; i++) {
this.configResources[i] = new ClassPathResource(paths[i], clazz);
}
refresh();
}
@Override
public ClassLoader getClassLoader() {
if(classloader == null) {
classloader = Bootstrap.getClassLoader();
}
super.setClassLoader(classloader);
return classloader;
}
public void setClassLoader(URLClassLoader classloader) {
this.classloader = classloader;
super.setClassLoader(classloader);
}
@Override
protected Resource[] getConfigResources() {
return this.configResources;
}
}