@Override protected ApplicationContext initApplicationContext() { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); context.register(WebConfig.class); context.refresh(); return context; }
@Override protected ApplicationContext initApplicationContext() { AnnotationConfigApplicationContext wac = new AnnotationConfigApplicationContext(); wac.register(WebConfig.class); wac.refresh(); return wac; }
@Override protected ApplicationContext initApplicationContext() { AnnotationConfigApplicationContext wac = new AnnotationConfigApplicationContext(); wac.register(WebConfig.class, TestRestController.class); wac.refresh(); return wac; }
/** * Fails with classpath errors on trying to classload AnnotationAsyncExecutionAspect */ @Test(expected = BeanDefinitionStoreException.class) public void aspectModeAspectJAttemptsToRegisterAsyncAspect() { @SuppressWarnings("resource") AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(AspectJAsyncAnnotationConfig.class); ctx.refresh(); }
@Test public void autowiringFailsWithBFPPAsInstanceMethod() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(TestBeanConfig.class, AutowiredConfigWithBFPPAsInstanceMethod.class); ctx.refresh(); // instance method BFPP interferes with lifecycle -> autowiring fails! // WARN-level logging should have been issued about returning BFPP from non-static @Bean method assertThat(ctx.getBean(AutowiredConfigWithBFPPAsInstanceMethod.class).autowiredTestBean, nullValue()); }
@Test public void withResolvablePlaceholder() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(ConfigWithResolvablePlaceholder.class); System.setProperty("path.to.properties", "org/springframework/context/annotation"); ctx.refresh(); assertThat(ctx.getBean(TestBean.class).getName(), equalTo("p1TestBean")); System.clearProperty("path.to.properties"); }
@Test public void getBeansWithAnnotation() { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); context.register(Config.class, NameConfig.class, UntypedFactoryBean.class); context.refresh(); context.getBean("testBean"); context.getBean("name"); Map<String, Object> beans = context.getBeansWithAnnotation(Configuration.class); assertEquals(2, beans.size()); }
@Test public void withCustomFactoryAsMeta() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(ConfigWithImplicitName.class, WithCustomFactoryAsMeta.class); ctx.refresh(); assertThat(ctx.getBean(TestBean.class).getName(), equalTo("P2TESTBEAN")); }
@Test public void withUnresolvablePlaceholderAndDefault() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(ConfigWithUnresolvablePlaceholderAndDefault.class); ctx.refresh(); assertThat(ctx.getBean(TestBean.class).getName(), equalTo("p1TestBean")); }
@Before public void setup() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(TestConfig.class); ctx.refresh(); this.dispatcherHandler = new DispatcherHandler(ctx); }
@Test public void repositoryIsTxProxy_withCustomTxManagerName() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(Config.class, CustomTxManagerNameConfig.class); ctx.refresh(); assertTxProxying(ctx); }
@Test public void nonAnnotatedService_PTC_true() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(PTCTrue.class, AnnotatedServiceImpl.class); ctx.refresh(); NonAnnotatedService s = ctx.getBean(NonAnnotatedService.class); assertTrue("expected a subclass proxy", AopUtils.isCglibProxy(s)); assertThat(s, instanceOf(AnnotatedServiceImpl.class)); }
@Test @SuppressWarnings("resource") public void withConfigurationClassWithPlainFactoryBean() { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); context.register(ConfigWithPlainFactoryBean.class); context.refresh(); MyBean myBean = context.getBean("myBean", MyBean.class); assertSame(context.getBean("myService"), myBean.myService); myBean.myService.handle(); myBean.myService.handleAsync(); }
@Test public void withScopedProxyThroughAspectJPattern() throws IOException, ClassNotFoundException { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(ComponentScanWithScopedProxyThroughAspectJPattern.class); ctx.getBeanFactory().registerScope("myScope", new SimpleMapScope()); ctx.refresh(); // should cast to the interface FooService bean = (FooService) ctx.getBean("scopedProxyTestBean"); // should be dynamic proxy assertThat(AopUtils.isJdkDynamicProxy(bean), is(true)); }
@Test public void staticBeanMethodsDoNotRespectScoping() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(ConfigWithStaticBeanMethod.class); ctx.refresh(); assertThat(ConfigWithStaticBeanMethod.testBean(), not(sameInstance(ConfigWithStaticBeanMethod.testBean()))); }
@Test public void mostSpecificDerivedClassDrivesEnvironment_withDevEnvAndDerivedDevConfigClass() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.setEnvironment(devEnv); ctx.register(DerivedDevConfig.class); ctx.refresh(); assertThat("should not have dev bean", ctx.containsBean(DEV_BEAN_NAME), is(false)); assertThat("should not have derived dev bean", ctx.containsBean(DERIVED_DEV_BEAN_NAME), is(false)); assertThat("should not have transitive bean", ctx.containsBean(TRANSITIVE_BEAN_NAME), is(false)); }
private GroovyMarkupView createViewWithUrl(String viewUrl) throws Exception { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(GroovyMarkupConfiguration.class); ctx.refresh(); GroovyMarkupView view = new GroovyMarkupView(); view.setUrl(viewUrl); view.setApplicationContext(ctx); view.afterPropertiesSet(); return view; }
@Test public void annotationConfigApplicationContext_withDevEnvAndDevConfigClass() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); assertHasStandardEnvironment(ctx); ctx.setEnvironment(devEnv); ctx.register(DevConfig.class); ctx.refresh(); assertThat("should have dev bean", ctx.containsBean(DEV_BEAN_NAME), is(true)); assertThat("should have transitive bean", ctx.containsBean(TRANSITIVE_BEAN_NAME), is(true)); }
@Test public void annotationConfigApplicationContext_withImportedConfigClasses() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); assertHasStandardEnvironment(ctx); ctx.setEnvironment(prodEnv); ctx.register(Config.class); ctx.refresh(); assertEnvironmentAwareInvoked(ctx, prodEnv); assertThat("should have prod bean", ctx.containsBean(PROD_BEAN_NAME), is(true)); assertThat("should not have dev bean", ctx.containsBean(DEV_BEAN_NAME), is(false)); assertThat("should not have transitive bean", ctx.containsBean(TRANSITIVE_BEAN_NAME), is(false)); }
@Test public void beanMethodOverriding() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(OverridingConfig.class); ctx.setAllowBeanDefinitionOverriding(false); ctx.refresh(); assertFalse(ctx.getDefaultListableBeanFactory().containsSingleton("testBean")); assertEquals("overridden", ctx.getBean("testBean", TestBean.class).toString()); assertTrue(ctx.getDefaultListableBeanFactory().containsSingleton("testBean")); }