/** @param args */ public static void main(String[] args) { // TODO Auto-generated method stub ApplicationContext bf = SpringApplicationContext.getContext(); System.out.println("isSingleton? " + bf.isSingleton("employee")); Employee employee = (Employee) bf.getBean("employee"); employee.setFirstName("Amit"); System.out.println(employee.toString()); employee.sayHello(); }
public static void main(String[] args) throws PerformanceException { ctx = new ClassPathXmlApplicationContext("p_binding.xml"); Performer performer = (Performer) ctx.getBean("hank"); performer.perform(); Stage stage = (Stage) ctx.getBean("theStage"); Stage stage2 = (Stage) ctx.getBean("theStage"); System.out.println(stage.equals(stage2)); System.out.println(ctx.isSingleton("theStage")); Performer performer2 = (Performer) ctx.getBean("kenny"); performer2.perform(); }
@Test public void logBeansInContext() { DefaultListableBeanFactory factory = (DefaultListableBeanFactory) beanFactory; if (factory != null) { logger.debug("Bean Factory: '{}'", factory); } if (applicationContext.getParent() != null) { logger.debug("Bean Factory: '{}'", applicationContext.getParentBeanFactory()); } logger.debug("******************************************************************************"); String[] beans = applicationContext.getBeanDefinitionNames(); for (String o : beans) { logger.debug("________________________"); logger.debug("BEAN id: '{}'", o); logger.debug("\tType: '{}'", applicationContext.getType(o)); String[] aliases = applicationContext.getAliases(o); if (factory.isFactoryBean(o)) logger.debug("\tFACTORY"); if (aliases != null && aliases.length > 0) { for (String a : aliases) { logger.debug("\tAliased as: '{}'", a); } } if (factory.getBeanDefinition(o).isAbstract()) { logger.debug("\tABSTRACT"); } else { if (applicationContext.isPrototype(o)) logger.debug("\tScope: 'Prototype'"); if (applicationContext.isSingleton(o)) logger.debug("\tScope: 'Singleton'"); Annotation[] annotations = applicationContext.getBean(o).getClass().getAnnotations(); if (annotations != null && annotations.length > 0) { logger.debug("\tAnnotations:"); for (Annotation annotation : annotations) { logger.debug("\t\t'{}'", annotation.annotationType()); } } if (!applicationContext .getBean(o) .toString() .startsWith(applicationContext.getType(o).toString() + "@")) { logger.debug("\tContents: {}", applicationContext.getBean(o).toString()); } } } logger.debug("******************************************************************************"); logger.debug("*** Number of Beans={} ***", applicationContext.getBeanDefinitionCount()); logger.debug("*** Number of Bean Post Processors={} ***", factory.getBeanPostProcessorCount()); logger.debug("******************************************************************************"); }
@Test public void isSingleton() { boolean res = context.isSingleton("person"); logger.debug("res={}", res); }
/** * 判断以给定名字注册的bean定义是一个singleton还是一个prototype。 * 如果与给定名字相应的bean定义没有被找到,将会抛出一个异常(NoSuchBeanDefinitionException) * * @param name * @return boolean * @throws NoSuchBeanDefinitionException */ public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException { return applicationContext.isSingleton(name); }
public boolean isSingleton(String arg0) throws NoSuchBeanDefinitionException { return applicationContext.isSingleton(arg0); }