下面列出了org.springframework.core.io.support.SpringFactoriesLoader#loadFactories ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@SuppressWarnings("unchecked")
@Substitute
private <T> Collection<T> getSpringFactoriesInstances(Class<T> type, Class<?>[] parameterTypes, Object... args) {
List<T> instances;
if (type.equals(SpringApplicationRunListener.class)) {
instances = (List<T>) Arrays.asList(new EventPublishingRunListener((SpringApplication)(Object)this, new String[0])); // TODO convert args
// Error when using it, and we probably should do that at build time
//AnnotationAwareOrderComparator.sort(instances);
}
else if (type.equals(SpringBootExceptionReporter.class)) {
instances = (List<T>) Arrays.asList(DiagnosticsProvider.getFailureAnalyzers((ConfigurableApplicationContext) args[0])); // Package private
// Error when using it, and we probably should do that at build time
//AnnotationAwareOrderComparator.sort(instances);
}
else {
instances = SpringFactoriesLoader.loadFactories(type, null);
}
return instances;
}
StubRepository(File repository, List<HttpServerStub> httpServerStubs,
StubRunnerOptions options) {
if (!repository.isDirectory()) {
throw new IllegalArgumentException(
"Missing descriptor repository under path [" + repository + "]");
}
this.contractConverters = SpringFactoriesLoader
.loadFactories(ContractConverter.class, null);
if (log.isTraceEnabled()) {
log.trace(
"Found the following contract converters " + this.contractConverters);
}
this.httpServerStubs = httpServerStubs;
this.path = repository;
this.options = options;
this.stubs = stubs();
this.contracts = contracts();
if (log.isTraceEnabled()) {
log.trace("Found the following contracts " + this.contracts);
}
}
@Override
public void starting() {
if (executed.compareAndSet(false, true)) {
List<DefaultConfigFactory> defaultConfigs = SpringFactoriesLoader.loadFactories(DefaultConfigFactory.class, this.getClass().getClassLoader());
Map<String, Object> defaultConfig = new Hashtable<>(1);
for (DefaultConfigFactory defaultConfigFactory : defaultConfigs) {
defaultConfig.putAll(defaultConfigFactory.defaultConfig());
}
springApplication.setDefaultProperties(defaultConfig);
}
}
@Override
public void starting() {
if (executed.compareAndSet(false, true)) {
List<DefaultConfigFactory> defaultConfigs = SpringFactoriesLoader.loadFactories(DefaultConfigFactory.class, this.getClass().getClassLoader());
Map<String, Object> defaultConfig = new Hashtable<>(1);
for (DefaultConfigFactory defaultConfigFactory : defaultConfigs) {
defaultConfig.putAll(defaultConfigFactory.defaultConfig());
}
springApplication.setDefaultProperties(defaultConfig);
}
}
@Override
public void environmentPrepared(ConfigurableEnvironment env) {
if("bootstrap".equals(env.getProperty("spring.config.name"))) {
List<EnvironmentCustomizer> environmentCustomizers =
SpringFactoriesLoader.loadFactories(EnvironmentCustomizer.class, AdminEndpointApplicationRunListener.class.getClassLoader());
if(CollectionUtils.isEmpty(environmentCustomizers)) return;
for(EnvironmentCustomizer customizer: environmentCustomizers) {
customizer.customize(env);
}
}
}
@Override
public void environmentPrepared(ConfigurableEnvironment env) {
if("bootstrap".equals(env.getProperty("spring.config.name"))) {
List<EnvironmentCustomizer> environmentCustomizers =
SpringFactoriesLoader.loadFactories(EnvironmentCustomizer.class, AdminEndpointApplicationRunListener.class.getClassLoader());
if(CollectionUtils.isEmpty(environmentCustomizers)) return;
for(EnvironmentCustomizer customizer: environmentCustomizers) {
customizer.customize(env);
}
}
}
@Test
public void sayHello() throws Exception {
List<OrderService> services = SpringFactoriesLoader.loadFactories(OrderService.class, null);
for (OrderService service : services) {
service.getOrder("worder.");
}
}
@Override
public Object invoke(RpcInvoker invoker, RpcInvocation invocation) {
logger.info("before acquire,"+new Date());
List<AccessLimitService> accessLimitServiceLoader = SpringFactoriesLoader.loadFactories(AccessLimitService.class, null);
if(!CollectionUtils.isEmpty(accessLimitServiceLoader)){
AccessLimitService accessLimitService=accessLimitServiceLoader.get(0);
accessLimitService.acquire(invocation);
}
Object rpcResponse=invoker.invoke(invocation);
logger.info("after acquire,"+new Date());
return rpcResponse;
}
@Test
public void test() {
List<EnvironmentPostProcessor> processorList = SpringFactoriesLoader.loadFactories(EnvironmentPostProcessor.class, getClass().getClassLoader());
Boolean containsApollo = !Collections2.filter(processorList, new Predicate<EnvironmentPostProcessor>() {
@Override
public boolean apply(EnvironmentPostProcessor input) {
return input instanceof ApolloApplicationContextInitializer;
}
}).isEmpty();
Assert.assertTrue(containsApollo);
}
public StubRunner(StubRunnerOptions stubRunnerOptions, String repositoryPath,
StubConfiguration stubsConfiguration,
MessageVerifier<?> contractVerifierMessaging) {
this.stubsConfiguration = stubsConfiguration;
this.stubRunnerOptions = stubRunnerOptions;
List<HttpServerStub> serverStubs = SpringFactoriesLoader
.loadFactories(HttpServerStub.class, null);
this.stubRepository = new StubRepository(new File(repositoryPath), serverStubs,
this.stubRunnerOptions);
AvailablePortScanner portScanner = new AvailablePortScanner(
stubRunnerOptions.getMinPortValue(), stubRunnerOptions.getMaxPortValue());
this.localStubRunner = new StubRunnerExecutor(portScanner,
contractVerifierMessaging, serverStubs);
}
private Extension[] responseTransformers() {
List<WireMockExtensions> wireMockExtensions = SpringFactoriesLoader
.loadFactories(WireMockExtensions.class, null);
List<Extension> extensions = new ArrayList<>();
if (!wireMockExtensions.isEmpty()) {
for (WireMockExtensions wireMockExtension : wireMockExtensions) {
extensions.addAll(wireMockExtension.extensions());
}
}
else {
extensions.add(new DefaultResponseTransformer(false, helpers()));
}
return extensions.toArray(new Extension[extensions.size()]);
}
Loader(ConfigurableEnvironment environment, ResourceLoader resourceLoader) {
this.environment = environment;
this.resourceLoader = resourceLoader == null ? new DefaultResourceLoader()
: resourceLoader;
this.propertySourceLoaders = SpringFactoriesLoader.loadFactories(
PropertySourceLoader.class, getClass().getClassLoader());
}
public MagicPropertySourcePostProcessor() {
this.resourceLoader = new DefaultResourceLoader();
this.propertySourceLoaders = SpringFactoriesLoader.loadFactories(PropertySourceLoader.class, getClass().getClassLoader());
}
@Test
public void test() {
// Should not throw exception
SpringFactoriesLoader.loadFactories(CfEnvProcessor.class, getClass().getClassLoader());
}
private List<PropertySourceLoader> initPropertyLoaders() {
return SpringFactoriesLoader.loadFactories(PropertySourceLoader.class, getClass().getClassLoader());
}
/**
* Get the {@link ContextCustomizerFactory} instances for this bootstrapper.
* <p>The default implementation uses the {@link SpringFactoriesLoader} mechanism
* for loading factories configured in all {@code META-INF/spring.factories}
* files on the classpath.
* @since 4.3
* @see SpringFactoriesLoader#loadFactories
*/
protected List<ContextCustomizerFactory> getContextCustomizerFactories() {
return SpringFactoriesLoader.loadFactories(ContextCustomizerFactory.class, getClass().getClassLoader());
}
/**
* Get the {@link ContextCustomizerFactory} instances for this bootstrapper.
* <p>The default implementation uses the {@link SpringFactoriesLoader} mechanism
* for loading factories configured in all {@code META-INF/spring.factories}
* files on the classpath.
* @since 4.3
* @see SpringFactoriesLoader#loadFactories
*/
protected List<ContextCustomizerFactory> getContextCustomizerFactories() {
return SpringFactoriesLoader.loadFactories(ContextCustomizerFactory.class, getClass().getClassLoader());
}