Java Code Examples for org.springframework.context.ApplicationContext#getBeanDefinitionNames()

The following examples show how to use org.springframework.context.ApplicationContext#getBeanDefinitionNames() . 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
Source Project: Spring   File: CIBeansTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testConfig() {
    // ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:spring/ctr/sample-config-01.xml");
    ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:spring/ctr/sample-config-02.xml");

    logger.info(" --- All beans in context --- ");
    for(String beanName :  ctx.getBeanDefinitionNames()) {
        logger.info("Bean " + beanName + " of type " + ctx.getBean(beanName).getClass().getSimpleName());
    }

    ComplexBeanImpl complexBean0 = (ComplexBeanImpl) ctx.getBean("complexBean0");
    assertNotNull(complexBean0.getSimpleBean());

    ComplexBeanImpl complexBean1 = (ComplexBeanImpl) ctx.getBean("complexBean1");
    assertNotNull(complexBean1.getSimpleBean());
    assertTrue(complexBean1.isComplex());

    ComplexBean2Impl complexBean2 = (ComplexBean2Impl) ctx.getBean("complexBean2");
    assertNotNull(complexBean2.getSimpleBean1());
    assertNotNull(complexBean2.getSimpleBean2());
}
 
Example 2
Source Project: Spring   File: CIBeansTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testConfig() {
    // ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:spring/ctr/sample-config-01.xml");
    ApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:spring/ctr/sample-config-02.xml");

    System.out.println(" --- All beans in context --- ");
    for(String beanName :  ctx.getBeanDefinitionNames()) {
        System.out.println("Bean " + beanName + " of type " + ctx.getBean(beanName).getClass().getSimpleName());
    }

    ComplexBeanImpl complexBean0 = (ComplexBeanImpl) ctx.getBean("complexBean0");
    assertNotNull(complexBean0.getSimpleBean());

    ComplexBeanImpl complexBean1 = (ComplexBeanImpl) ctx.getBean("complexBean1");
    assertNotNull(complexBean1.getSimpleBean());
    assertTrue(complexBean1.isComplex());

    ComplexBean2Impl complexBean2 = (ComplexBean2Impl) ctx.getBean("complexBean2");
    assertNotNull(complexBean2.getSimpleBean1());
    assertNotNull(complexBean2.getSimpleBean2());
}
 
Example 3
@Profile("trace")
@Bean
public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
    return args -> {

        System.out.println("Let's inspect the beans provided by Spring Boot:\n");

        String[] beanNames = ctx.getBeanDefinitionNames();
        Arrays.sort(beanNames);
        for (String beanName : beanNames) {
            System.out.println(beanName);
        }

        System.out.println("---");
    };
}
 
Example 4
Source Project: cxf   File: SpringBeanLocator.java    License: Apache License 2.0 6 votes vote down vote up
public SpringBeanLocator(ApplicationContext ctx, Bus bus) {
    context = ctx;
    if (bus != null) {
        orig = bus.getExtension(ConfiguredBeanLocator.class);
        if (orig instanceof ExtensionManagerImpl) {
            List<String> names = new ArrayList<>();
            for (String s : ctx.getBeanDefinitionNames()) {
                names.add(s);
                for (String s2 : ctx.getAliases(s)) {
                    names.add(s2);
                }
            }

            ((ExtensionManagerImpl)orig).removeBeansOfNames(names);
        }
    }
    loadOSGIContext(bus);
}
 
Example 5
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
	final ApplicationContext context = event.getApplicationContext();
	final String[] beanDefinitionNames = context.getBeanDefinitionNames();
	for(String beanName : beanDefinitionNames){
		final BeanDefinition beanDefinition = factory.getBeanDefinition(beanName);
		final String originalClassName = beanDefinition.getBeanClassName();
		try {
			final Class<?> originalClass = Class.forName(originalClassName);
			final Method[] methods = originalClass.getMethods();
			for(Method method : methods){
				if(method.isAnnotationPresent(PostProxy.class)){
					final Object bean = context.getBean(beanName);
					final Method currentMethod = bean.getClass().getMethod(method.getName(), method.getParameterTypes());
					currentMethod.invoke(bean);
				}
			}
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}
}
 
Example 6
/**
 * stream不能复用
 * java.lang.IllegalStateException: stream has already been operated upon or closed
 *
 * @param ctx
 * @return
 */
@Bean
public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
    return args -> {
        log.info("来看看 SpringBoot 默认为我们提供的 Bean:");
        String[] beanNames = ctx.getBeanDefinitionNames();
        Arrays.sort(beanNames);
        Arrays.stream(beanNames).forEach(System.out::println);
        Arrays.stream(beanNames).forEach(log::info);
    };
}
 
Example 7
Source Project: JGiven   File: Application.java    License: Apache License 2.0 5 votes vote down vote up
public static void main( String[] args ) {
    ApplicationContext ctx = SpringApplication.run( Application.class, args );

    System.out.println( "Let's inspect the beans provided by Spring Boot:" );

    String[] beanNames = ctx.getBeanDefinitionNames();
    Arrays.sort( beanNames );
    for( String beanName : beanNames ) {
        System.out.println( beanName );
    }
}
 
Example 8
@Bean
public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
    return args -> {

        System.out.println("Let's inspect the beans provided by Spring Boot:\n");

        String[] beanNames = ctx.getBeanDefinitionNames();
        Arrays.sort(beanNames);
        for (String beanName : beanNames) {
            System.out.println(beanName);
        }

        System.out.println("---");
    };
}
 
Example 9
Source Project: xxproject   File: Application.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
    return args -> {

        System.out.println("Let's inspect the beans provided by Spring Boot:");

        String[] beanNames = ctx.getBeanDefinitionNames();
        Arrays.sort(beanNames);
        for (String beanName : beanNames) {
            LOGGER.debug(beanName);
        }
    };
}
 
Example 10
Source Project: Spring-Framework-Essentials   File: RunDemo.java    License: MIT License 5 votes vote down vote up
public static void main(String[] args) {
    ApplicationContext context =
            new AnnotationConfigApplicationContext(
                    AppConfig.class, AppConfig.class);
    System.out.println(context.getBeanDefinitionCount());
    for (String name : context.getBeanDefinitionNames()) {
        System.out.println(name);
    }
}
 
Example 11
Source Project: code   File: IOCTest.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 测试组件扫描规则是否生效
 *
 * @param
 * @return void
 */
@Test
public void test01() {
    ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfig.class);
    String[] definitionNames = applicationContext.getBeanDefinitionNames();
    for (String name : definitionNames) {
        System.out.println(name);
    }
}
 
Example 12
private void registerRepositoryDefinitions(Repository repository, ApplicationContext repoApplicationContext) {
    DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory)
            ((ConfigurableApplicationContext) applicationContext).getBeanFactory();

    String [] beanNames = repoApplicationContext.getBeanDefinitionNames();
    for(String beanName : beanNames) {
        Object repositoryClassInstance = repoApplicationContext.getBean(beanName);
        Class<?> repositoryObjectClass = repositoryClassInstance.getClass();
        if ((beanName.endsWith("Repository") ||
                (beanName.endsWith("Manager") && ! beanName.endsWith("TransactionManager")) )
                        && ! repository.getClass().equals(repositoryClassInstance.getClass())) {
            if (repositoryObjectClass.getInterfaces().length > 0) {
                Class<?> repositoryItfClass = repositoryObjectClass.getInterfaces()[0];
                LOGGER.debug("Set proxy target for {} [{}]", beanName, repositoryItfClass);
                try {
                    Object proxyRepository = beanFactory.getBean(repositoryItfClass);
                    if (proxyRepository instanceof AbstractProxy) {
                        AbstractProxy proxy = (AbstractProxy) proxyRepository;
                        proxy.setTarget(repositoryClassInstance);
                    }
                } catch (NoSuchBeanDefinitionException nsbde) {
                    LOGGER.debug("Unable to proxify {} [{}]", beanName, repositoryItfClass);
                }
            }
        } else if (beanName.endsWith("TransactionManager")) {
            beanFactory.registerSingleton(beanName, repositoryClassInstance);
        }
    }
}
 
Example 13
public static void main(String[] args) {
	ApplicationContext applicationContext = 
			SpringApplication.run(SpringbootIn10StepsApplication.class, args);
	
	for (String name : applicationContext.getBeanDefinitionNames()) {
		System.out.println(name);
	}
}
 
Example 14
Source Project: Mastering-Spring-5.1   File: Application.java    License: MIT License 5 votes vote down vote up
public static void main(String[] args) {
	ApplicationContext ctx = SpringApplication.run(Application.class, args);
	String[] beanNames = ctx.getBeanDefinitionNames();
	Arrays.sort(beanNames);
	for (String beanName : beanNames) {
		System.out.println(beanName);
	}
}
 
Example 15
void setContext(ApplicationContext context) {
    this.context = context;
    String[] beanDefinitionNames = context.getBeanDefinitionNames();
    if (beanDefinitionNames != null && beanDefinitionNames.length > 0) {
        for (String beanName : context.getBeanDefinitionNames()) {
            try {
                addBean(beanName, context.getBean(beanName));
            } catch (Throwable t) {
                // ignore
            }
        }
    }
}
 
Example 16
Source Project: Spring-Framework-Essentials   File: RunDemo.java    License: MIT License 5 votes vote down vote up
public static void main(String[] args) {
    ApplicationContext context =
            new AnnotationConfigApplicationContext(
                    AppConfig.class, AppConfig.class);
    System.out.println(context.getBeanDefinitionCount());
    for (String name : context.getBeanDefinitionNames()) {
        System.out.println(name);
    }
}
 
Example 17
Source Project: boost   File: Application.java    License: Eclipse Public License 1.0 5 votes vote down vote up
@Bean
public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
    return args -> {

        System.out.println("Let's inspect the beans provided by Spring Boot:");

        String[] beanNames = ctx.getBeanDefinitionNames();
        Arrays.sort(beanNames);
        for (String beanName : beanNames) {
            System.out.println(beanName);
        }

    };
}
 
Example 18
/** Displays the names of all Spring beans in the given application context.
 * @param applicationContext
 *            object to access many Spring services
 */
private static void displayAllBeans(final ApplicationContext applicationContext) {
	System.out.println("Application.displayAllBeans:");
	final String[] allBeanNames = applicationContext.getBeanDefinitionNames();
	for (final String beanName : allBeanNames) {
		System.out.println(beanName);
	}
}
 
Example 19
public static void main(String[] args) {
    ApplicationContext ctx = SpringApplication.run(Application.class, args);

    System.out.println("Let's inspect the beans provided by Spring Boot:");

    String[] beanNames = ctx.getBeanDefinitionNames();
    Arrays.sort(beanNames);
    for (String beanName : beanNames) {
        System.out.println(beanName);
    }
}
 
Example 20
Source Project: Mastering-Spring-5.0   File: Application.java    License: MIT License 5 votes vote down vote up
public static void main(String[] args) {
	ApplicationContext ctx = SpringApplication.run(Application.class, args);
	String[] beanNames = ctx.getBeanDefinitionNames();
	Arrays.sort(beanNames);
	for (String beanName : beanNames) {
		System.out.println(beanName);
	}
}