com.github.edgarespina.mwa.Beans Maven / Gradle / Ivy
The newest version!
package com.github.edgarespina.mwa;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
/**
* Helper class for beans.
*
* @author edgar.espina
* @since 0.1.3
*/
public final class Beans {
/**
* Not allowed.
*/
private Beans() {
}
/**
* Look for bean of an specific type in the Application Context.
*
* @param context The application context.
* @param beanType The bean type to look for.
* @param The bean generic type.
* @return All the of the specific types found in the Application Context.
*/
public static List lookFor(final ApplicationContext context,
final Class beanType) {
Collection beans = context.getBeansOfType(beanType).values();
List result = new ArrayList();
if (beans != null) {
result.addAll(beans);
}
return result;
}
/**
* Look for bean of an specific type in the Application Context.
*
* @param context The application context.
* @param beanType The bean type to look for.
* @param The bean generic type.
* @return The matching bean or null.
*/
public static T get(final ApplicationContext context,
final Class beanType) {
try {
return context.getBean(beanType);
} catch (BeansException ex) {
return null;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy