Java Code Examples for org.springframework.beans.factory.support.StaticListableBeanFactory

Following code examples demonstrate how to use org.springframework.beans.factory.support.StaticListableBeanFactoryfrom spring-framework. These examples are extracted from various highly rated open source projects. You can directly use these code snippets or view their entire linked source code. These snippets are extracted to provide contextual information about how to use this class in the real world. These samples also let you understand some good practices on how to use org.springframework.beans.factory.support.StaticListableBeanFactoryand various code implementation of this class.

  public static StateMachine<OrderStates, OrderEvents> buildMachine() throws Exception {
    Builder<OrderStates, OrderEvents> builder = StateMachineBuilder.builder();

    builder.configureConfiguration().withConfiguration().autoStartup(true)
        .beanFactory(new StaticListableBeanFactory()).taskExecutor(new SyncTaskExecutor())
        .taskScheduler(new ConcurrentTaskScheduler());

    builder.configureStates().withStates()
    .initial(OrderStates.SI)
    .end(OrderStates.S2)
        .states(EnumSet.allOf(OrderStates.class));;

    builder.configureTransitions().withExternal()
    .source(OrderStates.SI)
    .target(OrderStates.S1)
        .event(OrderEvents.E1).action(new OrderAction());

    return builder.build();
  } 


	public void test() {

		

		

		

		

		

		

		

		

		
		C bean = new C();
		Map<String, Object> beans = new HashMap<>();
		beans.put("bean", bean);
		StaticListableBeanFactory bf = new StaticListableBeanFactory(beans);

		
		aeq(1, bf.getBeanDefinitionCount());

		
		aeq(1, bf.getBeanDefinitionNames().length);
		aeq("bean", bf.getBeanDefinitionNames()[0]);

		
		aeq(true, bf.containsBeanDefinition("bean"));

		

		

		
		aeq(1, bf.getBeanNamesForAnnotation(A.class).length);
		aeq("bean", bf.getBeanNamesForAnnotation(A.class)[0]);

		
		aeq(1, bf.getBeansWithAnnotation(A.class).size());
		aeqr(bean, bf.getBeansWithAnnotation(A.class).get("bean"));

		
		aeq("value", bf.findAnnotationOnBean("bean", A.class).value());

		

		
		aeq(1, bf.getBeanNamesForType(C.class).length);
		aeq("bean", bf.getBeanNamesForType(C.class)[0]);

		aeq(1, bf.getBeanNamesForType(ResolvableType.forClass(C.class)).length);
		aeq("bean", bf.getBeanNamesForType(ResolvableType.forClass(C.class))[0]);

		aeq(1, bf.getBeanNamesForType(C.class, false, false).length);
		aeq("bean", bf.getBeanNamesForType(C.class, false, false)[0]);

		
		aeq(1, bf.getBeansOfType(C.class).size());
		aeqr(bean, bf.getBeansOfType(C.class).get("bean"));

		aeq(1, bf.getBeansOfType(C.class, false, false).size());
		aeqr(bean, bf.getBeansOfType(C.class, false, false).get("bean"));

		

	} 

    public void testStateMachine() throws Exception {
        StateMachineBuilder.Builder<States, Events> builder = StateMachineBuilder.builder();
        Configurer config = new Configurer();
        config.configure(builder.configureConfiguration());
        config.configure(builder.configureStates());
        config.configure(builder.configureTransitions());
        builder.configureConfiguration().withConfiguration().beanFactory(new StaticListableBeanFactory());
        StateMachine<States, Events> stateMachine = builder.build();

        Message<Events> startMessage = MessageBuilder
                .withPayload(Events.START)
                .setHeader("MongoTableName", "we_connect_test")
                .setHeader("MaxComputeTableName", "stat_Test")
                .build()
                ;
        stateMachine.start();
        stateMachine.sendEvent(startMessage);
    } 

	public void test() {

		

		

		

		

		
		Object bean1 = new Object();
		Object bean2 = new Object();

		Map<String, Object> beans = new HashMap<>();
		beans.put("bean1", bean1);
		StaticListableBeanFactory parentBeanFactory = new StaticListableBeanFactory(beans);

		DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory(parentBeanFactory);
		beanFactory.registerSingleton("bean2", bean2);

		

		aeqr(parentBeanFactory, beanFactory.getParentBeanFactory());

		
		aeq(true, beanFactory.containsLocalBean("bean2"));
		aeq(false, beanFactory.containsLocalBean("bean1"));

	} 

	public static FlowBuilderServices getServices() {
		FlowBuilderServices services = new FlowBuilderServices();
		services.setViewFactoryCreator(new MockViewFactoryCreator());
		services.setConversionService(new DefaultConversionService());
		services.setExpressionParser(DefaultExpressionParserFactory.getExpressionParser());
		services.setResourceLoader(new DefaultResourceLoader());
		services.setBeanFactory(new StaticListableBeanFactory());
		return services;
	} 

    public static ApplicationContext create(Map<String, Object> beans) {
        log.debug("Creating concrete application context with {} bean(s)", beans.size());
        log.trace("- Specifically {}", beans);
        StaticListableBeanFactory factory = new StaticListableBeanFactory();
        for (Map.Entry<String, Object> entry : beans.entrySet()) {
            factory.addBean(entry.getKey(), entry.getValue());
        }
        ConcreteStaticApplicationContext context = new ConcreteStaticApplicationContext(
            new DefaultListableBeanFactory(factory));
        context.refresh();
        return context;
    } 

    public StateMachine<SimpleState, SimpleEvent> simpleStateMachine() throws Exception {
        StateMachineBuilder.Builder<SimpleState, SimpleEvent> stateMachineBuilder = StateMachineBuilder.builder();

        stateMachineBuilder.configureConfiguration().withConfiguration().beanFactory(new StaticListableBeanFactory());

        stateMachineBuilder.configureStates().withStates().initial(SimpleState.ROOT)
                .end(SimpleState.END).states(EnumSet.allOf(SimpleState.class));

        stateMachineBuilder.configureTransitions().withExternal().source(SimpleState.ROOT).target(SimpleState.S1).event(SimpleEvent.ENTRY).action(entryAction)
                .and().withExternal().source(SimpleState.S1).target(SimpleState.S2).event(SimpleEvent.E1).action(event1Action).timer(1000L)
                .and().withExternal().source(SimpleState.S2).target(SimpleState.S3).event(SimpleEvent.E2).action(event2Action)
                .and().withExternal().source(SimpleState.S3).target(SimpleState.END).event(SimpleEvent.EXIT).action(exitAction);

        StateMachine<SimpleState, SimpleEvent> stateMachine = stateMachineBuilder.build();
        return stateMachine;
    } 

    public void createBadInstance() {
        StaticListableBeanFactory bf = new StaticListableBeanFactory();
        bf.addBean("bean", "This is a bean");
        bad = (BadInterface)
                Proxy.newProxyInstance(this.getClass().getClassLoader(),
                        new Class[]{BadInterface.class},
                        new BeanFactoryInvocationHandler(bf));
    } 

    public void setup() {
        StaticListableBeanFactory beanFactory = new StaticListableBeanFactory();
        beanFactory.addBean("userService", userService);
        factory = new SpelAwareProxyProjectionFactory();
        factory.setBeanFactory(beanFactory);
        Mockito.when(userService.findUserById(USER_ID)).thenReturn(TestFixtures.createUser());
    } 

  public static StateMachine<CallStates, CallEvents> buildMachine() throws Exception {
    Builder<CallStates, CallEvents> builder = StateMachineBuilder.builder();

    builder.configureConfiguration()
        .withConfiguration()
          .autoStartup(true)
            .beanFactory(new StaticListableBeanFactory())
            .taskExecutor(new SyncTaskExecutor())
            .taskScheduler(new ConcurrentTaskScheduler());

    builder.configureStates()
        .withStates()
            .initial(CallStates.SI)
            .end(CallStates.S2)
            .states(EnumSet.allOf(CallStates.class));;

    builder.configureTransitions()
        .withExternal()
            .source(CallStates.SI)
            .target(CallStates.S1)
            .event(CallEvents.E1)
            .action(new CallAction());

    return builder.build();
} 

    @Override
    public void configure(StateMachineConfigurationConfigurer<States, Events> config)
            throws Exception {
        config
                .withConfiguration()
                .autoStartup(true)
                .beanFactory(new StaticListableBeanFactory())
                .taskExecutor(new SyncTaskExecutor())
                .taskScheduler(new ConcurrentTaskScheduler())
                .listener(new StateMachineListenerAdapter<>());
    } 

    public StaticTabConfigurer(Object... beans) {
        staticFactory = new StaticListableBeanFactory();
        ModelMap modelMap = new ModelMap();
        modelMap.addAllObjects(Arrays.asList(beans));
        for (Object o : modelMap.entrySet()) {
            Map.Entry<String, Object> entry = (Map.Entry<String, Object>) o;
            staticFactory.addBean(entry.getKey(), entry.getValue());
        }
    } 

Advertisement
Javadoc
Static org.springframework.beans.factory.BeanFactory implementation which allows to register existin

g singleton instances programmatically. Does not have support for prototype beans or aliases.

Serves as example for a simple implementation of the org.springframework.beans.factory.ListableBeanFactory interface, managing existing bean instances rather than creating new ones based on bean definitions, and not implementing any extended SPI interfaces (such as org.springframework.beans.factory.config.ConfigurableBeanFactory).

For a full-fledged factory based on bean definitions, have a look at DefaultListableBeanFactory. @author Rod Johnson @author Juergen Hoeller @since 06.01.2003 @see DefaultListableBeanFactory

Read More
Advertisement