use of org.springframework.beans.factory.ListableBeanFactory in project spring-framework by spring-projects.
the class TestContextTransactionUtils method retrieveTransactionManager.
/**
* Retrieve the {@linkplain PlatformTransactionManager transaction manager}
* to use for the supplied {@linkplain TestContext test context}.
* <p>The following algorithm is used to retrieve the transaction manager
* from the {@link org.springframework.context.ApplicationContext ApplicationContext}
* of the supplied test context:
* <ol>
* <li>Look up the transaction manager by type and explicit name, if the supplied
* {@code name} is non-empty, throwing a {@link BeansException} if the named
* transaction manager does not exist.
* <li>Attempt to look up the single transaction manager by type.
* <li>Attempt to look up the <em>primary</em> transaction manager by type.
* <li>Attempt to look up the transaction manager via a
* {@link TransactionManagementConfigurer}, if present.
* <li>Attempt to look up the transaction manager by type and the
* {@linkplain #DEFAULT_TRANSACTION_MANAGER_NAME default transaction manager
* name}.
* @param testContext the test context for which the transaction manager
* should be retrieved; never {@code null}
* @param name the name of the transaction manager to retrieve; may be
* {@code null} or <em>empty</em>
* @return the transaction manager to use, or {@code null} if not found
* @throws BeansException if an error occurs while retrieving an explicitly
* named transaction manager
* @throws IllegalStateException if more than one TransactionManagementConfigurer
* exists in the ApplicationContext
*/
public static PlatformTransactionManager retrieveTransactionManager(TestContext testContext, String name) {
Assert.notNull(testContext, "TestContext must not be null");
BeanFactory bf = testContext.getApplicationContext().getAutowireCapableBeanFactory();
try {
// look up by type and explicit name
if (StringUtils.hasText(name)) {
return bf.getBean(name, PlatformTransactionManager.class);
}
} catch (BeansException ex) {
logger.error(String.format("Failed to retrieve transaction manager named '%s' for test context %s", name, testContext), ex);
throw ex;
}
try {
if (bf instanceof ListableBeanFactory) {
ListableBeanFactory lbf = (ListableBeanFactory) bf;
// look up single bean by type
Map<String, PlatformTransactionManager> txMgrs = BeanFactoryUtils.beansOfTypeIncludingAncestors(lbf, PlatformTransactionManager.class);
if (txMgrs.size() == 1) {
return txMgrs.values().iterator().next();
}
try {
// look up single bean by type, with support for 'primary' beans
return bf.getBean(PlatformTransactionManager.class);
} catch (BeansException ex) {
logBeansException(testContext, ex, PlatformTransactionManager.class);
}
// look up single TransactionManagementConfigurer
Map<String, TransactionManagementConfigurer> configurers = BeanFactoryUtils.beansOfTypeIncludingAncestors(lbf, TransactionManagementConfigurer.class);
Assert.state(configurers.size() <= 1, "Only one TransactionManagementConfigurer may exist in the ApplicationContext");
if (configurers.size() == 1) {
return configurers.values().iterator().next().annotationDrivenTransactionManager();
}
}
// look up by type and default name
return bf.getBean(DEFAULT_TRANSACTION_MANAGER_NAME, PlatformTransactionManager.class);
} catch (BeansException ex) {
logBeansException(testContext, ex, PlatformTransactionManager.class);
return null;
}
}
use of org.springframework.beans.factory.ListableBeanFactory in project spring-framework by spring-projects.
the class BeanTypeNotPresentCondition method matches.
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
ListableBeanFactory factory = context.getBeanFactory();
String[] names = BeanFactoryUtils.beanNamesForTypeIncludingAncestors(factory, this.beanType, false, false);
if (ObjectUtils.isEmpty(names)) {
logger.debug("No bean of type [" + this.beanType + "]. Conditional configuration applies.");
return true;
} else {
logger.debug("Found bean of type [" + this.beanType + "]. Conditional configuration does not apply.");
return false;
}
}
use of org.springframework.beans.factory.ListableBeanFactory in project spring-security by spring-projects.
the class ClientApplication method main.
public static void main(String[] args) {
String username = System.getProperty("username", "");
String password = System.getProperty("password", "");
String nrOfCallsString = System.getProperty("nrOfCalls", "");
if ("".equals(username) || "".equals(password)) {
System.out.println("You need to specify the user ID to use, the password to use, and optionally a number of calls " + "using the username, password, and nrOfCalls system properties respectively. eg for user rod, " + "use: -Dusername=rod -Dpassword=koala' for a single call per service and " + "use: -Dusername=rod -Dpassword=koala -DnrOfCalls=10 for ten calls per service.");
System.exit(-1);
} else {
int nrOfCalls = 1;
if (!"".equals(nrOfCallsString)) {
nrOfCalls = Integer.parseInt(nrOfCallsString);
}
ListableBeanFactory beanFactory = new FileSystemXmlApplicationContext("clientContext.xml");
ClientApplication client = new ClientApplication(beanFactory);
client.invokeContactManager(new UsernamePasswordAuthenticationToken(username, password), nrOfCalls);
System.exit(0);
}
}
use of org.springframework.beans.factory.ListableBeanFactory in project spring-security by spring-projects.
the class UserDetailsServiceFactoryBean method getBeansOfType.
private Map<String, ?> getBeansOfType(Class<?> type) {
Map<String, ?> beans = beanFactory.getBeansOfType(type);
// Check ancestor bean factories if they exist and the current one has none of the
// required type
BeanFactory parent = beanFactory.getParentBeanFactory();
while (parent != null && beans.size() == 0) {
if (parent instanceof ListableBeanFactory) {
beans = ((ListableBeanFactory) parent).getBeansOfType(type);
}
if (parent instanceof HierarchicalBeanFactory) {
parent = ((HierarchicalBeanFactory) parent).getParentBeanFactory();
} else {
break;
}
}
return beans;
}
use of org.springframework.beans.factory.ListableBeanFactory in project ignite by apache.
the class GridJobLoadTest method startNode.
/**
* Starts new grid node.
*
* @param igniteInstanceName name of new node.
* @param springCfg file with spring configuration to use for this node.
* @return a grid instance local to new node {@link org.apache.ignite.Ignition#start(org.apache.ignite.configuration.IgniteConfiguration)}.
* @throws Exception if node run failed.
*/
protected Ignite startNode(String igniteInstanceName, File springCfg) throws Exception {
assert springCfg != null;
ListableBeanFactory springCtx = new FileSystemXmlApplicationContext("file:///" + springCfg.getAbsolutePath());
Map cfgMap = springCtx.getBeansOfType(IgniteConfiguration.class);
assert cfgMap != null;
assert !cfgMap.isEmpty();
IgniteConfiguration cfg = (IgniteConfiguration) cfgMap.values().iterator().next();
cfg.setIgniteInstanceName(igniteInstanceName + "-" + getNextNodeNum());
return G.start(cfg);
}
Aggregations