@Test public void withCustomTypeFilter() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ComponentScanWithCustomTypeFilter.class); CustomAnnotationAutowiredBean testBean = ctx.getBean(CustomAnnotationAutowiredBean.class); assertThat(testBean.getDependency(), notNullValue()); }
@Test public void withBasePackagesAndValueAlias() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(ComponentScanWithBasePackagesAndValueAlias.class); ctx.refresh(); assertThat(ctx.containsBean("fooServiceImpl"), is(true)); }
public void evenCircularScansAreSupported() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(LeftConfig.class); // left scans right, and right scans left ctx.refresh(); ctx.getBean("leftConfig"); // but this is handled gracefully ctx.getBean("rightConfig"); // and beans from both packages are available }
@Test public void enableLTW_withAjWeavingEnabled() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(EnableLTWConfig_withAjWeavingEnabled.class); ctx.refresh(); LoadTimeWeaver loadTimeWeaver = ctx.getBean("loadTimeWeaver", LoadTimeWeaver.class); verify(loadTimeWeaver).addTransformer(isA(ClassFileTransformer.class)); }
@Test public void withCustomBeanNameGenerator() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(ComponentScanWithBeanNameGenenerator.class); ctx.refresh(); assertThat(ctx.containsBean("custom_fooServiceImpl"), is(true)); assertThat(ctx.containsBean("fooServiceImpl"), is(false)); }
@Test public void enableLTW_withAjWeavingDisabled() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(EnableLTWConfig_withAjWeavingDisabled.class); ctx.refresh(); LoadTimeWeaver loadTimeWeaver = ctx.getBean("loadTimeWeaver", LoadTimeWeaver.class); verifyZeroInteractions(loadTimeWeaver); }
public static void main(String[] args) throws InterruptedException { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(Config.class); while (true) { context.getBean(ColorFrame.class).showOnRandomPlace(); Thread.sleep(1000); } }
@Test public void withMultipleAnnotationIncludeFilters2() throws IOException, ClassNotFoundException { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(ComponentScanWithMultipleAnnotationIncludeFilters2.class); ctx.refresh(); ctx.getBean(DefaultNamedComponent.class); // @CustomStereotype-annotated ctx.getBean(MessageBean.class); // @CustomComponent-annotated }
@Test public void autowiringSucceedsWithBFPPAsStaticMethod() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(TestBeanConfig.class, AutowiredConfigWithBFPPAsStaticMethod.class); ctx.refresh(); // static method BFPP does not interfere with lifecycle -> autowiring succeeds assertThat( ctx.getBean(AutowiredConfigWithBFPPAsStaticMethod.class).autowiredTestBean, notNullValue()); }
@Test public void staticBeanMethodsDoNotRespectScoping() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(ConfigWithStaticBeanMethod.class); ctx.refresh(); assertThat( ConfigWithStaticBeanMethod.testBean(), not(sameInstance(ConfigWithStaticBeanMethod.testBean()))); }
@Test public void enableLTW_withAjWeavingAutodetect() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(EnableLTWConfig_withAjWeavingAutodetect.class); ctx.refresh(); LoadTimeWeaver loadTimeWeaver = ctx.getBean("loadTimeWeaver", LoadTimeWeaver.class); // no expectations -> a class file transformer should NOT be added // because no META-INF/aop.xml is present on the classpath verifyZeroInteractions(loadTimeWeaver); }
@Test public void withScopeResolver() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(ComponentScanWithScopeResolver.class); // custom scope annotation makes the bean prototype scoped. subsequent calls // to getBean should return distinct instances. assertThat( ctx.getBean(CustomScopeAnnotationBean.class), not(sameInstance(ctx.getBean(CustomScopeAnnotationBean.class)))); }
@Test public void controlScan() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.scan(example.scannable._package.class.getPackage().getName()); ctx.refresh(); assertThat( "control scan for example.scannable package failed to register FooServiceImpl bean", ctx.containsBean("fooServiceImpl"), is(true)); }
@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 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 withScopedProxy() throws IOException, ClassNotFoundException { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(ComponentScanWithScopedProxy.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 serializability assertThat(bean.foo(1), equalTo("bar")); FooService deserialized = (FooService) SerializationTestUtils.serializeAndDeserialize(bean); assertThat(deserialized, notNullValue()); assertThat(deserialized.foo(1), equalTo("bar")); }
@Override public void start(Stage primaryStage) throws Exception { appContext = new AnnotationConfigApplicationContext(SmartCSV.class); String name = appContext.getEnvironment().getProperty("application.name"); String version = appContext.getEnvironment().getProperty("application.version"); Platform.setImplicitExit(false); AboutController aboutController = appContext.getBean(AboutController.class); aboutController.setHostServices(getHostServices()); try { showUI(primaryStage, name, version); } catch (Exception e) { e.printStackTrace(); } }
@Override public void stop() throws Exception { if (appContext != null) { appContext.close(); } super.stop(); }
@Test public void recursion() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(Level1Config.class); ctx.refresh(); // assert that all levels have been detected ctx.getBean(Level1Config.class); ctx.getBean(Level2Config.class); ctx.getBean(Level3Component.class); // assert that enhancement is working assertThat(ctx.getBean("level1Bean"), sameInstance(ctx.getBean("level1Bean"))); assertThat(ctx.getBean("level2Bean"), sameInstance(ctx.getBean("level2Bean"))); }
private void showUI(Stage primaryStage, String name, String version) { SmartCSVController smartCVSController = appContext.getBean(SmartCSVController.class); Scene scene = new Scene((Parent) smartCVSController.getView()); primaryStage.setScene(scene); primaryStage.setTitle(String.format("%s %s", name, version)); primaryStage.show(); primaryStage.setMaximized(true); primaryStage.setOnCloseRequest( event -> { if (!smartCVSController.canExit()) { event.consume(); } else { exit(); } }); }
@Test public void viaContextRegistration_WithValueAttribute() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(ComponentScanAnnotatedConfig_WithValueAttribute.class); ctx.refresh(); ctx.getBean(ComponentScanAnnotatedConfig_WithValueAttribute.class); ctx.getBean(TestBean.class); assertThat( "config class bean not found", ctx.containsBeanDefinition("componentScanAnnotatedConfig_WithValueAttribute"), is(true)); assertThat( "@ComponentScan annotated @Configuration class registered directly against " + "AnnotationConfigApplicationContext did not trigger component scanning as expected", ctx.containsBean("fooServiceImpl"), is(true)); }
@Test public void viaContextRegistration_WithLocallyDeclaredComposedAnnotation() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(ComposedAnnotationConfig.class); ctx.refresh(); ctx.getBean(ComposedAnnotationConfig.class); ctx.getBean(SimpleComponent.class); assertThat( "config class bean not found", ctx.containsBeanDefinition( "componentScanAnnotationIntegrationTests.ComposedAnnotationConfig"), is(true)); assertThat( "@ComponentScan annotated @Configuration class registered directly against " + "AnnotationConfigApplicationContext did not trigger component scanning as expected", ctx.containsBean("simpleComponent"), is(true)); }
@Test public void viaContextRegistration_FromPackageOfConfigClass() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(); ctx.register(ComponentScanAnnotatedConfigWithImplicitBasePackage.class); ctx.refresh(); ctx.getBean(ComponentScanAnnotatedConfigWithImplicitBasePackage.class); assertThat( "config class bean not found", ctx.containsBeanDefinition("componentScanAnnotatedConfigWithImplicitBasePackage"), is(true)); assertThat( "@ComponentScan annotated @Configuration class registered directly against " + "AnnotationConfigApplicationContext did not trigger component scanning as expected", ctx.containsBean("scannedComponent"), is(true)); }
@Test public void earlyProxyCreationAndBeanRegistrationLifecycle() { AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(FeatureConfig.class); // // see additional assertions in FeatureConfig#feature() // // sanity check that all the bean definitions we expecting are present assertThat(ctx.getBeanFactory().containsBeanDefinition("lazyHelperBean"), is(true)); assertThat(ctx.getBeanFactory().containsBeanDefinition("eagerHelperBean"), is(true)); assertThat(ctx.getBeanFactory().containsBeanDefinition("lazyPassthroughBean"), is(true)); assertThat(ctx.getBeanFactory().containsBeanDefinition("eagerPassthroughBean"), is(true)); // the lazy helper bean had methods invoked during feature method execution. it should be // registered assertThat(ctx.getBeanFactory().containsSingleton("lazyHelperBean"), is(true)); // the eager helper bean had methods invoked but should be registered in any case is it is // non-lazy assertThat(ctx.getBeanFactory().containsSingleton("eagerHelperBean"), is(true)); // the lazy passthrough bean was referenced in the feature method, but never invoked. it should // not be registered assertThat(ctx.getBeanFactory().containsSingleton("lazyPassthroughBean"), is(false)); // the eager passthrough bean should be registered in any case as it is non-lazy assertThat(ctx.getBeanFactory().containsSingleton("eagerPassthroughBean"), is(true)); // now actually fetch all the beans. none should be proxies assertThat(ctx.getBean("lazyHelperBean"), not(instanceOf(EarlyBeanReferenceProxy.class))); assertThat(ctx.getBean("eagerHelperBean"), not(instanceOf(EarlyBeanReferenceProxy.class))); assertThat(ctx.getBean("lazyPassthroughBean"), not(instanceOf(EarlyBeanReferenceProxy.class))); assertThat(ctx.getBean("eagerPassthroughBean"), not(instanceOf(EarlyBeanReferenceProxy.class))); }