/** * Register a bean from the given bean class, optionally customizing its * bean definition metadata (typically declared as a lambda expression * or method reference). * @param beanClass the class of the bean (resolving a public constructor * to be autowired, possibly simply the default constructor) * @param customizers one or more callbacks for customizing the factory's * {@link BeanDefinition}, e.g. setting a lazy-init or primary flag * @since 5.0 * @see #registerBean(String, Class, Supplier, BeanDefinitionCustomizer...) */ public final <T> void registerBean(Class<T> beanClass, BeanDefinitionCustomizer... customizers) { registerBean(null, beanClass, null, customizers); }
/** * Register a bean from the given bean class, using the given supplier for * obtaining a new instance (typically declared as a lambda expression or * method reference), optionally customizing its bean definition metadata * (again typically declared as a lambda expression or method reference). * @param beanClass the class of the bean * @param supplier a callback for creating an instance of the bean * @param customizers one or more callbacks for customizing the factory's * {@link BeanDefinition}, e.g. setting a lazy-init or primary flag * @since 5.0 * @see #registerBean(String, Class, Supplier, BeanDefinitionCustomizer...) */ public final <T> void registerBean( Class<T> beanClass, Supplier<T> supplier, BeanDefinitionCustomizer... customizers) { registerBean(null, beanClass, supplier, customizers); }
/** * Register a bean from the given bean class, optionally customizing its * bean definition metadata (typically declared as a lambda expression * or method reference). * @param beanClass the class of the bean (resolving a public constructor * to be autowired, possibly simply the default constructor) * @param customizers one or more callbacks for customizing the factory's * {@link BeanDefinition}, e.g. setting a lazy-init or primary flag * @since 5.0 * @see #registerBean(String, Class, Supplier, BeanDefinitionCustomizer...) */ public final <T> void registerBean(Class<T> beanClass, BeanDefinitionCustomizer... customizers) { registerBean(null, beanClass, null, customizers); }
/** * Register a bean from the given bean class, using the given supplier for * obtaining a new instance (typically declared as a lambda expression or * method reference), optionally customizing its bean definition metadata * (again typically declared as a lambda expression or method reference). * @param beanClass the class of the bean * @param supplier a callback for creating an instance of the bean * @param customizers one or more callbacks for customizing the factory's * {@link BeanDefinition}, e.g. setting a lazy-init or primary flag * @since 5.0 * @see #registerBean(String, Class, Supplier, BeanDefinitionCustomizer...) */ public final <T> void registerBean( Class<T> beanClass, Supplier<T> supplier, BeanDefinitionCustomizer... customizers) { registerBean(null, beanClass, supplier, customizers); }
/** * Register a bean from the given bean class, using the given supplier for * obtaining a new instance (typically declared as a lambda expression or * method reference), optionally customizing its bean definition metadata * (again typically declared as a lambda expression or method reference). * @param beanName the name of the bean (may be {@code null}) * @param beanClass the class of the bean (resolving a public constructor * to be autowired, possibly simply the default constructor) * @param customizers one or more callbacks for customizing the factory's * {@link BeanDefinition}, e.g. setting a lazy-init or primary flag * @since 5.0 * @see #registerBean(String, Class, Supplier, BeanDefinitionCustomizer...) */ public final <T> void registerBean( @Nullable String beanName, Class<T> beanClass, BeanDefinitionCustomizer... customizers) { registerBean(beanName, beanClass, null, customizers); }
/** * Register a bean from the given bean class, using the given supplier for * obtaining a new instance (typically declared as a lambda expression or * method reference), optionally customizing its bean definition metadata * (again typically declared as a lambda expression or method reference). * @param beanName the name of the bean (may be {@code null}) * @param beanClass the class of the bean (resolving a public constructor * to be autowired, possibly simply the default constructor) * @param customizers one or more callbacks for customizing the factory's * {@link BeanDefinition}, e.g. setting a lazy-init or primary flag * @since 5.0 * @see #registerBean(String, Class, Supplier, BeanDefinitionCustomizer...) */ public final <T> void registerBean( @Nullable String beanName, Class<T> beanClass, BeanDefinitionCustomizer... customizers) { registerBean(beanName, beanClass, null, customizers); }
@Autowired void register(GenericApplicationContext ctx) { ctx.registerBean("spouse", TestBean.class, () -> new TestBean("functional")); Supplier<TestBean> testBeanSupplier = () -> new TestBean(ctx.getBean("spouse", TestBean.class)); ctx.registerBean(TestBean.class, testBeanSupplier, bd -> bd.setPrimary(true)); }
@Test public void individualNamedBeans() { GenericApplicationContext context = new GenericApplicationContext(); context.registerBean("a", BeanA.class); context.registerBean("b", BeanB.class); context.registerBean("c", BeanC.class); context.refresh(); assertSame(context.getBean("b"), context.getBean("a", BeanA.class).b); assertSame(context.getBean("c"), context.getBean("a", BeanA.class).c); assertSame(context, context.getBean("b", BeanB.class).applicationContext); }
@Test public void individualBeans() { GenericApplicationContext context = new GenericApplicationContext(); context.registerBean(BeanA.class); context.registerBean(BeanB.class); context.registerBean(BeanC.class); context.refresh(); assertSame(context.getBean(BeanB.class), context.getBean(BeanA.class).b); assertSame(context.getBean(BeanC.class), context.getBean(BeanA.class).c); assertSame(context, context.getBean(BeanB.class).applicationContext); }
@Test public void individualBeanWithNullReturningSupplier() { GenericApplicationContext context = new GenericApplicationContext(); context.registerBean("a", BeanA.class, () -> null); context.registerBean("b", BeanB.class, BeanB::new); context.registerBean("c", BeanC.class, BeanC::new); context.refresh(); assertTrue(ObjectUtils.containsElement(context.getBeanNamesForType(BeanA.class), "a")); assertTrue(ObjectUtils.containsElement(context.getBeanNamesForType(BeanB.class), "b")); assertTrue(ObjectUtils.containsElement(context.getBeanNamesForType(BeanC.class), "c")); assertTrue(context.getBeansOfType(BeanA.class).isEmpty()); assertSame(context.getBean(BeanB.class), context.getBeansOfType(BeanB.class).values().iterator().next()); assertSame(context.getBean(BeanC.class), context.getBeansOfType(BeanC.class).values().iterator().next()); }
@Test public void individualBeanWithSupplierAndCustomizer() { GenericApplicationContext context = new GenericApplicationContext(); context.registerBean(BeanA.class, () -> new BeanA(context.getBean(BeanB.class), context.getBean(BeanC.class)), bd -> bd.setLazyInit(true)); context.registerBean(BeanB.class, BeanB::new); context.registerBean(BeanC.class, BeanC::new); context.refresh(); assertFalse(context.getBeanFactory().containsSingleton(BeanA.class.getName())); assertSame(context.getBean(BeanB.class), context.getBean(BeanA.class).b); assertSame(context.getBean(BeanC.class), context.getBean(BeanA.class).c); assertSame(context, context.getBean(BeanB.class).applicationContext); }
@Test public void individualBeanWithSupplier() { GenericApplicationContext context = new GenericApplicationContext(); context.registerBean(BeanA.class, () -> new BeanA(context.getBean(BeanB.class), context.getBean(BeanC.class))); context.registerBean(BeanB.class, BeanB::new); context.registerBean(BeanC.class, BeanC::new); context.refresh(); assertTrue(context.getBeanFactory().containsSingleton(BeanA.class.getName())); assertSame(context.getBean(BeanB.class), context.getBean(BeanA.class).b); assertSame(context.getBean(BeanC.class), context.getBean(BeanA.class).c); assertSame(context, context.getBean(BeanB.class).applicationContext); assertArrayEquals(new String[] {BeanA.class.getName()}, context.getDefaultListableBeanFactory().getDependentBeans(BeanB.class.getName())); assertArrayEquals(new String[] {BeanA.class.getName()}, context.getDefaultListableBeanFactory().getDependentBeans(BeanC.class.getName())); }
@Test public void individualNamedBeanWithSupplier() { GenericApplicationContext context = new GenericApplicationContext(); context.registerBean("a", BeanA.class, () -> new BeanA(context.getBean(BeanB.class), context.getBean(BeanC.class))); context.registerBean("b", BeanB.class, BeanB::new); context.registerBean("c", BeanC.class, BeanC::new); context.refresh(); assertTrue(context.getBeanFactory().containsSingleton("a")); assertSame(context.getBean("b", BeanB.class), context.getBean(BeanA.class).b); assertSame(context.getBean("c"), context.getBean("a", BeanA.class).c); assertSame(context, context.getBean("b", BeanB.class).applicationContext); }
@Test public void individualNamedBeanWithSupplierAndCustomizer() { GenericApplicationContext context = new GenericApplicationContext(); context.registerBean("a", BeanA.class, () -> new BeanA(context.getBean(BeanB.class), context.getBean(BeanC.class)), bd -> bd.setLazyInit(true)); context.registerBean("b", BeanB.class, BeanB::new); context.registerBean("c", BeanC.class, BeanC::new); context.refresh(); assertFalse(context.getBeanFactory().containsSingleton("a")); assertSame(context.getBean("b", BeanB.class), context.getBean(BeanA.class).b); assertSame(context.getBean("c"), context.getBean("a", BeanA.class).c); assertSame(context, context.getBean("b", BeanB.class).applicationContext); }
@SuppressWarnings("unchecked") private static void registerBean(String beanName, Object bean, BeanFactory beanFactory) { Assert.notNull(beanName, "bean name must not be null"); Assert.isInstanceOf(GenericApplicationContext.class, beanFactory, "beanFactory must be an instance of GenericApplicationContext"); GenericApplicationContext applicationContext = (GenericApplicationContext) beanFactory; applicationContext.registerBean(beanName, (Class<Object>) bean.getClass(), () -> bean); }
/** * Add a MicrometerMetricsCaptor to the context if there's a MeterRegistry; if * there's already a {@link MetricsCaptor} bean, return that. * @param applicationContext the application context. * @return the instance. */ public static MetricsCaptor loadCaptor(ApplicationContext applicationContext) { try { MeterRegistry registry = applicationContext.getBean(MeterRegistry.class); if (applicationContext instanceof GenericApplicationContext && !applicationContext.containsBean(MICROMETER_CAPTOR_NAME)) { ((GenericApplicationContext) applicationContext).registerBean(MICROMETER_CAPTOR_NAME, MicrometerMetricsCaptor.class, () -> new MicrometerMetricsCaptor(registry)); } return applicationContext.getBean(MICROMETER_CAPTOR_NAME, MetricsCaptor.class); } catch (NoSuchBeanDefinitionException e) { return null; } }
private void registerWebFluxAutoConfiguration(GenericApplicationContext context) { context.registerBean(DefaultErrorWebExceptionHandler.class, () -> errorHandler(context)); context.registerBean(WebHttpHandlerBuilder.WEB_HANDLER_BEAN_NAME, HttpWebHandlerAdapter.class, () -> httpHandler(context)); context.addApplicationListener(new ServerListener(context)); }
@Override public void initialize(GenericApplicationContext context) { context.registerBean(Jackson2ObjectMapperBuilderCustomizer.class, () -> new Jackson2ObjectMapperBuilderCustomizerConfiguration().standardJacksonObjectMapperBuilderCustomizer(context, this.properties)); context.registerBean(Jackson2ObjectMapperBuilder.class, () -> new JacksonObjectMapperBuilderConfiguration(context).jacksonObjectMapperBuilder(new ArrayList<>(context.getBeansOfType(Jackson2ObjectMapperBuilderCustomizer.class).values()))); context.registerBean(ObjectMapper.class, () -> new JacksonObjectMapperConfiguration().jacksonObjectMapper(context.getBean(Jackson2ObjectMapperBuilder.class))); } }
private static ConfigurableApplicationContext parentContext() { GenericApplicationContext parentContext = new GenericApplicationContext(); parentContext.setId("parent-application-context"); // 设置上下文 ID // 注册名为 "helloWorld" 的 String 类型的 Bean,内容为 "Hello,World!" parentContext.registerBean("helloWorld", String.class, () -> "Hello,World!"); parentContext.refresh(); // 双亲应用上下文务必启动 return parentContext; } }
@Override public void initialize(GenericApplicationContext context) { Properties properties = new Properties(); this.props = properties; context.registerBean(Properties.class, () -> properties); context.registerBean("function", FunctionRegistration.class, () -> new FunctionRegistration<Function<Foo, Bar>>(function()) .type(FunctionType.from(Foo.class).to(Bar.class).getType())); }