Skip to content

Latest commit

 

History

History
72 lines (65 loc) · 1.61 KB

README.md

File metadata and controls

72 lines (65 loc) · 1.61 KB

spring-jnr

Spring extension to load native libraries via jnr-ffi.

Tests are written for windows

Get it

Gradle

dependencies {
    compile group: 'com.github.goto1134', name: 'spring-jnr', version: '1.0'
}

Maven

<dependencies>
    <dependency>
      <groupId>com.github.goto1134</groupId>
      <artifactId>spring-jnr</artifactId>
      <version>1.0</version>
      <scope>compile</scope>
    </dependency>
</dependencies>

Use it

  1. Register BeanPostProcessor
@Bean
public BeanPostProcessor nativeLibraryBeanPostProcessor() {
    return new NativeLibraryBeanPostProcessor();
}
  1. Declare configuration qualifier
@Qualifier
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface MicrosoftVisualCRuntime {
}
  1. Declare library configuration
@MicrosoftVisualCRuntime
@Component
public class MicrosoftVisualCRuntimeConfiguration
        implements NativeLibraryConfiguration {

    @Override
    public LibraryInfo getLibraryInfo() {
        if (Platform.getNativePlatform()
                    .getOS() != Platform.OS.WINDOWS) {
            throw new IllegalStateException("Must be windows OS");
        }
        return new LibraryInfo("msvcrt", "", CallingConvention.STDCALL, true, IdentityFunctionMapper.getInstance());
    }
}
  1. Mark all libraries with the same qualifier
@MicrosoftVisualCRuntime
public interface PseudoRandomSequenceGenerator {
    int rand();
}
  1. Mark all fields where injection is needed
@NativeLibrary
private PseudoRandomSequenceGenerator generator;