Java Code Examples for org.springframework.context.annotation.AnnotationConfigApplicationContext#getBeanFactory()

The following examples show how to use org.springframework.context.annotation.AnnotationConfigApplicationContext#getBeanFactory() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
@Before
@SuppressWarnings("resource")
public void setup() throws Exception {
	AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
	context.refresh();

	ReactiveAdapterRegistry adapterRegistry = ReactiveAdapterRegistry.getSharedInstance();
	this.resolver = new CookieValueMethodArgumentResolver(context.getBeanFactory(), adapterRegistry);
	this.bindingContext = new BindingContext();

	Method method = ReflectionUtils.findMethod(getClass(), "params", (Class<?>[]) null);
	this.cookieParameter = new SynthesizingMethodParameter(method, 0);
	this.cookieStringParameter = new SynthesizingMethodParameter(method, 1);
	this.stringParameter = new SynthesizingMethodParameter(method, 2);
	this.cookieMonoParameter = new SynthesizingMethodParameter(method, 3);
}
 
Example 2
@Before
@SuppressWarnings("resource")
public void setup() throws Exception {
	AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
	context.refresh();
	ReactiveAdapterRegistry adapterRegistry = ReactiveAdapterRegistry.getSharedInstance();
	this.resolver = new RequestHeaderMethodArgumentResolver(context.getBeanFactory(), adapterRegistry);

	ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer();
	initializer.setConversionService(new DefaultFormattingConversionService());
	this.bindingContext = new BindingContext(initializer);

	Method method = ReflectionUtils.findMethod(getClass(), "params", (Class<?>[]) null);
	this.paramNamedDefaultValueStringHeader = new SynthesizingMethodParameter(method, 0);
	this.paramNamedValueStringArray = new SynthesizingMethodParameter(method, 1);
	this.paramSystemProperty = new SynthesizingMethodParameter(method, 2);
	this.paramResolvedNameWithExpression = new SynthesizingMethodParameter(method, 3);
	this.paramResolvedNameWithPlaceholder = new SynthesizingMethodParameter(method, 4);
	this.paramNamedValueMap = new SynthesizingMethodParameter(method, 5);
	this.paramDate = new SynthesizingMethodParameter(method, 6);
	this.paramInstant = new SynthesizingMethodParameter(method, 7);
	this.paramMono = new SynthesizingMethodParameter(method, 8);
}
 
Example 3
public static void main(String[] args) throws InterruptedException {
    // 创建 BeanFactory 容器
    AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
    // 创建一个外部 UserFactory 对象
    UserFactory userFactory = new DefaultUserFactory();
    SingletonBeanRegistry singletonBeanRegistry = applicationContext.getBeanFactory();
    // 注册外部单例对象
    singletonBeanRegistry.registerSingleton("userFactory", userFactory);
    // 启动 Spring 应用上下文
    applicationContext.refresh();

    // 通过依赖查找的方式来获取 UserFactory
    UserFactory userFactoryByLookup = applicationContext.getBean("userFactory", UserFactory.class);
    System.out.println("userFactory  == userFactoryByLookup : " + (userFactory == userFactoryByLookup));

    // 关闭 Spring 应用上下文
    applicationContext.close();
}
 
Example 4
@Before
@SuppressWarnings("resource")
public void setup() throws Exception {
	AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
	context.refresh();

	ReactiveAdapterRegistry adapterRegistry = ReactiveAdapterRegistry.getSharedInstance();
	this.resolver = new CookieValueMethodArgumentResolver(context.getBeanFactory(), adapterRegistry);
	this.bindingContext = new BindingContext();

	Method method = ReflectionUtils.findMethod(getClass(), "params", (Class<?>[]) null);
	this.cookieParameter = new SynthesizingMethodParameter(method, 0);
	this.cookieStringParameter = new SynthesizingMethodParameter(method, 1);
	this.stringParameter = new SynthesizingMethodParameter(method, 2);
	this.cookieMonoParameter = new SynthesizingMethodParameter(method, 3);
}
 
Example 5
@Before
@SuppressWarnings("resource")
public void setup() throws Exception {
	AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
	context.refresh();
	ReactiveAdapterRegistry adapterRegistry = ReactiveAdapterRegistry.getSharedInstance();
	this.resolver = new RequestHeaderMethodArgumentResolver(context.getBeanFactory(), adapterRegistry);

	ConfigurableWebBindingInitializer initializer = new ConfigurableWebBindingInitializer();
	initializer.setConversionService(new DefaultFormattingConversionService());
	this.bindingContext = new BindingContext(initializer);

	Method method = ReflectionUtils.findMethod(getClass(), "params", (Class<?>[]) null);
	this.paramNamedDefaultValueStringHeader = new SynthesizingMethodParameter(method, 0);
	this.paramNamedValueStringArray = new SynthesizingMethodParameter(method, 1);
	this.paramSystemProperty = new SynthesizingMethodParameter(method, 2);
	this.paramResolvedNameWithExpression = new SynthesizingMethodParameter(method, 3);
	this.paramResolvedNameWithPlaceholder = new SynthesizingMethodParameter(method, 4);
	this.paramNamedValueMap = new SynthesizingMethodParameter(method, 5);
	this.paramDate = new SynthesizingMethodParameter(method, 6);
	this.paramInstant = new SynthesizingMethodParameter(method, 7);
	this.paramMono = new SynthesizingMethodParameter(method, 8);
}
 
Example 6
private AnnotationConfigApplicationContext createContext(String dataId,
		String groupId, String content) throws NacosException {
	AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();

	ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();

	configService.publishConfig(dataId, groupId, content);

	beanFactory.registerSingleton(CONFIG_SERVICE_BEAN_NAME, configService);

	context.register(AnnotationNacosInjectedBeanPostProcessor.class,
			NacosPropertySourcePostProcessor.class, ConfigServiceBeanBuilder.class,
			AnnotationNacosPropertySourceBuilder.class, TestConfiguration.class,
			TestApplicationHolder.class);
	return context;
}
 
Example 7
@Before
@SuppressWarnings("resource")
public void setup() throws Exception {
	AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
	context.refresh();
	ReactiveAdapterRegistry registry = ReactiveAdapterRegistry.getSharedInstance();
	this.resolver = new RequestAttributeMethodArgumentResolver(context.getBeanFactory(), registry);
}
 
Example 8
@Before
@SuppressWarnings("resource")
public void setup() {
	AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
	context.refresh();
	ReactiveAdapterRegistry adapterRegistry = ReactiveAdapterRegistry.getSharedInstance();
	this.resolver = new SessionAttributeMethodArgumentResolver(context.getBeanFactory(), adapterRegistry);
	this.session = mock(WebSession.class);
	this.exchange = MockServerWebExchange.builder(MockServerHttpRequest.get("/")).session(this.session).build();
	this.handleMethod = ReflectionUtils.findMethod(getClass(), "handleWithSessionAttribute", (Class<?>[]) null);
}
 
Example 9
@Before
@SuppressWarnings("resource")
public void setup() throws Exception {
	AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
	context.refresh();
	ReactiveAdapterRegistry adapterRegistry = ReactiveAdapterRegistry.getSharedInstance();
	this.resolver = new ExpressionValueMethodArgumentResolver(context.getBeanFactory(), adapterRegistry);

	Method method = ReflectionUtils.findMethod(getClass(), "params", (Class<?>[]) null);
	this.paramSystemProperty = new MethodParameter(method, 0);
	this.paramNotSupported = new MethodParameter(method, 1);
	this.paramAlsoNotSupported = new MethodParameter(method, 2);
}
 
Example 10
public static void main(String[] args) {
        // 创建 BeanFactory 容器
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
        // 将当前类 ObjectProviderDemo 作为配置类(Configuration Class)
        applicationContext.register(ObjectProviderDemo.class);

        // 1. 获取 HierarchicalBeanFactory <- ConfigurableBeanFactory <- ConfigurableListableBeanFactory
        ConfigurableListableBeanFactory beanFactory = applicationContext.getBeanFactory();
//        System.out.println("当前 BeanFactory 的 Parent BeanFactory : " + beanFactory.getParentBeanFactory());

        // 2. 设置 Parent BeanFactory
        HierarchicalBeanFactory parentBeanFactory = createParentBeanFactory();
        beanFactory.setParentBeanFactory(parentBeanFactory);
//        System.out.println("当前 BeanFactory 的 Parent BeanFactory : " + beanFactory.getParentBeanFactory());

        displayContainsLocalBean(beanFactory, "user");
        displayContainsLocalBean(parentBeanFactory, "user");

        displayContainsBean(beanFactory, "user");
        displayContainsBean(parentBeanFactory, "user");

        // 启动应用上下文
        applicationContext.refresh();

        // 关闭应用上下文
        applicationContext.close();

    }
 
Example 11
@Before
@SuppressWarnings("resource")
public void setup() throws Exception {
	AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
	context.refresh();
	ReactiveAdapterRegistry registry = ReactiveAdapterRegistry.getSharedInstance();
	this.resolver = new RequestAttributeMethodArgumentResolver(context.getBeanFactory(), registry);
}
 
Example 12
@Before
@SuppressWarnings("resource")
public void setup() {
	AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
	context.refresh();
	ReactiveAdapterRegistry adapterRegistry = ReactiveAdapterRegistry.getSharedInstance();
	this.resolver = new SessionAttributeMethodArgumentResolver(context.getBeanFactory(), adapterRegistry);
	this.session = mock(WebSession.class);
	this.exchange = MockServerWebExchange.builder(MockServerHttpRequest.get("/")).session(this.session).build();
	this.handleMethod = ReflectionUtils.findMethod(getClass(), "handleWithSessionAttribute", (Class<?>[]) null);
}
 
Example 13
@Before
@SuppressWarnings("resource")
public void setup() throws Exception {
	AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
	context.refresh();
	ReactiveAdapterRegistry adapterRegistry = ReactiveAdapterRegistry.getSharedInstance();
	this.resolver = new ExpressionValueMethodArgumentResolver(context.getBeanFactory(), adapterRegistry);

	Method method = ReflectionUtils.findMethod(getClass(), "params", (Class<?>[]) null);
	this.paramSystemProperty = new MethodParameter(method, 0);
	this.paramNotSupported = new MethodParameter(method, 1);
	this.paramAlsoNotSupported = new MethodParameter(method, 2);
}
 
Example 14
Source Project: riptide   File: DefaultRiptideConfigurerTest.java    License: MIT License 5 votes vote down vote up
@Test
void shouldFindPrimaryBeanDefinitionIfAvailable() {
    final AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
    context.register(PrimaryTracerConfiguration.class);
    context.refresh();
    final ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
    final DefaultRiptideConfigurer configurer = new DefaultRiptideConfigurer(beanFactory, null);
    final BeanReference bd = configurer.getBeanRef(Tracer.class, "tracer");
    assertThat(bd.getBeanName()).isEqualTo("primaryTracer");
}
 
Example 15
Source Project: riptide   File: DefaultRiptideConfigurerTest.java    License: MIT License 5 votes vote down vote up
@Test
void shouldBeanDefinitionIfSingleBeanRegisteredForType() {
    final AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
    context.register(SingleTracerConfiguration.class);
    context.refresh();
    final ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
    final DefaultRiptideConfigurer configurer = new DefaultRiptideConfigurer(beanFactory, null);
    final BeanReference bd = configurer.getBeanRef(Tracer.class, "tracer");
    assertThat(bd.getBeanName()).isEqualTo("opentracingTracer");
}
 
Example 16
Source Project: riptide   File: DefaultRiptideConfigurerTest.java    License: MIT License 5 votes vote down vote up
@Test
void shouldFindBeanDefinitionByNameIfNoPrimaryBeanAvailable() {
    final AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
    context.register(DoubleTracerConfiguration.class);
    context.refresh();
    final ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
    final DefaultRiptideConfigurer configurer = new DefaultRiptideConfigurer(beanFactory, null);
    final BeanReference bd = configurer.getBeanRef(Tracer.class, "tracer");
    assertThat(bd.getBeanName()).isEqualTo("tracer");
}
 
Example 17
Source Project: riptide   File: DefaultRiptideConfigurerTest.java    License: MIT License 5 votes vote down vote up
@Test
void shouldFailIfMultipleBeanFoundWithoutCorrespondingName() {
    final AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
    context.register(DoubleTracerConfiguration.class);
    context.refresh();
    final ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
    final DefaultRiptideConfigurer configurer = new DefaultRiptideConfigurer(beanFactory, null);
    assertThrows(NoSuchBeanDefinitionException.class,
                 () -> configurer.getBeanRef(Tracer.class, "opentracingTracer"));
}
 
Example 18
Source Project: synopsys-detect   File: DetectContext.java    License: Apache License 2.0 4 votes vote down vote up
public DetectContext(final DetectRun detectRun) {
    //Detect context is currently actually backed by Spring.
    springContext = new AnnotationConfigApplicationContext();
    springContext.setDisplayName("Detect Context " + detectRun.getRunId());
    beanFactory = springContext.getBeanFactory();
}
 
Example 19
Source Project: hub-detect   File: DetectContext.java    License: Apache License 2.0 4 votes vote down vote up
public DetectContext(DetectRun detectRun) {
    //Detect context is currently actually backed by Spring.
    springContext = new AnnotationConfigApplicationContext();
    springContext.setDisplayName("Detect Context " + detectRun.getRunId());
    beanFactory = springContext.getBeanFactory();
}