private void assertEnvironmentBeanRegistered( ConfigurableApplicationContext ctx) { // ensure environment is registered as a bean assertThat(ctx.containsBean(ENVIRONMENT_BEAN_NAME), is(true)); }
@Test public void asyncAspectRegistered() { assertTrue(context.containsBean(TaskManagementConfigUtils.ASYNC_EXECUTION_ASPECT_BEAN_NAME)); }
@Test public void classPathXmlApplicationContext() { ConfigurableApplicationContext ctx = new ClassPathXmlApplicationContext(XML_PATH); ctx.setEnvironment(prodEnv); ctx.refresh(); assertEnvironmentBeanRegistered(ctx); assertHasEnvironment(ctx, prodEnv); assertEnvironmentAwareInvoked(ctx, ctx.getEnvironment()); assertThat(ctx.containsBean(DEV_BEAN_NAME), is(false)); assertThat(ctx.containsBean(PROD_BEAN_NAME), is(true)); }
@Test public void transactionalEventListenerRegisteredProperly() { assertTrue(this.context.containsBean(TransactionManagementConfigUtils .TRANSACTIONAL_EVENT_LISTENER_FACTORY_BEAN_NAME)); assertEquals(1, this.context.getBeansOfType(TransactionalEventListenerFactory.class).size()); }
@Test public void fileSystemXmlApplicationContext() throws IOException { ClassPathResource xml = new ClassPathResource(XML_PATH); File tmpFile = File.createTempFile("test", "xml"); FileCopyUtils.copy(xml.getFile(), tmpFile); // strange - FSXAC strips leading '/' unless prefixed with 'file:' ConfigurableApplicationContext ctx = new FileSystemXmlApplicationContext(new String[] {"file:" + tmpFile.getPath()}, false); ctx.setEnvironment(prodEnv); ctx.refresh(); assertEnvironmentBeanRegistered(ctx); assertHasEnvironment(ctx, prodEnv); assertEnvironmentAwareInvoked(ctx, ctx.getEnvironment()); assertThat(ctx.containsBean(DEV_BEAN_NAME), is(false)); assertThat(ctx.containsBean(PROD_BEAN_NAME), is(true)); }
@Test public void loadConfigWhenMultipleWebSecurityConfigurationThenContextLoads() throws Exception { this.spring.register(ParentConfig.class).autowire(); this.child.register(ChildConfig.class); this.child.getContext().setParent(this.spring.getContext()); this.child.autowire(); assertThat(this.spring.getContext().getBean("springSecurityFilterChain")).isNotNull(); assertThat(this.child.getContext().getBean("springSecurityFilterChain")).isNotNull(); assertThat(this.spring.getContext().containsBean("springSecurityFilterChain")).isTrue(); assertThat(this.child.getContext().containsBean("springSecurityFilterChain")).isTrue(); }
@Test public void testCustomizeContext() { // given ConfigurableApplicationContext context = new GenericApplicationContext(); MergedContextConfiguration mergedConfig = Mockito.mock(MergedContextConfiguration.class); // when this.contextCustomizer.customizeContext(context, mergedConfig); // then Assert.assertTrue(context.containsBean("jobLauncherTestUtils")); Assert.assertTrue(context.containsBean("jobRepositoryTestUtils")); }
@Test public void componentScanRespectsProfileAnnotation() { String xmlLocation = "org/springframework/context/annotation/componentScanRespectsProfileAnnotationTests.xml"; { // should exclude the profile-annotated bean if active profiles remains unset GenericXmlApplicationContext context = new GenericXmlApplicationContext(); context.load(xmlLocation); context.refresh(); assertThat(context.containsBean(ProfileAnnotatedComponent.BEAN_NAME), is(false)); context.close(); } { // should include the profile-annotated bean with active profiles set GenericXmlApplicationContext context = new GenericXmlApplicationContext(); context.getEnvironment().setActiveProfiles(ProfileAnnotatedComponent.PROFILE_NAME); context.load(xmlLocation); context.refresh(); assertThat(context.containsBean(ProfileAnnotatedComponent.BEAN_NAME), is(true)); context.close(); } { // ensure the same works for AbstractRefreshableApplicationContext impls too ConfigurableApplicationContext context = new ClassPathXmlApplicationContext(new String[] { xmlLocation }, false); context.getEnvironment().setActiveProfiles(ProfileAnnotatedComponent.PROFILE_NAME); context.refresh(); assertThat(context.containsBean(ProfileAnnotatedComponent.BEAN_NAME), is(true)); context.close(); } }
private void doRegisterObject(String key, Object value) throws RegistrationException { if (springContextInitialised.get()) { if (applicationContext.containsBean(key)) { if (logger.isWarnEnabled()) { logger.warn( format("Spring registry already contains an object named '%s'. The previous object will be overwritten.", key)); } SpringRegistry.this.unregisterObject(key); } try { value = initialiseObject(applicationContext, key, value); applyLifecycle(value); applicationContext.getBeanFactory().registerSingleton(key, value); } catch (Exception e) { throw new RegistrationException(createStaticMessage("Could not register object for key " + key), e); } } else { // since the context has not yet bean initialized, we register a bean definition instead. registeredBeanDefinitionsBeforeInitialization.put(key, genericBeanDefinition(ConstantFactoryBean.class) .addConstructorArgValue(value).getBeanDefinition()); } } }
if (StringUtils.hasText(endpoint.getGroup()) && this.applicationContext != null) { List<MessageListenerContainer> containerGroup; if (this.applicationContext.containsBean(endpoint.getGroup())) { containerGroup = this.applicationContext.getBean(endpoint.getGroup(), List.class);
@Test public void testMapPayloadOutboundChannelAdapter() { setUp("handlingMapPayloadJdbcOutboundChannelAdapterTest.xml", getClass()); assertTrue(context.containsBean("jdbcAdapter")); Message<?> message = MessageBuilder.withPayload(Collections.singletonMap("foo", "bar")).build(); channel.send(message); Map<String, Object> map = this.jdbcTemplate.queryForMap("SELECT * from FOOS"); assertEquals("Wrong id", message.getHeaders().getId().toString(), map.get("ID")); assertEquals("Wrong name", "bar", map.get("name")); }
/** * @see IPentahoObjectFactory#objectDefined(String) */ public boolean objectDefined( String key ) { return beanFactory.containsBean( key ); }
@Test public void testMapPayloadMapReply() { setUp("handlingMapPayloadJdbcOutboundGatewayTest.xml", getClass()); assertTrue(this.context.containsBean("jdbcGateway")); Message<?> message = MessageBuilder.withPayload(Collections.singletonMap("foo", "bar")).build(); this.channel.send(message); Message<?> reply = this.messagingTemplate.receive(); assertNotNull(reply); @SuppressWarnings("unchecked") Map<String, ?> payload = (Map<String, ?>) reply.getPayload(); assertEquals("bar", payload.get("name")); Map<String, Object> map = this.jdbcTemplate.queryForMap("SELECT * from FOOS"); assertEquals("Wrong id", message.getHeaders().getId().toString(), map.get("ID")); assertEquals("Wrong name", "bar", map.get("name")); JdbcOutboundGateway gateway = context.getBean("jdbcGateway.handler", JdbcOutboundGateway.class); assertEquals(23, TestUtils.getPropertyValue(gateway, "order")); Assert.assertTrue(TestUtils.getPropertyValue(gateway, "requiresReply", Boolean.class)); assertEquals(1, adviceCalled); }
/** * @param clazz Interface or class literal to search for * @return true if a definition exists */ @Override public boolean objectDefined( Class<?> clazz ) { return beanFactory.containsBean( clazz.getSimpleName() ); }
public ISessionService getSessionService() { try { if (remoteRepCtx.containsBean("AasSessionService")) { return (ISessionService) remoteRepCtx.getBean("AasSessionService"); } } catch (Exception e) { } return sessionService; }
@Override public boolean containsBean(String name) { lock.readLock().lock(); try { return configurableApplicationContext.containsBean(name); } finally { lock.readLock().unlock(); } }
public static void main(String[] args) { ConfigurableApplicationContext context = new SpringApplicationBuilder( AnnotationConditionalOnPropertyBootstrap.class) .web(WebApplicationType.NONE) // 非 Web 应用 .run(args); System.out.printf("Spring 应用上下文是否包含 enabledValue Bean : %b \n", context.containsBean("enabledValue")); //关闭 Spring 应用上下文 context.close(); }
@Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { this.applicationContext = (ConfigurableApplicationContext) applicationContext; GenericConversionService cs = (GenericConversionService) IntegrationUtils.getConversionService(this.applicationContext.getBeanFactory()); if (this.applicationContext.containsBean("spelConverter")) { Converter<?,?> converter = (Converter<?, ?>) this.applicationContext.getBean("spelConverter"); cs.addConverter(converter); } }
public Object getBean(String name) { if (!getApplicationContext().containsBean(name)) { return null; } return getApplicationContext().getBean(name); }
@Test public void testNoDataflowConfig() { SpringApplication app = new SpringApplication(LocalTestNoDataFlowServer.class); context = app.run(new String[] { "--spring.cloud.kubernetes.enabled=false", "--server.port=0", "--spring.jpa.database=H2", "--spring.flyway.enabled=false" }); assertThat(context.containsBean("appRegistry"), is(false)); } }