本文整理匯總了Java中org.springframework.beans.factory.ListableBeanFactory.getBean方法的典型用法代碼示例。如果您正苦於以下問題:Java ListableBeanFactory.getBean方法的具體用法?Java ListableBeanFactory.getBean怎麽用?Java ListableBeanFactory.getBean使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類org.springframework.beans.factory.ListableBeanFactory
的用法示例。
在下文中一共展示了ListableBeanFactory.getBean方法的9個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: main
import org.springframework.beans.factory.ListableBeanFactory; //導入方法依賴的package包/類
/**
* @throws SQLException If Setup of emulation database failed, or if the
* application JDBC work fails.
*/
static public void main(String[] sa) throws SQLException {
if (sa.length != 1) throw new IllegalArgumentException(SYNTAX_MSG);
String authSpringFile = null;
if (sa[0].equals("LDAP")) {
authSpringFile = "ldapbeans.xml";
} else if (sa[0].equals("JAAS")) {
authSpringFile = "jaasbeans.xml";
} else if (sa[0].equals("HsqldbSlave")) {
authSpringFile = "slavebeans.xml";
} else if (sa[0].equals("JAAS_LDAP")) {
authSpringFile = "jaasldapbeans.xml";
}
if (authSpringFile == null)
throw new IllegalArgumentException(SYNTAX_MSG);
SpringExtAuth.prepMemoryDatabases(!sa[0].equals("HsqldbSlave"));
ApplicationContext ctx =
new ClassPathXmlApplicationContext("beandefs.xml", authSpringFile);
ListableBeanFactory bf = (ListableBeanFactory) ctx;
JdbcAppClass appBean = bf.getBean("appBean", JdbcAppClass.class);
appBean.doJdbcWork();
}
示例2: findScheduler
import org.springframework.beans.factory.ListableBeanFactory; //導入方法依賴的package包/類
protected Scheduler findScheduler(String schedulerName) throws SchedulerException {
if (this.beanFactory instanceof ListableBeanFactory) {
ListableBeanFactory lbf = (ListableBeanFactory) this.beanFactory;
String[] beanNames = lbf.getBeanNamesForType(Scheduler.class);
for (String beanName : beanNames) {
Scheduler schedulerBean = (Scheduler) lbf.getBean(beanName);
if (schedulerName.equals(schedulerBean.getSchedulerName())) {
return schedulerBean;
}
}
}
Scheduler schedulerInRepo = SchedulerRepository.getInstance().lookup(schedulerName);
if (schedulerInRepo == null) {
throw new IllegalStateException("No Scheduler named '" + schedulerName + "' found");
}
return schedulerInRepo;
}
示例3: findEntityManagerFactory
import org.springframework.beans.factory.ListableBeanFactory; //導入方法依賴的package包/類
/**
* Find an EntityManagerFactory with the given name in the given
* Spring application context (represented as ListableBeanFactory).
* <p>The specified unit name will be matched against the configured
* persistence unit, provided that a discovered EntityManagerFactory
* implements the {@link EntityManagerFactoryInfo} interface. If not,
* the persistence unit name will be matched against the Spring bean name,
* assuming that the EntityManagerFactory bean names follow that convention.
* <p>If no unit name has been given, this method will search for a default
* EntityManagerFactory through {@link ListableBeanFactory#getBean(Class)}.
* @param beanFactory the ListableBeanFactory to search
* @param unitName the name of the persistence unit (may be {@code null} or empty,
* in which case a single bean of type EntityManagerFactory will be searched for)
* @return the EntityManagerFactory
* @throws NoSuchBeanDefinitionException if there is no such EntityManagerFactory in the context
* @see EntityManagerFactoryInfo#getPersistenceUnitName()
*/
public static EntityManagerFactory findEntityManagerFactory(
ListableBeanFactory beanFactory, String unitName) throws NoSuchBeanDefinitionException {
Assert.notNull(beanFactory, "ListableBeanFactory must not be null");
if (StringUtils.hasLength(unitName)) {
// See whether we can find an EntityManagerFactory with matching persistence unit name.
String[] candidateNames =
BeanFactoryUtils.beanNamesForTypeIncludingAncestors(beanFactory, EntityManagerFactory.class);
for (String candidateName : candidateNames) {
EntityManagerFactory emf = (EntityManagerFactory) beanFactory.getBean(candidateName);
if (emf instanceof EntityManagerFactoryInfo) {
if (unitName.equals(((EntityManagerFactoryInfo) emf).getPersistenceUnitName())) {
return emf;
}
}
}
// No matching persistence unit found - simply take the EntityManagerFactory
// with the persistence unit name as bean name (by convention).
return beanFactory.getBean(unitName, EntityManagerFactory.class);
}
else {
// Find unique EntityManagerFactory bean in the context, falling back to parent contexts.
return beanFactory.getBean(EntityManagerFactory.class);
}
}
示例4: configureDatastoreResolvers
import org.springframework.beans.factory.ListableBeanFactory; //導入方法依賴的package包/類
/**
* Register {@link DatastoreResolver} annotated beans as <code>datastore</code> {@link ExpressionResolver}s.
* @param datastore Datastore to configure
* @param datastoreBeanName Datastore bean name to configure
* @param beanFactory Bean factory
*/
private static void configureDatastoreResolvers(Datastore datastore, String datastoreBeanName,
ListableBeanFactory beanFactory) {
final String[] beanNames = beanFactory.getBeanNamesForAnnotation(DatastoreResolver.class);
if (beanNames != null && beanNames.length > 0) {
for (String beanName : beanNames) {
if (!ExpressionResolver.class.isAssignableFrom(beanFactory.getType(beanName))) {
throw new BeanNotOfRequiredTypeException(beanName, ExpressionResolver.class,
beanFactory.getType(beanName));
}
DatastoreResolver dr = beanFactory.findAnnotationOnBean(beanName, DatastoreResolver.class);
String datastoreRef = AnnotationUtils.getStringValue(dr.datastoreBeanName());
if (datastoreRef == null || datastoreRef.equals(datastoreBeanName)) {
// register resolver
ExpressionResolver<?, ?> resolver = (ExpressionResolver<?, ?>) beanFactory.getBean(beanName);
datastore.addExpressionResolver(resolver);
LOGGER.debug(() -> "Registered expression resolver [" + resolver.getClass().getName()
+ "] into Datastore with bean name [" + datastoreBeanName + "]");
}
}
}
}
示例5: getBean
import org.springframework.beans.factory.ListableBeanFactory; //導入方法依賴的package包/類
/**
* Attempts to find a Spring bean based of the specified class. This method first attempts to load a bean
* following the default Spring auto-naming conventions (de-capitalizing class simple name). In case the bean with
* this name can't be found, the method returns the first available bean of this class.
* <p/>
* Such logic is required in order to enable this method to lookup components from legacy contexts. "Older" beans seems to
* have identifiers hard-coded in the bean definition file, while "newer" bean identifiers are auto-generated by Spring based on the
* annotations.
*
* @param clazz
* Class of the bean to be looked up
* @return
* Returns the bean instance
* @throws NoSuchBeanDefinitionException if there is no bean definition with the specified name
*/
@SuppressWarnings("unchecked")
public static <T> T getBean(Class<?> clazz) {
// legacy code - I wonder if it's necessary since we are looking up a bean based directly on the class name anyways
// but to keep legacy logic working properly attempt to locate component based on the Spring conventions
String className = WordUtils.uncapitalize(clazz.getSimpleName());
if (beanFactory.containsBean(className))
return (T) beanFactory.getBean(className);
if (ListableBeanFactory.class.isAssignableFrom(beanFactory.getClass())) {
ListableBeanFactory listableBeanFactory = (ListableBeanFactory) beanFactory;
String[] beanNames = listableBeanFactory.getBeanNamesForType(clazz);
if (beanNames.length > 0)
return (T) listableBeanFactory.getBean(beanNames[0]);
}
throw new NoSuchBeanDefinitionException(clazz);
}
示例6: getOrderedBeansOfType
import org.springframework.beans.factory.ListableBeanFactory; //導入方法依賴的package包/類
private <T> List<Entry<String, T>> getOrderedBeansOfType(
ListableBeanFactory beanFactory, Class<T> type, Set<?> excludes) {
List<Entry<String, T>> beans = new ArrayList<Entry<String, T>>();
Comparator<Entry<String, T>> comparator = new Comparator<Entry<String, T>>() {
@Override
public int compare(Entry<String, T> o1, Entry<String, T> o2) {
return AnnotationAwareOrderComparator.INSTANCE.compare(o1.getValue(),
o2.getValue());
}
};
String[] names = beanFactory.getBeanNamesForType(type, true, false);
Map<String, T> map = new LinkedHashMap<String, T>();
for (String name : names) {
if (!excludes.contains(name)) {
T bean = beanFactory.getBean(name, type);
if (!excludes.contains(bean)) {
map.put(name, bean);
}
}
}
beans.addAll(map.entrySet());
Collections.sort(beans, comparator);
return beans;
}
開發者ID:vikrammane23,項目名稱:https-github.com-g0t4-jenkins2-course-spring-boot,代碼行數:27,代碼來源:ServletContextInitializerBeans.java
示例7: loadFactories
import org.springframework.beans.factory.ListableBeanFactory; //導入方法依賴的package包/類
private Map<String, JobFactory> loadFactories(ListableBeanFactory beanFactory) {
ImmutableMap.Builder<String, JobFactory> mb = ImmutableMap.builder();
//load factory beans
String[] factoryNames = beanFactory.getBeanNamesForType(JobFactory.class);
for(String factoryName: factoryNames) {
JobFactory factory = beanFactory.getBean(factoryName, JobFactory.class);
if(factory == this) {
// we do not want to load self
continue;
}
Set<String> types = factory.getTypes();
for(String type: types) {
mb.put(type, factory);
}
}
//load job beans
String[] jobNames = beanFactory.getBeanNamesForAnnotation(JobBean.class);
for(String jobName: jobNames) {
Class<?> jobType = beanFactory.getType(jobName);
JobDescription jd = descFactory.getFor(jobName);
mb.put(jobName, new JobFactoryForBean(this, jobName, jobType, jd));
}
return mb.build();
}
示例8: configureDatastoreCommodityFactories
import org.springframework.beans.factory.ListableBeanFactory; //導入方法依賴的package包/類
/**
* Register {@link com.holonplatform.spring.DatastoreCommodityFactory} annotated beans as <code>datastore</code>
* {@link DatastoreCommodityFactory}s.
* @param datastore Datastore to configure
* @param datastoreBeanName Datastore bean name to configure
* @param beanFactory Bean factory
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
private static void configureDatastoreCommodityFactories(Datastore datastore, String datastoreBeanName,
ListableBeanFactory beanFactory) {
if (datastore instanceof DatastoreCommodityRegistrar) {
final Class<? extends DatastoreCommodityFactory> baseType = ((DatastoreCommodityRegistrar<?>) datastore)
.getCommodityFactoryType();
if (baseType != null) {
final String[] beanNames = beanFactory
.getBeanNamesForAnnotation(com.holonplatform.spring.DatastoreCommodityFactory.class);
if (beanNames != null && beanNames.length > 0) {
for (String beanName : beanNames) {
com.holonplatform.spring.DatastoreCommodityFactory dr = beanFactory.findAnnotationOnBean(
beanName, com.holonplatform.spring.DatastoreCommodityFactory.class);
String datastoreRef = AnnotationUtils.getStringValue(dr.datastoreBeanName());
if (datastoreRef == null || datastoreRef.equals(datastoreBeanName)) {
if (!baseType.isAssignableFrom(beanFactory.getType(beanName))) {
throw new BeanNotOfRequiredTypeException(beanName, baseType,
beanFactory.getType(beanName));
}
// register resolver
DatastoreCommodityFactory datastoreCommodityFactory = (DatastoreCommodityFactory) beanFactory
.getBean(beanName);
((DatastoreCommodityRegistrar) datastore).registerCommodity(datastoreCommodityFactory);
LOGGER.debug(() -> "Registered factory [" + datastoreCommodityFactory.getClass().getName()
+ "] into Datastore with bean name [" + datastoreBeanName + "]");
}
}
}
} else {
LOGGER.debug(
() -> "Datastore [" + datastore + "] does not declares a DatastoreCommodityFactory base type: "
+ "skipping DatastoreCommodityFactory beans configuration");
}
} else {
LOGGER.debug(() -> "Datastore [" + datastore
+ "] is not a DatastoreCommodityRegistrar: skipping DatastoreCommodityFactory beans configuration");
}
}
示例9: vaultTokenSupplier
import org.springframework.beans.factory.ListableBeanFactory; //導入方法依賴的package包/類
/**
* @return the {@link VaultTokenSupplier} for reactive Vault session management
* adapting {@link ClientAuthentication} that also implement
* {@link AuthenticationStepsFactory}.
* @see AuthenticationStepsFactory
*/
@Bean
@ConditionalOnMissingBean(name = "vaultTokenSupplier")
public VaultTokenSupplier vaultTokenSupplier(ListableBeanFactory beanFactory) {
Assert.notNull(beanFactory, "BeanFactory must not be null");
String[] authStepsFactories = beanFactory
.getBeanNamesForType(AuthenticationStepsFactory.class);
if (!ObjectUtils.isEmpty(authStepsFactories)) {
AuthenticationStepsFactory factory = beanFactory
.getBean(AuthenticationStepsFactory.class);
return createAuthenticationStepsOperator(factory);
}
String[] clientAuthentications = beanFactory
.getBeanNamesForType(ClientAuthentication.class);
if (!ObjectUtils.isEmpty(clientAuthentications)) {
ClientAuthentication clientAuthentication = beanFactory
.getBean(ClientAuthentication.class);
if (clientAuthentication instanceof TokenAuthentication) {
TokenAuthentication authentication = (TokenAuthentication) clientAuthentication;
return () -> Mono.just(authentication.login());
}
if (clientAuthentication instanceof AuthenticationStepsFactory) {
return createAuthenticationStepsOperator((AuthenticationStepsFactory) clientAuthentication);
}
throw new IllegalStateException(
String.format(
"Cannot construct VaultTokenSupplier from %s. "
+ "ClientAuthentication must implement AuthenticationStepsFactory or be TokenAuthentication",
clientAuthentication));
}
throw new IllegalStateException(
"Cannot construct VaultTokenSupplier. Please configure VaultTokenSupplier bean named vaultTokenSupplier.");
}