/** * register bean in spring ioc. * @param beanName bean name * @param obj bean */ public void registerBean(final String beanName, final Object obj) { cfgContext.getBeanFactory().registerSingleton(beanName, obj); }
/** * Refresh the given application context, if necessary. */ protected void refreshApplicationContext(ApplicationContext context) { if (context instanceof ConfigurableApplicationContext) { ConfigurableApplicationContext cac = (ConfigurableApplicationContext) context; if (!cac.isActive()) { cac.refresh(); } } }
static void registerApplicationContext(ConfigurableApplicationContext applicationContext) { String mbeanDomain = applicationContext.getEnvironment().getProperty(MBEAN_DOMAIN_PROPERTY_NAME); if (mbeanDomain != null) { synchronized (applicationContexts) { if (applicationContexts.isEmpty()) { try { MBeanServer server = ManagementFactory.getPlatformMBeanServer(); applicationName = applicationContext.getApplicationName(); server.registerMBean(new LiveBeansView(), new ObjectName(mbeanDomain, MBEAN_APPLICATION_KEY, applicationName)); } catch (Throwable ex) { throw new ApplicationContextException("Failed to register LiveBeansView MBean", ex); } } applicationContexts.add(applicationContext); } } }
private void registerGlueCodeScope(ConfigurableApplicationContext context) { do { context.getBeanFactory().registerScope(GlueCodeScope.NAME, new GlueCodeScope()); context = (ConfigurableApplicationContext)context.getParent(); } while (context != null); } }
@Test public void noHiddenHttpMethodFilter() { try (ConfigurableApplicationContext ctx = SpringApplication.run(NoHiddenHttpMethodFilterConfig.class, "--spring.jmx.enabled=false")) { assertThat(ctx.getEnvironment().getProperty("spring.webflux.hiddenmethod.filter.enabled")) .isEqualTo("false"); assertThat(ctx.getBeanNamesForType(HiddenHttpMethodFilter.class)).isEmpty(); } }
public static void main(String[] args) { ApplicationContext applicationContext = new ClassPathXmlApplicationContext(BEAN_CONFIG); AmqpTemplate amqpTemplate = applicationContext.getBean("inboundAmqpTemplate", RabbitTemplate.class); for (int i = 0; i < SEND_MESSAGE_COUNT; i++ ) { amqpTemplate.convertAndSend("foo message: " + i); } ((ConfigurableApplicationContext) applicationContext).close(); } }
private ContextId getContextId(ConfigurableApplicationContext applicationContext) { ApplicationContext parent = applicationContext.getParent(); if (parent != null && parent.containsBean(ContextId.class.getName())) { return parent.getBean(ContextId.class).createChildId(); } return new ContextId(getApplicationId(applicationContext.getEnvironment())); }
/** * Overridden as we can't trust superclass method * @see org.springframework.context.AbstractApplicationContextTests#testEvents() */ @Override protected void doTestEvents(TestListener listener, TestListener parentListener, MyEvent event) { TestListener listenerBean = (TestListener) this.applicationContext.getBean("testListener"); TestListener parentListenerBean = (TestListener) this.applicationContext.getParent().getBean("parentListener"); super.doTestEvents(listenerBean, parentListenerBean, event); }
for (Iterator<ConfigurableApplicationContext> it = contexts.iterator(); it.hasNext();) { ConfigurableApplicationContext context = it.next(); result.append("{\n\"context\": \"").append(context.getId()).append("\",\n"); if (context.getParent() != null) { result.append("\"parent\": \"").append(context.getParent().getId()).append("\",\n"); ConfigurableListableBeanFactory bf = context.getBeanFactory(); String[] beanNames = bf.getBeanDefinitionNames(); boolean elementAppended = false; for (String beanName : beanNames) { BeanDefinition bd = bf.getBeanDefinition(beanName); if (isBeanEligible(beanName, bd, bf)) { if (elementAppended) { appendArray(result, bf.getAliases(beanName)); result.append(",\n"); String scope = bd.getScope();
@Test public void test() { ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext(MyConfig.class); assertThat("someDependency was not post processed", ctx.getBeanFactory().getBeanDefinition("someDependency").getDescription(), equalTo("post processed by MyPostProcessor")); MyConfig config = ctx.getBean(MyConfig.class); assertTrue("Config class was not enhanced", ClassUtils.isCglibProxy(config)); } }
/** * get all acm properties from application context * @return */ public List<AcmPropertySource> getAll() { List<AcmPropertySource> result = new ArrayList<>(); ConfigurableApplicationContext ctx = (ConfigurableApplicationContext) applicationContext; for (PropertySource p : ctx.getEnvironment().getPropertySources()) { if (p instanceof AcmPropertySource) { result.add((AcmPropertySource) p); } else if (p instanceof CompositePropertySource) { collectAcmPropertySources((CompositePropertySource) p, result); } } return result; }
@Nullable private ConfigurableListableBeanFactory deduceBeanFactory(@Nullable BeanDefinitionRegistry source) { if (source instanceof ConfigurableListableBeanFactory) { return (ConfigurableListableBeanFactory) source; } if (source instanceof ConfigurableApplicationContext) { return (((ConfigurableApplicationContext) source).getBeanFactory()); } return null; }
/** * Close the view bean factory on context shutdown. */ @Override public void destroy() throws BeansException { if (this.cachedFactory != null) { this.cachedFactory.close(); } }
protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) { if (parentBeanFactory != null) { GenericApplicationContextFactory.this.prepareBeanFactory(parentBeanFactory, beanFactory); for (Class<? extends BeanFactoryPostProcessor> cls : getBeanFactoryPostProcessorClasses()) { for (String name : parent.getBeanNamesForType(cls)) { beanFactory.registerSingleton(name, (parent.getBean(name))); } } } }
@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 onApplicationPreparedEvent(ApplicationPreparedEvent event) { ConfigurableListableBeanFactory beanFactory = event.getApplicationContext() .getBeanFactory(); if (!beanFactory.containsBean(LOGGING_SYSTEM_BEAN_NAME)) { beanFactory.registerSingleton(LOGGING_SYSTEM_BEAN_NAME, this.loggingSystem); } }