Package org.togglz.spring.spi

Source Code of org.togglz.spring.spi.SpringBeanFinder

package org.togglz.spring.spi;

import java.util.ArrayList;
import java.util.Collection;

import javax.servlet.ServletContext;

import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.togglz.core.spi.BeanFinder;

public class SpringBeanFinder implements BeanFinder {

    @Override
    public <T> Collection<T> find(Class<T> clazz, Object context) {

        Collection<T> result = new ArrayList<T>();

        // use the Spring API to obtain the WebApplicationContext
        WebApplicationContext applicationContext = null;
        if (context instanceof ServletContext) {
            applicationContext = WebApplicationContextUtils.getWebApplicationContext((ServletContext) context);
        }
        if (applicationContext == null) {
            applicationContext = ContextLoader.getCurrentWebApplicationContext();
        }

        // may be null if Spring hasn't started yet
        if (applicationContext != null) {

            // ask spring about beans of this type
            result.addAll(applicationContext.getBeansOfType(clazz).values());

        }

        return result;

    }

}
TOP

Related Classes of org.togglz.spring.spi.SpringBeanFinder

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.