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

The following examples show how to use org.springframework.context.ApplicationContext#getBeanDefinitionNames() . 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 File: CIBeansTest.java    From Spring with 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 2
Source File: PostProxyInvokerContextListener.java    From Spring with Apache License 2.0 6 votes vote down vote up
@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 3
Source File: CalendarApplication.java    From Spring-Security-Third-Edition with MIT License 6 votes vote down vote up
@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 File: CIBeansTest.java    From Spring with 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 5
Source File: SpringBeanLocator.java    From cxf with 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 6
Source File: SpringbootIn10StepsApplication.java    From in28minutes-spring-microservices with MIT License 5 votes vote down vote up
public static void main(String[] args) {
	ApplicationContext applicationContext = 
			SpringApplication.run(SpringbootIn10StepsApplication.class, args);
	
	for (String name : applicationContext.getBeanDefinitionNames()) {
		System.out.println(name);
	}
}
 
Example 7
Source File: Application.java    From Mastering-Spring-5.0 with 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 8
Source File: Application.java    From docker-kubernetes-by-example-java with 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 9
Source File: Application.java    From spring-ddd-bank with GNU Lesser General Public License v2.1 5 votes vote down vote up
/** 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 10
Source File: Application.java    From boost with 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 11
Source File: RunDemo.java    From Spring-Framework-Essentials with 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 12
Source File: SpringContextContainer.java    From jvm-sandbox-repeater with Apache License 2.0 5 votes vote down vote up
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 13
Source File: Application.java    From Mastering-Spring-5.1 with 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 14
Source File: RepositoryPluginHandler.java    From gravitee-management-rest-api with Apache License 2.0 5 votes vote down vote up
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 15
Source File: IOCTest.java    From code with 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 16
Source File: RunDemo.java    From Spring-Framework-Essentials with 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 File: Application.java    From xxproject with 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 18
Source File: CalendarApplication.java    From Spring-Security-Third-Edition with MIT License 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:\n");

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

        System.out.println("---");
    };
}
 
Example 19
Source File: Application.java    From JGiven with 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 20
Source File: CtxCommandLine.java    From spring-boot-cookbook with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
    };
}