List of usage examples for org.springframework.context ConfigurableApplicationContext getBeanNamesForType
String[] getBeanNamesForType(ResolvableType type);
From source file:com.example.config.ApplicationBuilder.java
private static boolean hasListeners(ConfigurableApplicationContext context) { if (context.getBeanNamesForType(ShutdownApplicationListener.class).length != 0) { return true; }/*ww w. jav a 2s . co m*/ if (context instanceof AbstractApplicationContext) { if (((AbstractApplicationContext) context).getApplicationListeners().stream() .anyMatch(l -> l instanceof ShutdownApplicationListener)) { return true; } } if ((DefaultListableBeanFactory) context.getBeanFactory().getSingleton(SHUTDOWN_LISTENER) != null) { return true; } return false; }
From source file:grails.util.RunTests.java
public static void main(String[] args) { int exitCode = 0; try {//from w w w. ja v a 2s . co m log.info("Bootstrapping Grails from classpath"); ConfigurableApplicationContext appCtx = (ConfigurableApplicationContext) GrailsUtil .bootstrapGrailsFromClassPath(); GrailsApplication application = (GrailsApplication) appCtx.getBean(GrailsApplication.APPLICATION_ID); Class[] allClasses = application.getAllClasses(); log.debug("Going through [" + allClasses.length + "] classes"); TestSuite s = new TestSuite(); for (int i = 0; i < allClasses.length; i++) { Class clazz = allClasses[i]; if (TestCase.class.isAssignableFrom(clazz) && !Modifier.isAbstract(clazz.getModifiers())) { log.debug("Adding test [" + clazz.getName() + "]"); s.addTest(new GrailsTestSuite(appCtx, clazz)); } else { log.debug("[" + clazz.getName() + "] is not a test case."); } } String[] beanNames = appCtx.getBeanNamesForType(PersistenceContextInterceptor.class); PersistenceContextInterceptor interceptor = null; if (beanNames.length > 0) { interceptor = (PersistenceContextInterceptor) appCtx.getBean(beanNames[0]); } try { if (interceptor != null) { interceptor.init(); } TestResult r = TestRunner.run(s); exitCode = r.errorCount() + r.failureCount(); if (exitCode > 0) { System.err.println("Tests failed!"); } if (interceptor != null) interceptor.flush(); } finally { if (interceptor != null) interceptor.destroy(); } } catch (Exception e) { log.error("Error executing tests: " + e.getMessage(), e); exitCode = 1; } finally { System.exit(exitCode); } }
From source file:org.springframework.batch.core.configuration.support.DefaultJobLoader.java
@SuppressWarnings("resource") private Collection<Job> doLoad(ApplicationContextFactory factory, boolean unregister) throws DuplicateJobException { Collection<String> jobNamesBefore = jobRegistry.getJobNames(); ConfigurableApplicationContext context = factory.createApplicationContext(); Collection<String> jobNamesAfter = jobRegistry.getJobNames(); // Try to detect auto-registration (e.g. through a bean post processor) boolean autoRegistrationDetected = jobNamesAfter.size() > jobNamesBefore.size(); Collection<String> jobsRegistered = new HashSet<String>(); if (autoRegistrationDetected) { for (String name : jobNamesAfter) { if (!jobNamesBefore.contains(name)) { jobsRegistered.add(name); }// www . j ava 2s .c o m } } contexts.put(factory, context); String[] names = context.getBeanNamesForType(Job.class); for (String name : names) { if (!autoRegistrationDetected) { Job job = (Job) context.getBean(name); String jobName = job.getName(); // On reload try to unregister first if (unregister) { if (logger.isDebugEnabled()) { logger.debug( "Unregistering job: " + jobName + " from context: " + context.getDisplayName()); } doUnregister(jobName); } if (logger.isDebugEnabled()) { logger.debug("Registering job: " + jobName + " from context: " + context.getDisplayName()); } doRegister(context, job); jobsRegistered.add(jobName); } } Collection<Job> result = new ArrayList<Job>(); for (String name : jobsRegistered) { try { result.add(jobRegistry.getJob(name)); } catch (NoSuchJobException e) { // should not happen; throw new IllegalStateException("Could not retrieve job that was should have been registered", e); } } contextToJobNames.put(context, jobsRegistered); return result; }
From source file:org.springframework.context.event.EventListenerMethodProcessor.java
@Override public void afterSingletonsInstantiated() { List<EventListenerFactory> factories = getEventListenerFactories(); ConfigurableApplicationContext context = getApplicationContext(); String[] beanNames = context.getBeanNamesForType(Object.class); for (String beanName : beanNames) { if (!ScopedProxyUtils.isScopedTarget(beanName)) { Class<?> type = null; try { type = AutoProxyUtils.determineTargetClass(context.getBeanFactory(), beanName); } catch (Throwable ex) { // An unresolvable bean type, probably from a lazy bean - let's ignore it. if (logger.isDebugEnabled()) { logger.debug("Could not resolve target class for bean with name '" + beanName + "'", ex); }/*from w w w . j a va2 s. co m*/ } if (type != null) { if (ScopedObject.class.isAssignableFrom(type)) { try { Class<?> targetClass = AutoProxyUtils.determineTargetClass(context.getBeanFactory(), ScopedProxyUtils.getTargetBeanName(beanName)); if (targetClass != null) { type = targetClass; } } catch (Throwable ex) { // An invalid scoped proxy arrangement - let's ignore it. if (logger.isDebugEnabled()) { logger.debug("Could not resolve target bean for scoped proxy '" + beanName + "'", ex); } } } try { processBean(factories, beanName, type); } catch (Throwable ex) { throw new BeanInitializationException("Failed to process @EventListener " + "annotation on bean with name '" + beanName + "'", ex); } } } } }