解决Bundle中使用反射时,报ClassNotFoundException异常

源代码下载

user-model:

user-model/user:

/**
 
 * @author wumingkun
 * @version 1.0.0
 * @Description
 */

package com.demo.user.user_model;

/**
 * @author wumingkun
 *
 */
public class User {
	private int id;
	private String name;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String toString() {
		return "User [id=" + id + ", name=" + name + "]";
	}
	
	
}


user-modle/pom.xml



	4.0.0

	com.demo.user
	user-model
	0.0.1-SNAPSHOT
	bundle

	user-model
	http://maven.apache.org

	
		UTF-8
	

	
		
			junit
			junit
			4.10
			test
		
	
	
		
			
				org.apache.felix
				maven-bundle-plugin
				true
				
					
					
				
			
		
	




user-service:

UserService

/**
 * 
 * @author wumingkun
 * @version 1.0.0
 * @Description
 */

package com.demo.user.user_service;

/**
 * @author wumingkun
 *
 */
public class UserSerivce {
	public void add(Object obj){
		System.out.println(obj+" add....");
	}
}

pom.xml


  4.0.0

  com.demo.user
  user-service
  0.0.1-SNAPSHOT
  bundle

  user-service
  http://maven.apache.org

  
    UTF-8
  

  
    
      junit
      junit
      4.10
      test
    
  
  
		
			
				org.apache.felix
				maven-bundle-plugin
				true
				
					
					
				
			
		
	




user-action:

Activator

/**

 * @author wumingkun
 * @version 1.0.0
 * @Description
 */

package com.demo.user.user_action;

import java.lang.reflect.Method;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;

import com.demo.user.user_service.UserSerivce;

/**
 * @author wumingkun
 *
 */
public class Activator implements BundleActivator {

	/* (non-Javadoc)
	 * @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
	 */
	public void start(BundleContext context) throws Exception {
		try {
			Class clz=Class.forName("com.demo.user.user_model.User");
			Object obj=clz.newInstance();
			Method setIdMethod=clz.getMethod("setId", new Class[]{int.class});
			setIdMethod.invoke(obj,1);
			Method setNameMethod=clz.getMethod("setName", new Class[]{String.class});
			setNameMethod.invoke(obj,"张三");
			UserSerivce serivce=new UserSerivce();
			serivce.add(obj);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/* (non-Javadoc)
	 * @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
	 */
	public void stop(BundleContext context) throws Exception {

	}

}


pom.xml

	4.0.0

	com.demo.user
	user-action
	0.0.1-SNAPSHOT
	bundle

	user-action
	http://maven.apache.org

	
		UTF-8
	

	
		
			junit
			junit
			4.10
			test
		
		
		
			org.osgi
			org.osgi.core
			4.2.0
		
		
		
			com.demo.user
			user-service
			0.0.1-SNAPSHOT
		
	
	
	
		
			
				org.apache.felix
				maven-bundle-plugin
				true
				
					
					com.demo.user.user_action.Activator
					
				
			
		
	




分析:

从user-action/pom.xml中可以看到它仅仅依赖了user-service,再看Activator.java文件中,import部份:

import java.lang.reflect.Method;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;

import com.demo.user.user_service.UserSerivce;

没有导入 com.demo.user.user_model,

查看MANIFEST.MF

Manifest-Version: 1.0
Bnd-LastModified: 1405153337259
Build-Jdk: 1.5.0_22
Built-By: Administrator
Bundle-Activator: com.demo.user.user_action.Activator
Bundle-ManifestVersion: 2
Bundle-Name: user-action
Bundle-SymbolicName: com.demo.user.action
Bundle-Version: 0.0.1.SNAPSHOT
Created-By: Apache Maven Bundle Plugin
Export-Package: com.demo.user.user_action;version="0.0.1.SNAPSHOT";uses:
 ="org.osgi.framework"
Import-Package: com.demo.user.user_service;version="[0.0,1)",org.osgi.fr
 amework;version="[1.5,2)"
Tool: Bnd-2.1.0.20130426-122213

它只导入了 com.demo.user.user_service及org.osgi.framework

当运行Bundle时就报错了:

java.lang.ClassNotFoundException: com.demo.user.user_model.User


解决办法

user-action/pom.xml的maven-bundle-plugin中,加入Bundle使用的所有外部包


	4.0.0

	com.demo.user
	user-action
	0.0.1-SNAPSHOT
	bundle

	user-action
	http://maven.apache.org

	
		UTF-8
	

	
		
			junit
			junit
			4.10
			test
		
		
		
			org.osgi
			org.osgi.core
			4.2.0
		
		
		
			com.demo.user
			user-service
			0.0.1-SNAPSHOT
		
	
	
	
		
			
				org.apache.felix
				maven-bundle-plugin
				true
				
					
					com.demo.user.user_action.Activator
					 
							com.demo.user.user_service,
							org.osgi.framework,
							com.demo.user.user_model
					
					
				
			
		
	


再次查看MANIFEST

Manifest-Version: 1.0
Bnd-LastModified: 1405154955217
Build-Jdk: 1.5.0_22
Built-By: Administrator
Bundle-Activator: com.demo.user.user_action.Activator
Bundle-ManifestVersion: 2
Bundle-Name: user-action
Bundle-SymbolicName: com.demo.user.action
Bundle-Version: 0.0.1.SNAPSHOT
Created-By: Apache Maven Bundle Plugin
Export-Package: com.demo.user.user_action;version="0.0.1.SNAPSHOT";uses:
 ="org.osgi.framework"
Import-Package: com.demo.user.user_service;version="[0.0,1)",org.osgi.fr
 amework;version="[1.5,2)",com.demo.user.user_model
Tool: Bnd-2.1.0.20130426-122213


部署运行:

User [id=1, name=张三] add....




你可能感兴趣的:(osgi)