Example usage for org.springframework.context.support GenericApplicationContext getBean

List of usage examples for org.springframework.context.support GenericApplicationContext getBean

Introduction

In this page you can find the example usage for org.springframework.context.support GenericApplicationContext getBean.

Prototype

@Override
    public Object getBean(String name) throws BeansException 

Source Link

Usage

From source file:org.springframework.aop.aspectj.autoproxy.AspectJAutoProxyCreatorTests.java

@Test
public void testAspectsAndAdvisorAreAppliedEvenIfComingFromParentFactory() {
    ClassPathXmlApplicationContext ac = newContext("aspectsPlusAdvisor.xml");
    GenericApplicationContext childAc = new GenericApplicationContext(ac);
    // Create a child factory with a bean that should be woven
    RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
    bd.getPropertyValues().addPropertyValue(new PropertyValue("name", "Adrian"))
            .addPropertyValue(new PropertyValue("age", new Integer(34)));
    childAc.registerBeanDefinition("adrian2", bd);
    // Register the advisor auto proxy creator with subclass
    childAc.registerBeanDefinition(AnnotationAwareAspectJAutoProxyCreator.class.getName(),
            new RootBeanDefinition(AnnotationAwareAspectJAutoProxyCreator.class));
    childAc.refresh();//from  w  ww .  j a va2 s .com

    ITestBean beanFromChildContextThatShouldBeWeaved = (ITestBean) childAc.getBean("adrian2");
    //testAspectsAndAdvisorAreApplied(childAc, (ITestBean) ac.getBean("adrian"));
    doTestAspectsAndAdvisorAreApplied(childAc, beanFromChildContextThatShouldBeWeaved);
}

From source file:org.springframework.cloud.function.web.function.FunctionEndpointInitializer.java

private void registerEndpoint(GenericApplicationContext context) {
    context.registerBean(StringConverter.class,
            () -> new BasicStringConverter(context.getBean(FunctionInspector.class), context.getBeanFactory()));
    context.registerBean(RequestProcessor.class,
            () -> new RequestProcessor(context.getBean(FunctionInspector.class),
                    context.getBeanProvider(JsonMapper.class), context.getBean(StringConverter.class),
                    context.getBeanProvider(ServerCodecConfigurer.class)));
    context.registerBean(FunctionEndpointFactory.class,
            () -> new FunctionEndpointFactory(context.getBean(FunctionCatalog.class),
                    context.getBean(FunctionInspector.class), context.getBean(RequestProcessor.class),
                    context.getEnvironment()));
    context.registerBean(RouterFunction.class,
            () -> context.getBean(FunctionEndpointFactory.class).functionEndpoints());
}

From source file:org.springframework.cloud.function.web.function.FunctionEndpointInitializer.java

private HttpWebHandlerAdapter httpHandler(GenericApplicationContext context) {
    return (HttpWebHandlerAdapter) RouterFunctions.toHttpHandler(context.getBean(RouterFunction.class),
            HandlerStrategies.empty().exceptionHandler(context.getBean(WebExceptionHandler.class))
                    .codecs(config -> config.registerDefaults(true)).build());
}

From source file:org.springframework.cloud.function.web.function.FunctionEndpointInitializer.java

private DefaultErrorWebExceptionHandler errorHandler(GenericApplicationContext context) {
    context.registerBean(ErrorAttributes.class, () -> new DefaultErrorAttributes());
    context.registerBean(ErrorProperties.class, () -> new ErrorProperties());
    context.registerBean(ResourceProperties.class, () -> new ResourceProperties());
    DefaultErrorWebExceptionHandler handler = new DefaultErrorWebExceptionHandler(
            context.getBean(ErrorAttributes.class), context.getBean(ResourceProperties.class),
            context.getBean(ErrorProperties.class), context);
    ServerCodecConfigurer codecs = ServerCodecConfigurer.create();
    handler.setMessageWriters(codecs.getWriters());
    handler.setMessageReaders(codecs.getReaders());
    return handler;
}

From source file:org.springframework.context.annotation.AnnotationProcessorPerformanceTests.java

@Test
public void testPrototypeCreationWithResourcePropertiesIsFastEnough() {
    GenericApplicationContext ctx = new GenericApplicationContext();
    AnnotationConfigUtils.registerAnnotationConfigProcessors(ctx);
    ctx.refresh();//  w ww  . j ava  2  s . c  o  m

    RootBeanDefinition rbd = new RootBeanDefinition(ResourceAnnotatedTestBean.class);
    rbd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
    ctx.registerBeanDefinition("test", rbd);
    ctx.registerBeanDefinition("spouse", new RootBeanDefinition(TestBean.class));
    TestBean spouse = (TestBean) ctx.getBean("spouse");
    StopWatch sw = new StopWatch();
    sw.start("prototype");
    for (int i = 0; i < 100000; i++) {
        TestBean tb = (TestBean) ctx.getBean("test");
        assertSame(spouse, tb.getSpouse());
    }
    sw.stop();
    assertTrue("Prototype creation took too long: " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() < 4000);
}

From source file:org.springframework.context.annotation.AnnotationProcessorPerformanceTests.java

@Test
public void testPrototypeCreationWithOverriddenResourcePropertiesIsFastEnough() {
    GenericApplicationContext ctx = new GenericApplicationContext();
    AnnotationConfigUtils.registerAnnotationConfigProcessors(ctx);
    ctx.refresh();/*from  www.  jav a 2  s . c  om*/

    RootBeanDefinition rbd = new RootBeanDefinition(ResourceAnnotatedTestBean.class);
    rbd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
    rbd.getPropertyValues().add("spouse", new RuntimeBeanReference("spouse"));
    ctx.registerBeanDefinition("test", rbd);
    ctx.registerBeanDefinition("spouse", new RootBeanDefinition(TestBean.class));
    TestBean spouse = (TestBean) ctx.getBean("spouse");
    StopWatch sw = new StopWatch();
    sw.start("prototype");
    for (int i = 0; i < 100000; i++) {
        TestBean tb = (TestBean) ctx.getBean("test");
        assertSame(spouse, tb.getSpouse());
    }
    sw.stop();
    assertTrue("Prototype creation took too long: " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() < 4000);
}

From source file:org.springframework.context.annotation.AnnotationProcessorPerformanceTests.java

@Test
public void testPrototypeCreationWithAutowiredPropertiesIsFastEnough() {
    GenericApplicationContext ctx = new GenericApplicationContext();
    AnnotationConfigUtils.registerAnnotationConfigProcessors(ctx);
    ctx.refresh();//from   w  ww  . java 2s .  com

    RootBeanDefinition rbd = new RootBeanDefinition(AutowiredAnnotatedTestBean.class);
    rbd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
    ctx.registerBeanDefinition("test", rbd);
    ctx.registerBeanDefinition("spouse", new RootBeanDefinition(TestBean.class));
    TestBean spouse = (TestBean) ctx.getBean("spouse");
    StopWatch sw = new StopWatch();
    sw.start("prototype");
    for (int i = 0; i < 100000; i++) {
        TestBean tb = (TestBean) ctx.getBean("test");
        assertSame(spouse, tb.getSpouse());
    }
    sw.stop();
    assertTrue("Prototype creation took too long: " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() < 4000);
}

From source file:org.springframework.context.annotation.AnnotationProcessorPerformanceTests.java

@Test
public void testPrototypeCreationWithOverriddenAutowiredPropertiesIsFastEnough() {
    GenericApplicationContext ctx = new GenericApplicationContext();
    AnnotationConfigUtils.registerAnnotationConfigProcessors(ctx);
    ctx.refresh();//from   w  w w . j  av  a 2s . com

    RootBeanDefinition rbd = new RootBeanDefinition(AutowiredAnnotatedTestBean.class);
    rbd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
    rbd.getPropertyValues().add("spouse", new RuntimeBeanReference("spouse"));
    ctx.registerBeanDefinition("test", rbd);
    ctx.registerBeanDefinition("spouse", new RootBeanDefinition(TestBean.class));
    TestBean spouse = (TestBean) ctx.getBean("spouse");
    StopWatch sw = new StopWatch();
    sw.start("prototype");
    for (int i = 0; i < 100000; i++) {
        TestBean tb = (TestBean) ctx.getBean("test");
        assertSame(spouse, tb.getSpouse());
    }
    sw.stop();
    assertTrue("Prototype creation took too long: " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() < 6000);
}

From source file:org.springframework.context.expression.ApplicationContextExpressionTests.java

@Test
public void prototypeCreationReevaluatesExpressions() {
    GenericApplicationContext ac = new GenericApplicationContext();
    AnnotationConfigUtils.registerAnnotationConfigProcessors(ac);
    GenericConversionService cs = new GenericConversionService();
    cs.addConverter(String.class, String.class, new Converter<String, String>() {
        @Override//from  w  w  w.j  av a  2s . c  om
        public String convert(String source) {
            return source.trim();
        }
    });
    ac.getBeanFactory().registerSingleton(GenericApplicationContext.CONVERSION_SERVICE_BEAN_NAME, cs);
    RootBeanDefinition rbd = new RootBeanDefinition(PrototypeTestBean.class);
    rbd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
    rbd.getPropertyValues().add("country", "#{systemProperties.country}");
    rbd.getPropertyValues().add("country2", new TypedStringValue("-#{systemProperties.country}-"));
    ac.registerBeanDefinition("test", rbd);
    ac.refresh();

    try {
        System.getProperties().put("name", "juergen1");
        System.getProperties().put("country", " UK1 ");
        PrototypeTestBean tb = (PrototypeTestBean) ac.getBean("test");
        assertEquals("juergen1", tb.getName());
        assertEquals("UK1", tb.getCountry());
        assertEquals("-UK1-", tb.getCountry2());

        System.getProperties().put("name", "juergen2");
        System.getProperties().put("country", "  UK2  ");
        tb = (PrototypeTestBean) ac.getBean("test");
        assertEquals("juergen2", tb.getName());
        assertEquals("UK2", tb.getCountry());
        assertEquals("-UK2-", tb.getCountry2());
    } finally {
        System.getProperties().remove("name");
        System.getProperties().remove("country");
    }
}

From source file:org.springframework.context.expression.ApplicationContextExpressionTests.java

@Test
public void prototypeCreationIsFastEnough() {
    Assume.group(TestGroup.PERFORMANCE);
    Assume.notLogging(factoryLog);/*from  w  w w .j ava  2s  .  co  m*/
    GenericApplicationContext ac = new GenericApplicationContext();
    RootBeanDefinition rbd = new RootBeanDefinition(TestBean.class);
    rbd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
    rbd.getConstructorArgumentValues().addGenericArgumentValue("#{systemProperties.name}");
    rbd.getPropertyValues().add("country", "#{systemProperties.country}");
    ac.registerBeanDefinition("test", rbd);
    ac.refresh();
    StopWatch sw = new StopWatch();
    sw.start("prototype");
    System.getProperties().put("name", "juergen");
    System.getProperties().put("country", "UK");
    try {
        for (int i = 0; i < 100000; i++) {
            TestBean tb = (TestBean) ac.getBean("test");
            assertEquals("juergen", tb.getName());
            assertEquals("UK", tb.getCountry());
        }
        sw.stop();
    } finally {
        System.getProperties().remove("country");
        System.getProperties().remove("name");
    }
    assertTrue("Prototype creation took too long: " + sw.getTotalTimeMillis(), sw.getTotalTimeMillis() < 6000);
}