アプリケーションの起動

エンティティとリポジトリの作成後に、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"という名前のメソッドが含まれるクラスを検索します。