OSGi bundle之间通过反射调用方法

两个bundle之间不通过pom文件引入jar包方式调用另一个方法,通过反射的方式调用。

前提:被调用的方法的bundle需要配置  <Export-Package> 要提供的被引用的包</Export-Package>

调用方需要配置 <Import-Package>引入的包名,多个可通过,隔开</Import-Package>

如此便可以通过java反射来调用另一个bundle中的方法。

示例export-package,import-package如何配置(通过pom文件中配置bnd来实现):

<!-- 由BND来完成JAR包的OSGI化 -->
	<build>
		<resources>
			<resource>
				<filtering>true</filtering>
				<directory>src/main/resources</directory>
			</resource>
		</resources>
		<plugins>
			<plugin>
				<groupId>org.apache.felix</groupId>
				<artifactId>maven-bundle-plugin</artifactId>
				<!-- <version>${maven-bundle-plugin.version}</version> -->
				<version>3.5.0</version>
				<extensions>true</extensions>
				<configuration>
					<instructions>
						<Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
						<Bundle-Version>${project.version}</Bundle-Version>
						<Bundle-Activator>com.myDemo.test.Activator</Bundle-Activator>
						<Export-Package>com.myDemo.test*;version=${project.version}</Export-Package>
						<Embed-Dependency>*;scope=compile|runtime</Embed-Dependency>
						<Embed-Directory>target/dependency</Embed-Directory>
						<Embed-Transitive>true</Embed-Transitive>
					<Import-Package> 
							com.mydemo.user.service,
							org.osgi.framework,
							*;resolution:=optional
					</Import-Package>
					</instructions>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-resources-plugin</artifactId>
				<version>${maven.resources.version}</version>
				<executions>
					<execution>
						<id>filter</id>
						<goals>
							<goal>resources</goal>
						</goals>
						<phase>generate-resources</phase>
					</execution>
				</executions>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<configuration>
					<source>8</source>
					<target>8</target>
					<!-- <maxmem>256M</maxmem> -->
				</configuration>
			</plugin>

		</plugins>
	</build>

之后使用java反射即可,如同普通java程序中一样。

猜你喜欢

转载自blog.csdn.net/m0_37657585/article/details/82973162