アプリケーションの起動

Spring bootアプリケーションを実行するプログラムを作成する方法について学習します。

エンティティとリポジトリの作成後に、Springアプリケーションを実行するプログラムを作成する必要があります。そのために、Spring bootアプリケーションを使用することも、Spring coreアプリケーションを使用することもできます。

Spring bootアプリケーションを実行するために、@SpringBootApplication注釈が付いたクラスを作成します。独自のコードを作成するために、CommandLineRunnerインタフェースのrunメソッドをオーバーライドできます。

次に、Spring bootアプリケーションの例を示します。

/* The annotation helps to build an application using Spring Data Framework rapidly.*/
@SpringBootApplication
public class BootExample implements CommandLineRunner {

    /*The annotation enables Spring Data Framework to 
      look up the configuration file for a matching bean.*/
    @Autowired
    private StudentRepository nosqlRepo;

    @Override
    public void run(String... args) throws Exception {
        ...
    }
}

次に、Spring coreアプリケーションの例を示します。

public class CoreExample {
    public static void main(String[] args) {
        ApplicationContext ctx =
            new AnnotationConfigApplicationContext(AppConfig.class);
        NosqlOperations ops = (NosqlOperations)ctx.getBean("nosqlTemplate");
        ...
    }
}

ノート:

Spring Data Frameworkは、クラス・パスで@configuration注釈が付いていて、@Bean注釈が付いたNosqlTemplateという名前のメソッドが含まれるクラスを検索します。