卸载Class?

由于在Java中有一个可以自己替换的ClassLoader,所以在Eclipse中很容易管理成千上万的插件,可以在同一个应用域中自由地装载和卸载。在运行时为某个Class重新定义一个ClassLoader并且重新实例化,原来加载的东西就交给GC了。这个机制还有一个我一直梦想的一个价值就是在Load一个Class到应用域的时候运行某些方法,例如读出配置文件。如果配置文件中涉及到一些额外的Class需要更替就不必重新启动应用程序了。这对于需要类似热部署的场合非常有用。
在.net中没有这么幸运。Assembly是决不能被卸载的。有一个解决方案是通过卸载AppDomain的方式来卸载一系列的Assembly。经过一小番试验,跨域的调用是可以实现的,不过需要遵循一点点小规则。
一、只有自MashalByRefObject派生的类型可以跨应用域访问。因为Calling的域使用的中Executing域中的实例通过Unwrap出来的代理类,所以其实并不是真正的“跨应用域”,而是类似Remoting的方式。
二、所有传递的数据类型必须是加上Serializable Attribute的类型。
我的应用场景是通过选项在运行时替换一个已升级的Assembly依赖。这个依赖显然不能写到主应用程序所依赖的Assembly清单中,而是通过从配置文档读出其相关的Assembly并从指定位置加载。
下面的例子是读出一个Assembly中的所有类型并将其继承关系显示到一个TreeView中。
这是个用来传递类型信息的数据类型:

ExpandedBlockStart.gif ContractedBlock.gif      /**/ /// <summary>
InBlock.gif    
/// 可在应用域间传递的详细类型信息
ExpandedBlockEnd.gif    
/// </summary>

None.gif     [Serializable]
None.gif    
public   class  TypeInfo
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
public TypeInfo(Type type)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            _Name 
= type.Name;
InBlock.gif            _FullName 
= type.FullName;
InBlock.gif            _BaseName 
= type.BaseType.FullName;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private string _Name, _FullName, _BaseName;
InBlock.gif
InBlock.gif        
public string Name
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return _Name;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public string FullName
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return _FullName;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public string BaseName
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return _BaseName;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

None.gif
这是个装载器:
ExpandedBlockStart.gif ContractedBlock.gif      /**/ /// <summary>
InBlock.gif    
/// 跨域工作的程序集装载器
ExpandedBlockEnd.gif    
/// </summary>

None.gif      public   class  AssemblyLoader : MarshalByRefObject
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
public TypeInfo [] GetAssemblyTypeInfos(string path)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Assembly assembly 
= Assembly.LoadFile(path);
InBlock.gif            Type [] types 
= assembly.GetExportedTypes();
InBlock.gif            TypeInfo [] typeInfos 
= new TypeInfo[types.Length];
InBlock.gif            
int i = 0;
InBlock.gif            
foreach (Type t in types)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                typeInfos[i 
++= new TypeInfo(t);
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return typeInfos;
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

None.gif
剩下的就是客户端代码了,拿到这个列表生成TreeView就没什么困难了:
None.gif              private  TypeInfo [] GetTypeInfos( string  path)
ExpandedBlockStart.gifContractedBlock.gif            
dot.gif {
InBlock.gif                AppDomain appDomain 
= AppDomain.CreateDomain("TemporaryAssembly");
InBlock.gif                Type t 
= typeof(AssemblyLoader);
InBlock.gif                AssemblyLoader loader 
= (AssemblyLoader) appDomain.CreateInstance(t.Assembly.FullName, t.FullName).Unwrap();
InBlock.gif                TypeInfo [] typeInfos 
= loader.GetAssemblyTypeInfos(path);
InBlock.gif                AppDomain.Unload(appDomain);
InBlock.gif                
return typeInfos;
ExpandedBlockEnd.gif            }

None.gif
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SPI(Service Provider Interface)是Java提供的一种服务发现机制,它允许第三方服务提供者在不需要修改代码的情况下,向程序动态地添加服务实现。 在Spring Boot中,SPI可以用来加载和卸载插件。Spring Boot提供了一个自动配置类`SpringFactoriesLoader`,它可以自动加载classpath下的`META-INF/spring.factories`文件,并根据其中定义的`EnableAutoConfiguration`类进行自动配置。 要实现SPI的加载和卸载,可以按照以下步骤进行: 1. 定义接口和接口实现类 定义一个接口,例如: ```java public interface Plugin { void doSomething(); } ``` 定义一个或多个实现类,例如: ```java public class PluginA implements Plugin { @Override public void doSomething() { System.out.println("Plugin A is doing something."); } } public class PluginB implements Plugin { @Override public void doSomething() { System.out.println("Plugin B is doing something."); } } ``` 2. 创建`META-INF/services`目录 在classpath下创建`META-INF/services`目录。 3. 创建服务提供者配置文件 在`META-INF/services`目录下创建一个以接口全限定名为文件名的文件,例如: ``` com.example.Plugin ``` 在文件中写入实现类的全限定名,例如: ``` com.example.PluginA com.example.PluginB ``` 4. 加载插件 使用`ServiceLoader`类加载插件,例如: ```java ServiceLoader<Plugin> plugins = ServiceLoader.load(Plugin.class); for (Plugin plugin : plugins) { plugin.doSomething(); } ``` 5. 卸载插件 可以将实现类从服务提供者配置文件中删除,再重新加载插件即可完成卸载。或者在程序中对实现类进行过滤,例如: ```java ServiceLoader<Plugin> plugins = ServiceLoader.load(Plugin.class); for (Plugin plugin : plugins) { if (plugin instanceof PluginA) { continue; } plugin.doSomething(); } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值