Example usage for org.springframework.context.annotation AnnotationConfigApplicationContext getBean

List of usage examples for org.springframework.context.annotation AnnotationConfigApplicationContext getBean

Introduction

In this page you can find the example usage for org.springframework.context.annotation AnnotationConfigApplicationContext getBean.

Prototype

@Override
    public Object getBean(String name) throws BeansException 

Source Link

Usage

From source file:org.jasypt.spring31.annotation.EncryptablePropertySourcePostProcessorTest.java

/**
 * Tests the LIFO behavior of @EncryptablePropertySource annotaitons. The
 * last one registered should 'win'.//from   w w  w .  j a  v  a 2  s  . co m
 */
@Test
public void orderingIsLifo() {
    {
        AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
        ctx.register(ConfigWithImplicitName.class, P2Config.class);
        ctx.refresh();
        // p2 should 'win' as it was registered last
        assertThat(ctx.getBean(TestBean.class).getName(), equalTo("p2TestBean"));
        ctx.close();
    }

    {
        AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
        ctx.register(P2Config.class, ConfigWithImplicitName.class);
        ctx.refresh();
        // p1 should 'win' as it was registered last
        assertThat(ctx.getBean(TestBean.class).getName(), equalTo("p1TestBean"));
        ctx.close();
    }
}

From source file:org.jasypt.spring31.annotation.EncryptablePropertySourcePostProcessorTest.java

@Test
public void withResolvablePlaceholderAndFactoryBean() {
    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
    ctx.register(ConfigWithResolvablePlaceholderAndFactoryBean.class);
    System.setProperty("path.to.properties", "org/jasypt/spring31/annotation");
    ctx.refresh();/* w  w  w.  j a  v a 2 s  .co m*/
    assertThat(ctx.getBean(TestBean.class).getName(), equalTo("p1TestBean"));
    System.clearProperty("path.to.properties");
    ctx.close();

}

From source file:org.jasypt.spring31.annotation.EncryptablePropertySourcePostProcessorTest.java

@Test
public void withExplicitName() {
    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
    ctx.register(ConfigWithExplicitName.class);
    ctx.refresh();// w  ww  . java  2s  .com
    assertTrue("property source p1 was not added", ctx.getEnvironment().getPropertySources().contains("p1"));
    assertThat(ctx.getBean(TestBean.class).getName(), equalTo("p1TestBean"));

    // assert that the property source was added last to the set of sources
    String name;
    MutablePropertySources sources = ctx.getEnvironment().getPropertySources();
    Iterator<org.springframework.core.env.PropertySource<?>> iterator = sources.iterator();
    do {
        name = iterator.next().getName();
    } while (iterator.hasNext());

    assertThat(name, is("p1"));
    ctx.close();
}

From source file:org.smartfrog.services.anubis.Node.java

public Node(AnnotationConfigApplicationContext context, int c, CountDownLatch launchLatch,
        CountDownLatch startLatch, CountDownLatch endLatch, int maxSleep, int messageCount) throws Exception {
    this.context = context;
    cardinality = c;/*  ww w . jav  a2s  . c o  m*/
    this.launchLatch = launchLatch;
    this.startLatch = startLatch;
    this.endLatch = endLatch;
    this.maxSleep = maxSleep;
    messagesToSend = messageCount;
    for (int i = 0; i < cardinality; i++) {
        receiveHistory.put(i, new CopyOnWriteArrayList<ValueHistory>());
        msgReceivedLatches.put(i, new CountDownLatch(messageCount));
    }
    identity = context.getBean(Identity.class).id;
    partition = context.getBean(Partition.class);
    partition.register(new PartitionNotification() {

        @Override
        public void objectNotification(Object obj, int sender, long time) {
            receiveHistory.get(sender).add(new ValueHistory(sender, obj, time, Action.NEW));
            msgReceivedLatches.get(sender).countDown();
        }

        @Override
        public void partitionNotification(View view, int leader) {
            Node.this.view = view;
            if (view.isStable() && view.cardinality() == cardinality) {
                // System.out.println("Launching: " + view + " : " + instance);
                Node.this.launchLatch.countDown();
            } else {
                // System.out.println("Not launching: " + view + " : " + instance);
            }
        }
    });
}

From source file:org.jasypt.spring31.annotation.EncryptablePropertySourcePostProcessorTest.java

@Test
public void withImplicitName() {
    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
    ctx.register(ConfigWithImplicitName.class);
    ctx.refresh();/*from  ww w  .  j  a  v  a2  s.  c  o  m*/
    assertTrue("property source p1 was not added", ctx.getEnvironment().getPropertySources()
            .contains("class path resource [org/jasypt/spring31/annotation/basic-encrypted-p1.properties]"));
    assertThat(ctx.getBean(TestBean.class).getName(), equalTo("p1TestBean"));
    ctx.close();
}

From source file:org.jacpfx.vertx.spring.SpringVerticleFactory.java

private Verticle createSpringVerticle(final Class<?> currentVerticleClass, ClassLoader classLoader) {
    final SpringVerticle annotation = currentVerticleClass.getAnnotation(SpringVerticle.class);
    final Class<?> springConfigClass = annotation.springConfig();

    // Create the parent context  
    final GenericApplicationContext genericApplicationContext = new GenericApplicationContext();
    genericApplicationContext.setClassLoader(classLoader);
    if (parentContext != null) {
        genericApplicationContext.setParent(parentContext);
    }//from www.  j a v  a2s  . c o  m
    genericApplicationContext.refresh();
    genericApplicationContext.start();

    // 1. Create a new context for each verticle and use the specified spring configuration class if possible
    AnnotationConfigApplicationContext annotationConfigApplicationContext = new AnnotationConfigApplicationContext();
    annotationConfigApplicationContext.setParent(genericApplicationContext);
    annotationConfigApplicationContext.register(SpringContextConfiguration.class, springConfigClass);

    // 2. Register a bean definition for this verticle
    annotationConfigApplicationContext.registerBeanDefinition(currentVerticleClass.getSimpleName(),
            new VerticleBeanDefinition(currentVerticleClass));

    // 3. Add a bean factory post processor to avoid configuration issues
    annotationConfigApplicationContext
            .addBeanFactoryPostProcessor(new SpringSingleVerticleConfiguration(currentVerticleClass));
    annotationConfigApplicationContext.refresh();
    annotationConfigApplicationContext.start();
    annotationConfigApplicationContext.registerShutdownHook();

    // 5. Return the verticle by fetching the bean from the context
    return (Verticle) annotationConfigApplicationContext.getBeanFactory()
            .getBean(currentVerticleClass.getSimpleName());
}

From source file:org.springframework.boot.actuate.autoconfigure.metrics.PublicMetricsAutoConfigurationTests.java

@Test
public void richGaugePublicMetrics() {
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
            RichGaugeReaderConfig.class, MetricRepositoryAutoConfiguration.class,
            PublicMetricsAutoConfiguration.class);
    RichGaugeReader richGaugeReader = context.getBean(RichGaugeReader.class);
    assertThat(richGaugeReader).isNotNull();
    given(richGaugeReader.findAll()).willReturn(Collections.singletonList(new RichGauge("bar", 3.7d)));
    RichGaugeReaderPublicMetrics publicMetrics = context.getBean(RichGaugeReaderPublicMetrics.class);
    assertThat(publicMetrics).isNotNull();
    Collection<Metric<?>> metrics = publicMetrics.metrics();
    assertThat(metrics).isNotNull();//  w  w  w  . j  av  a2  s  . c om
    assertThat(6).isEqualTo(metrics.size());
    assertHasMetric(metrics, new Metric<>("bar.val", 3.7d));
    assertHasMetric(metrics, new Metric<>("bar.avg", 3.7d));
    assertHasMetric(metrics, new Metric<>("bar.min", 3.7d));
    assertHasMetric(metrics, new Metric<>("bar.max", 3.7d));
    assertHasMetric(metrics, new Metric<>("bar.alpha", -1.d));
    assertHasMetric(metrics, new Metric<>("bar.count", 1L));
    context.close();
}

From source file:org.springframework.boot.actuate.autoconfigure.PublicMetricsAutoConfigurationTests.java

@Test
public void richGaugePublicMetrics() {
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
            RichGaugeReaderConfig.class, MetricRepositoryAutoConfiguration.class,
            PublicMetricsAutoConfiguration.class);
    RichGaugeReader richGaugeReader = context.getBean(RichGaugeReader.class);
    assertNotNull(richGaugeReader);/*from   w  w  w  .ja va2 s  . c o m*/
    given(richGaugeReader.findAll()).willReturn(Collections.singletonList(new RichGauge("bar", 3.7d)));
    RichGaugeReaderPublicMetrics publicMetrics = context.getBean(RichGaugeReaderPublicMetrics.class);
    assertNotNull(publicMetrics);
    Collection<Metric<?>> metrics = publicMetrics.metrics();
    assertNotNull(metrics);
    assertEquals(metrics.size(), 6);
    assertHasMetric(metrics, new Metric<Double>("bar.val", 3.7d));
    assertHasMetric(metrics, new Metric<Double>("bar.avg", 3.7d));
    assertHasMetric(metrics, new Metric<Double>("bar.min", 3.7d));
    assertHasMetric(metrics, new Metric<Double>("bar.max", 3.7d));
    assertHasMetric(metrics, new Metric<Double>("bar.alpha", -1.d));
    assertHasMetric(metrics, new Metric<Long>("bar.count", 1L));
    context.close();
}

From source file:org.springframework.boot.autoconfigure.AutoConfigurationReportLoggingInitializerTests.java

@Test
public void canBeUsedInApplicationContext() throws Exception {
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
    context.register(Config.class);
    new AutoConfigurationReportLoggingInitializer().initialize(context);
    context.refresh();/*w w w.j  a va2 s .  c o m*/
    assertNotNull(context.getBean(AutoConfigurationReport.class));
}

From source file:org.springframework.boot.autoconfigure.logging.AutoConfigurationReportLoggingInitializerTests.java

@Test
public void canBeUsedInApplicationContext() throws Exception {
    AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
    context.register(Config.class);
    new AutoConfigurationReportLoggingInitializer().initialize(context);
    context.refresh();/*from w w  w.  j a  va2  s. c om*/
    assertNotNull(context.getBean(ConditionEvaluationReport.class));
}