Example usage for org.springframework.context ApplicationContext getBeansOfType

List of usage examples for org.springframework.context ApplicationContext getBeansOfType

Introduction

In this page you can find the example usage for org.springframework.context ApplicationContext getBeansOfType.

Prototype

<T> Map<String, T> getBeansOfType(@Nullable Class<T> type) throws BeansException;

Source Link

Document

Return the bean instances that match the given object type (including subclasses), judging from either bean definitions or the value of getObjectType in the case of FactoryBeans.

Usage

From source file:org.springframework.web.servlet.resource.PublicResourceUrlProvider.java

protected void detectResourceHandlers(ApplicationContext applicationContext) {
    logger.debug("Looking for resource handler mappings");
    Map<String, SimpleUrlHandlerMapping> beans = applicationContext
            .getBeansOfType(SimpleUrlHandlerMapping.class);
    for (SimpleUrlHandlerMapping hm : beans.values()) {
        for (String pattern : hm.getUrlMap().keySet()) {
            Object handler = hm.getUrlMap().get(pattern);
            if (handler instanceof ResourceHttpRequestHandler) {
                ResourceHttpRequestHandler resourceHandler = (ResourceHttpRequestHandler) handler;
                if (logger.isDebugEnabled()) {
                    logger.debug("Found pattern=\"" + pattern + "\" mapped to locations "
                            + resourceHandler.getLocations() + " with resolvers: "
                            + resourceHandler.getResourceResolvers());
                }//from w  w w.jav a2s  . co  m
                this.handlerMap.put(pattern, resourceHandler);
            }
        }
    }
}

From source file:org.springframework.web.servlet.resource.ResourceUrlProvider.java

protected void detectResourceHandlers(ApplicationContext appContext) {
    logger.debug("Looking for resource handler mappings");

    Map<String, SimpleUrlHandlerMapping> beans = appContext.getBeansOfType(SimpleUrlHandlerMapping.class);
    List<SimpleUrlHandlerMapping> mappings = new ArrayList<>(beans.values());
    AnnotationAwareOrderComparator.sort(mappings);

    for (SimpleUrlHandlerMapping mapping : mappings) {
        for (String pattern : mapping.getHandlerMap().keySet()) {
            Object handler = mapping.getHandlerMap().get(pattern);
            if (handler instanceof ResourceHttpRequestHandler) {
                ResourceHttpRequestHandler resourceHandler = (ResourceHttpRequestHandler) handler;
                if (logger.isDebugEnabled()) {
                    logger.debug("Found resource handler mapping: URL pattern=\"" + pattern + "\", "
                            + "locations=" + resourceHandler.getLocations() + ", " + "resolvers="
                            + resourceHandler.getResourceResolvers());
                }//  w  w w. ja v a  2  s.c o m
                this.handlerMap.put(pattern, resourceHandler);
            }
        }
    }
}

From source file:org.unitils.database.core.impl.SpringApplicationContextDataSourceProvider.java

@SuppressWarnings("unchecked")
protected Map<String, DataSourceWrapper> createDataSourceWrappers(ApplicationContext applicationContext) {
    Map<String, DataSourceWrapper> dataSourceWrappers = new HashMap<String, DataSourceWrapper>(3);

    Map<String, UnitilsDataSourceBean> unitilsDataSourceBeans = applicationContext
            .getBeansOfType(UnitilsDataSourceBean.class);
    if (unitilsDataSourceBeans.isEmpty()) {
        throw new UnitilsException("No beans of type UnitilsDataSourceBean found in test application context.");
    }//  ww  w .j  a v a 2 s. c o m
    for (Map.Entry<String, UnitilsDataSourceBean> entry : unitilsDataSourceBeans.entrySet()) {
        String databaseName = entry.getKey();
        UnitilsDataSourceBean unitilsDataSourceBean = entry.getValue();

        boolean defaultDatabase = unitilsDataSourceBean.isDefaultDatabase();
        if (unitilsDataSourceBeans.size() == 1) {
            defaultDatabase = true;
        }
        DataSourceWrapper dataSourceWrapper = createDataSourceWrapper(databaseName, defaultDatabase,
                unitilsDataSourceBean);
        dataSourceWrappers.put(databaseName, dataSourceWrapper);
    }
    return dataSourceWrappers;
}

From source file:org.unitils.database.transaction.impl.SpringApplicationContextTransactionProvider.java

@SuppressWarnings("unchecked")
protected PlatformTransactionManager getPlatformTransactionManagerFromApplicationContext(
        String transactionManagerName, ApplicationContext applicationContext) {
    Map<String, PlatformTransactionManager> platformTransactionManagers = applicationContext
            .getBeansOfType(PlatformTransactionManager.class);
    if (platformTransactionManagers.isEmpty()) {
        return null;
    }//from w w  w. j  a va  2 s.co  m
    if (isBlank(transactionManagerName)) {
        if (platformTransactionManagers.size() > 1) {
            throw new UnitilsException(
                    "Unable to get default platform transaction manager from application context. More than one bean of type PlatformTransactionManager found in application context. Please specify the id of the transaction manager explicitly.");
        }
        return platformTransactionManagers.values().iterator().next();
    }
    PlatformTransactionManager platformTransactionManager = platformTransactionManagers
            .get(transactionManagerName);
    if (platformTransactionManager == null) {
        throw new UnitilsException(
                "Unable to get platform transaction manager from application context. No bean of type PlatformTransactionManager with id '"
                        + transactionManagerName + "' found in application context.");
    }
    return platformTransactionManager;
}

From source file:org.unitils.spring.SpringModule.java

/**
 * No after initialization needed for this module
 *///from   w  ww.jav a  2s.  c om
public void afterInit() {
    // Make sure that, if a custom transaction manager is configured in the spring ApplicationContext associated with
    // the current test, it is used for managing transactions. 
    if (isDatabaseModuleEnabled()) {
        getDatabaseModule()
                .registerTransactionManagementConfiguration(new UnitilsTransactionManagementConfiguration() {

                    public boolean isApplicableFor(Object testObject) {
                        if (!isApplicationContextConfiguredFor(testObject)) {
                            return false;
                        }
                        ApplicationContext context = getApplicationContext(testObject);
                        return context.getBeansOfType(getPlatformTransactionManagerClass()).size() != 0;
                    }

                    @SuppressWarnings("unchecked")
                    public PlatformTransactionManager getSpringPlatformTransactionManager(Object testObject) {
                        ApplicationContext context = getApplicationContext(testObject);
                        Class<?> platformTransactionManagerClass = getPlatformTransactionManagerClass();
                        Map<String, PlatformTransactionManager> platformTransactionManagers = (Map<String, PlatformTransactionManager>) context
                                .getBeansOfType(platformTransactionManagerClass);
                        if (platformTransactionManagers.size() == 0) {
                            throw new UnitilsException("Could not find a bean of type "
                                    + platformTransactionManagerClass.getSimpleName()
                                    + " in the spring ApplicationContext for this class");
                        }
                        if (platformTransactionManagers.size() > 1) {
                            Method testMethod = Unitils.getInstance().getTestContext().getTestMethod();
                            String transactionManagerName = getMethodOrClassLevelAnnotationProperty(
                                    Transactional.class, "transactionManagerName", "", testMethod,
                                    testObject.getClass());
                            if (isEmpty(transactionManagerName))
                                throw new UnitilsException("Found more than one bean of type "
                                        + platformTransactionManagerClass.getSimpleName()
                                        + " in the spring ApplicationContext for this class. Use the transactionManagerName on the @Transactional"
                                        + " annotation to select the correct one.");
                            if (!platformTransactionManagers.containsKey(transactionManagerName))
                                throw new UnitilsException(
                                        "No bean of type " + platformTransactionManagerClass.getSimpleName()
                                                + " found in the spring ApplicationContext with the name "
                                                + transactionManagerName);
                            return platformTransactionManagers.get(transactionManagerName);
                        }
                        return platformTransactionManagers.values().iterator().next();
                    }

                    public boolean isTransactionalResourceAvailable(Object testObject) {
                        return true;
                    }

                    public Integer getPreference() {
                        return 20;
                    }

                    protected Class<?> getPlatformTransactionManagerClass() {
                        return ReflectionUtils
                                .getClassWithName("org.springframework.transaction.PlatformTransactionManager");
                    }

                });
    }
}