Upgrade to Pro — share decks privately, control downloads, hide ads and more …

SpringBoot 3.0 のNative Imageを試してみた

Sponsored · Ship Features Fearlessly Turn features on and off without deploys. Used by thousands of Ruby developers.

SpringBoot 3.0 のNative Imageを試してみた

Avatar for Kazuhiro Seo

Kazuhiro Seo

January 29, 2023
Tweet

More Decks by Kazuhiro Seo

Other Decks in Programming

Transcript

  1. Bean @Data public class Item { private String id; private

    String name; private String code; }
  2. Controller @RestController @RequestMapping("/items") public class ItemController { @Autowired private ItemService

    service; @GetMapping("/") public Iterable<Item> index() { return service.list(); }
  3. Repository public interface ItemRepository { Item save(Item item); Iterable<Item> findAll();

    Optional<Item> findById(String id); void deleteById(String id); }
  4. Gradle plugins plugins { id 'java' id 'org.springframework.boot' version '3.0.1'

    id 'io.spring.dependency-management' version '1.1.0' id 'org.graalvm.buildtools.native' version '0.9.18' }
  5. bootBuildImage タスク Spring Boot Plugin のタスク Cloud Native Buildpacks を利用してビルド

    ソースコードからビルド構成を検知 Native BuildTools プラグインがある時Native Build
  6. 原因 - CASE 1 - AWS SDK for Java は

    v2 以降からNativeImage に対応 該当ライブラリはv1 にしか対応していなかった
  7. 対策 - CASE 1 - 公式のdynamodb-enhanced を利用 dependencies { implementation

    'software.amazon.awssdk:dynamodb' implementation 'software.amazon.awssdk:dynamodb-enhanced' }
  8. 対策 - CASE 1 - Bean とDynamoDB Table のマッピングを生成 @Bean

    DynamoDbTable<Item> itemTable(DynamoDbEnhancedClient client) { return client.table("Items", TableSchema.fromBean(Item.class)); }
  9. 対策 - CASE 1 - Repository を独自実装 @Component public class

    ItemRepositoryImpl implements ItemRepository { @Autowired private DynamoDbTable<Item> table; @Override public Item save(Item item) { table.putItem(item); return item; }
  10. 対策 - CASE 2 - StaticTableSchema を利用 private TableSchema<Item> getStaticTableSchema()

    { return StaticTableSchema.builder(Item.class) .newItemSupplier(Item::new) .addAttribute(String.class, a -> a.name("id").setter(Item::se .addAttribute(String.class, a -> a.name("name").setter(Item:: .addAttribute(String.class, a -> a.name("code").setter(Item:: .build(); }