0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

PHPerがKotlinを勉強するNo.1〜DI注入が簡単にできる〜

0
Posted at

静的型付け言語に挑戦したいと思い、Kotlinを勉強していこうと思います。

当面の間、教材は以下のものを使って進めていきます。
https://zenn.dev/msksgm/books/implementing-server-side-kotlin-development

今回、SpringBootというフレームワークを使っていて思ったのは、DI注入が簡単にできるということです。

Laraveと比較したいと思います。
PHP8.0以上で使えるconstructor promotionを使うと、以下のように書けます。

<?php

namespace App\Http\Controllers;

use App\Services\SampleService;
use Illuminate\Routing\Controller;

class SampleController extends Controller
{
    public function __construct(
        private SampleService $sampleService
    ) {}
}

constructor promotionのおかげで、コンストラクタに記述するだけなのですが、Kotlinではプライマリコンストラクタを使って以下のように書けます。

@RestController
class SampleController(val sampleService: SampleService) // 型だけで自動注入!

@Service
class SampleService // @Serviceで自動登録

非常にシンプルに書けて、楽だなと思いました。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?