io.micronaut.context.ApplicationContext.run()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(13.2k)|赞(0)|评价(0)|浏览(103)

本文整理了Java中io.micronaut.context.ApplicationContext.run()方法的一些代码示例,展示了ApplicationContext.run()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ApplicationContext.run()方法的具体详情如下:
包路径:io.micronaut.context.ApplicationContext
类名称:ApplicationContext
方法名:run

ApplicationContext.run介绍

[英]Run the ApplicationContext. This method will instantiate a new ApplicationContext and call #start().
[中]运行ApplicationContext。此方法将实例化一个新的ApplicationContext并调用#start()。

代码示例

代码示例来源:origin: micronaut-projects/micronaut-core

/**
 * Main method.
 * @param args The arguments
 */
public static void main(String... args) {
  // enable Graal class analysis
  System.setProperty(GraalClassLoadingReporter.GRAAL_CLASS_ANALYSIS, Boolean.TRUE.toString());
  if (ArrayUtils.isNotEmpty(args)) {
    System.setProperty(GraalClassLoadingReporter.REFLECTION_JSON_FILE, args[0]);
  }
  try {
    ApplicationContext applicationContext = ApplicationContext.run();
    // following beans may impact classloading, so load them.
    applicationContext.findBean(EmbeddedServer.class);
    applicationContext.getBeansOfType(ExecutableMethodProcessor.class);
    // finish up
    applicationContext.stop();
  } catch (Throwable e) {
    System.err.println("An error occurred analyzing class requirements: " + e.getMessage());
    e.printStackTrace(System.err);
  }
}

代码示例来源:origin: micronaut-projects/micronaut-core

@BeforeClass
public static void setupServer() {
  System.setProperty(Environment.BOOTSTRAP_CONTEXT_PROPERTY, "true");
  Map<String, Object> map = new HashMap<>();
  map.put(MockSpringCloudConfigServer.ENABLED, true);
  map.put("micronaut.server.port", -1);
  map.put("spring.cloud.config.enabled", false);
  map.put("micronaut.environments", "dev,test");
  springCloudServer = ApplicationContext.run(EmbeddedServer.class, map);
  server = ApplicationContext.run(EmbeddedServer.class, CollectionUtils.mapOf(
      "micronaut.environments","test",
      "micronaut.application.name", "spring-config-sample",
      "micronaut.config-client.enabled", true,
      "spring.cloud.config.enabled", true,
      "spring.cloud.config.uri", springCloudServer.getURI()
  ));
  client = server
      .getApplicationContext()
      .createBean(HttpClient.class, LoadBalancer.fixed(server.getURL()));
}

代码示例来源:origin: micronaut-projects/micronaut-core

@Test
  public void testSenderHeaders() throws Exception {

    Map<String,Object> config =Collections.singletonMap(
        "pet.client.id", "11"
    );

    try(EmbeddedServer embeddedServer = ApplicationContext.run(EmbeddedServer.class, config)) {
      PetClient client = embeddedServer.getApplicationContext().getBean(PetClient.class);

      Pet pet = client.get("Fred").blockingGet();

      Assert.assertNotNull(pet);

      Assert.assertEquals(pet.getAge(), 11);
    }

  }
}

代码示例来源:origin: micronaut-projects/micronaut-core

@Test
  public void testSenderAttributes() throws Exception {

    try(EmbeddedServer embeddedServer = ApplicationContext.run(EmbeddedServer.class)) {
      StoryClient client = embeddedServer.getApplicationContext().getBean(StoryClient.class);
      StoryClientFilter filter = embeddedServer.getApplicationContext().getBean(StoryClientFilter.class);

      Story story = client.getById("jan2019").blockingGet();

      Assert.assertNotNull(story);

      Map<String, Object> attributes = filter.getLatestRequestAttributes();
      Assert.assertNotNull(attributes);
      Assert.assertEquals("jan2019", attributes.get("story-id"));
      Assert.assertEquals("storyClient", attributes.get("client-name"));
      Assert.assertEquals("1", attributes.get("version"));
    }
  }
}

代码示例来源:origin: micronaut-projects/micronaut-core

@Test
public void testClientAnnotationStreaming() throws Exception {
  try( EmbeddedServer embeddedServer = ApplicationContext.run(EmbeddedServer.class) ) {
    HeadlineClient headlineClient = embeddedServer
        .getApplicationContext()
        .getBean(HeadlineClient.class);
    Event<Headline> headline = headlineClient.streamHeadlines().blockFirst();
    assertNotNull( headline );
    assertTrue( headline.getData().getText().startsWith("Latest Headline") );
  }
}
// end::streamingClient[]

代码示例来源:origin: micronaut-projects/micronaut-core

@Test
  public void testPostInvalidFormData() {
    EmbeddedServer embeddedServer = ApplicationContext.run(EmbeddedServer.class);
    RxHttpClient client = embeddedServer.getApplicationContext().createBean(RxHttpClient.class, embeddedServer.getURL());

    // tag::postform[]
    Map<String, String> data = new LinkedHashMap<>();
    data.put("title", "The Stand");
    data.put("pages", "notnumber");
    data.put("url", "noturl");
    Flowable<HttpResponse<Book>> call = client.exchange(
        POST("/binding/book", data)
            .contentType(MediaType.APPLICATION_FORM_URLENCODED),
        Book.class
    );
    // end::postform[]

    thrown.expect(HttpClientResponseException.class);
    thrown.expectMessage(CoreMatchers.startsWith("Failed to convert argument [pages] for value [notnumber]"));

    HttpResponse<Book> response = call.blockingFirst();

    embeddedServer.stop();
    client.stop();
  }
}

代码示例来源:origin: micronaut-projects/micronaut-core

@Test
public void testClientAnnotationStreaming() throws Exception {
  try( EmbeddedServer embeddedServer = ApplicationContext.run(EmbeddedServer.class) ) {
    HeadlineClient headlineClient = embeddedServer
                      .getApplicationContext()
                      .getBean(HeadlineClient.class); // <1>
    Maybe<Headline> firstHeadline = headlineClient.streamHeadlines().firstElement(); // <2>
    Headline headline = firstHeadline.blockingGet(); // <3>
    assertNotNull( headline );
    assertTrue( headline.getText().startsWith("Latest Headline") );
  }
}
// end::streamingClient[]

代码示例来源:origin: micronaut-projects/micronaut-core

@Test
  public void testPostPetValidation() {
    EmbeddedServer embeddedServer = ApplicationContext.run(EmbeddedServer.class);
    PetClient client = embeddedServer.getApplicationContext().getBean(PetClient.class);

    // tag::error[]
    thrown.expect(HttpClientResponseException.class);
    thrown.expectMessage("age: must be greater than or equal to 1");
    client.save("Fred", -1).blockingGet();
    // end::error[]

    embeddedServer.stop();
  }
}

代码示例来源:origin: micronaut-projects/micronaut-core

@Test
public void testSimpleRetrieve() {
  EmbeddedServer embeddedServer = ApplicationContext.run(EmbeddedServer.class);
  RxHttpClient client = embeddedServer.getApplicationContext().createBean(RxHttpClient.class, embeddedServer.getURL());
  // tag::simple[]
  String uri = UriBuilder.of("/hello/{name}")
              .expand(Collections.singletonMap("name", "John"))
              .toString();
  assertEquals("/hello/John", uri);
  String result = client.toBlocking().retrieve(uri);
  assertEquals(
      "Hello John",
      result
  );
  // end::simple[]
  embeddedServer.stop();
  client.stop();
}

代码示例来源:origin: micronaut-projects/micronaut-core

@Test
  public void testGetPojoList() {
    ApplicationContext applicationContext = ApplicationContext.run();
    EmbeddedServer server = applicationContext.getBean(EmbeddedServer.class).start();

    HttpClient client = applicationContext.createBean(HttpClient.class, server.getURL());

    Flowable<HttpResponse<List>> flowable = Flowable.fromPublisher(client.exchange(
        HttpRequest.GET("/get/pojoList"), Argument.of(List.class, HttpGetSpec.Book.class)
    ));
    HttpResponse<List> response = flowable.blockingFirst();

    assertEquals(response.getStatus(), HttpStatus.OK);
    Optional<List> body = response.getBody();
    assertTrue(body.isPresent());

    List<HttpGetSpec.Book> list = body.get();
    assertEquals(list.size(), 1);
    assertTrue(list.get(0) instanceof HttpGetSpec.Book);

    client.stop();
    applicationContext.stop();
  }
}

代码示例来源:origin: micronaut-projects/micronaut-core

@Test
public void testSimpleGet() {
  ApplicationContext applicationContext = ApplicationContext.run();
  EmbeddedServer server = applicationContext.getBean(EmbeddedServer.class).start();
  HttpClient client = new DefaultHttpClient(server.getURL());
  Flowable<HttpResponse<String>> flowable = Flowable.fromPublisher(client.exchange(
      HttpRequest.GET("/get/simple"), String.class
  ));
  HttpResponse<String> response = flowable.blockingFirst();
  Assert.assertEquals(response.getStatus(), HttpStatus.OK);
  Optional<String> body = response.getBody(String.class);
  assertTrue(body.isPresent());
  assertEquals(body.get(), "success");
  client.stop();
  applicationContext.stop();
}

代码示例来源:origin: micronaut-projects/micronaut-core

@Test
public void testPostPet() {
  EmbeddedServer embeddedServer = ApplicationContext.run(EmbeddedServer.class);
  PetClient client = embeddedServer.getApplicationContext().getBean(PetClient.class);
  // tag::post[]
  Pet pet = client.save("Dino", 10).blockingGet();
  assertEquals("Dino", pet.getName());
  assertEquals(10, pet.getAge());
  // end::post[]
  embeddedServer.stop();
}

代码示例来源:origin: micronaut-projects/micronaut-core

@Test
public void testRetrieveWithJSON() {
  EmbeddedServer embeddedServer = ApplicationContext.run(EmbeddedServer.class);
  RxHttpClient client = embeddedServer.getApplicationContext().createBean(RxHttpClient.class, embeddedServer.getURL());
  // tag::jsonmap[]
  Flowable<Map> response = client.retrieve(
      GET("/greet/John"), Map.class
  );
  // end::jsonmap[]
  assertEquals(
      "Hello John",
      response.blockingFirst().get("text")
  );
  // tag::jsonmaptypes[]
  response = client.retrieve(
      GET("/greet/John"),
      Argument.of(Map.class, String.class, String.class) // <1>
  );
  // end::jsonmaptypes[]
  assertEquals(
      "Hello John",
      response.blockingFirst().get("text")
  );
  embeddedServer.stop();
  client.stop();
}

代码示例来源:origin: micronaut-projects/micronaut-core

@Test
public void testRetrieveWithPOJOResponse() {
  EmbeddedServer embeddedServer = ApplicationContext.run(EmbeddedServer.class);
  RxHttpClient client = embeddedServer.getApplicationContext().createBean(RxHttpClient.class, embeddedServer.getURL());
  // tag::pojoresponse[]
  Flowable<HttpResponse<Message>> call = client.exchange(
      GET("/greet/John"), Message.class // <1>
  );
  HttpResponse<Message> response = call.blockingFirst();
  Optional<Message> message = response.getBody(Message.class); // <2>
  // check the status
  assertEquals(
      HttpStatus.OK,
      response.getStatus() // <3>
  );
  // check the body
  assertTrue(message.isPresent());
  assertEquals(
      "Hello John",
      message.get().getText()
  );
  // end::pojoresponse[]
  embeddedServer.stop();
  client.stop();
}

代码示例来源:origin: micronaut-projects/micronaut-core

@Test
public void testPostWithURITemplate() {
  EmbeddedServer embeddedServer = ApplicationContext.run(EmbeddedServer.class);
  RxHttpClient client = embeddedServer.getApplicationContext().createBean(RxHttpClient.class, embeddedServer.getURL());
  // tag::posturitemplate[]
  Flowable<HttpResponse<Book>> call = client.exchange(
      POST("/amazon/book/{title}", new Book("The Stand")),
      Book.class
  );
  // end::posturitemplate[]
  HttpResponse<Book> response = call.blockingFirst();
  Optional<Book> message = response.getBody(Book.class); // <2>
  // check the status
  assertEquals(
      HttpStatus.CREATED,
      response.getStatus() // <3>
  );
  // check the body
  assertTrue(message.isPresent());
  assertEquals(
      "The Stand",
      message.get().getTitle()
  );
  embeddedServer.stop();
  client.stop();
}

代码示例来源:origin: micronaut-projects/micronaut-core

@Test
public void testPostRequestWithPOJO() {
  EmbeddedServer embeddedServer = ApplicationContext.run(EmbeddedServer.class);
  RxHttpClient client = embeddedServer.getApplicationContext().createBean(RxHttpClient.class, embeddedServer.getURL());

代码示例来源:origin: micronaut-projects/micronaut-core

@Test
public void testPostRequestWithString() {
  EmbeddedServer embeddedServer = ApplicationContext.run(EmbeddedServer.class);
  RxHttpClient client = embeddedServer.getApplicationContext().createBean(RxHttpClient.class, embeddedServer.getURL());
  // tag::poststring[]
  Flowable<HttpResponse<String>> call = client.exchange(
      POST("/hello", "Hello John") // <1>
        .contentType(MediaType.TEXT_PLAIN_TYPE)
        .accept(MediaType.TEXT_PLAIN_TYPE), // <2>
      String.class // <3>
  );
  // end::poststring[]
  HttpResponse<String> response = call.blockingFirst();
  Optional<String> message = response.getBody(String.class); // <2>
  // check the status
  assertEquals(
      HttpStatus.CREATED,
      response.getStatus() // <3>
  );
  // check the body
  assertTrue(message.isPresent());
  assertEquals(
      "Hello John",
      message.get()
  );
  embeddedServer.stop();
  client.stop();
}

代码示例来源:origin: micronaut-projects/micronaut-core

@Test
public void testRetrieveWithPOJO() {
  EmbeddedServer embeddedServer = ApplicationContext.run(EmbeddedServer.class);
  RxHttpClient client = embeddedServer.getApplicationContext().createBean(RxHttpClient.class, embeddedServer.getURL());
  // tag::jsonpojo[]
  Flowable<Message> response = client.retrieve(
      GET("/greet/John"), Message.class
  );
  assertEquals(
      "Hello John",
      response.blockingFirst().getText()
  );
  // end::jsonpojo[]
  embeddedServer.stop();
  client.stop();
}

代码示例来源:origin: micronaut-projects/micronaut-core

@Test
public void testPostFormData() {
  EmbeddedServer embeddedServer = ApplicationContext.run(EmbeddedServer.class);
  RxHttpClient client = embeddedServer.getApplicationContext().createBean(RxHttpClient.class, embeddedServer.getURL());

代码示例来源:origin: micronaut-projects/micronaut-core

@Test
public void testRetrieveWithHeaders() {
  EmbeddedServer embeddedServer = ApplicationContext.run(EmbeddedServer.class);
  RxHttpClient client = embeddedServer.getApplicationContext().createBean(RxHttpClient.class, embeddedServer.getURL());
  // tag::headers[]
  Flowable<String> response = client.retrieve(
      GET("/hello/John")
      .header("X-My-Header", "SomeValue")
  );
  // end::headers[]
  assertEquals(
      "Hello John",
      response.blockingFirst()
  );
  embeddedServer.stop();
  client.stop();
}

相关文章