下面列出了org.springframework.context.annotation.AnnotationConfigApplicationContext#registerBean ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Before
public void setup() {
ArgumentResolverConfigurer resolvers = new ArgumentResolverConfigurer();
resolvers.addCustomResolver(new CustomArgumentResolver());
resolvers.addCustomResolver(new CustomSyncArgumentResolver());
ServerCodecConfigurer codecs = ServerCodecConfigurer.create();
codecs.customCodecs().decoder(new ByteArrayDecoder());
codecs.customCodecs().decoder(new ByteBufferDecoder());
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
applicationContext.registerBean(TestControllerAdvice.class);
applicationContext.refresh();
this.methodResolver = new ControllerMethodResolver(
resolvers, ReactiveAdapterRegistry.getSharedInstance(), applicationContext, codecs.getReaders());
Method method = ResolvableMethod.on(TestController.class).mockCall(TestController::handle).method();
this.handlerMethod = new HandlerMethod(new TestController(), method);
}
@Before
public void setup() {
ArgumentResolverConfigurer resolvers = new ArgumentResolverConfigurer();
resolvers.addCustomResolver(new CustomArgumentResolver());
resolvers.addCustomResolver(new CustomSyncArgumentResolver());
ServerCodecConfigurer codecs = ServerCodecConfigurer.create();
codecs.customCodecs().decoder(new ByteArrayDecoder());
codecs.customCodecs().decoder(new ByteBufferDecoder());
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
applicationContext.registerBean(TestControllerAdvice.class);
applicationContext.refresh();
this.methodResolver = new ControllerMethodResolver(
resolvers, ReactiveAdapterRegistry.getSharedInstance(), applicationContext, codecs.getReaders());
Method method = ResolvableMethod.on(TestController.class).mockCall(TestController::handle).method();
this.handlerMethod = new HandlerMethod(new TestController(), method);
}
protected ApplicationContext createApplicationContext()
{
Plugin springPlugin = (Plugin) getWrapper().getPlugin();
// Create an application context for this plugin using Spring annotated classes starting
// with the plugin class
AnnotationConfigApplicationContext pluginContext = new AnnotationConfigApplicationContext();
pluginContext
.setResourceLoader(new DefaultResourceLoader(getWrapper().getPluginClassLoader()));
pluginContext.registerBean(ExportedComponentPostProcessor.class);
pluginContext.register(springPlugin.getSources().stream().toArray(Class[]::new));
// Attach the plugin application context to the main application context such that it can
// access its beans for auto-wiring
ApplicationContext parent = ((PluginManager) getWrapper().getPluginManager())
.getApplicationContext();
pluginContext.setParent(parent);
// Initialize the context
pluginContext.refresh();
return pluginContext;
}
public static void main(String[] args) throws Exception {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
PostRepository posts = new PostRepository();
PostHandler postHandler = new PostHandler(posts);
Routes routesBean = new Routes(postHandler);
context.registerBean(PostRepository.class, () -> posts);
context.registerBean(PostHandler.class, () -> postHandler);
context.registerBean(Routes.class, () -> routesBean);
context.registerBean(WebHandler.class, () -> RouterFunctions.toWebHandler(routesBean.routes(), HandlerStrategies.builder().build()));
context.refresh();
nettyServer(context).onDispose().block();
}
private static ApplicationContext applicationContext(WebFilter... webFilters) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
for(WebFilter filter : webFilters) {
context.registerBean(WebFilter.class, () -> filter);
}
context.registerBean("webHandler", DispatcherHandler.class, () -> new DispatcherHandler());
context.registerBean(HandlerMapping.class, () -> RouterFunctions.toHandlerMapping(request -> Mono.just(r -> ServerResponse.ok().build())));
context.registerBean(HandlerAdapter.class, () -> new HandlerFunctionAdapter());
context.registerBean(HandlerResultHandler.class, () -> new ServerResponseResultHandler());
context.refresh();
return context;
}
@Override
protected AnnotationConfigApplicationContext newClient(int port) {
AnnotationConfigApplicationContext result = new AnnotationConfigApplicationContext();
URI baseUrl = URI.create("http://127.0.0.1:" + server.getPort());
result.registerBean(HttpTracing.class, () -> httpTracing);
result.registerBean(CurrentTraceContext.class,
() -> httpTracing.tracing().currentTraceContext());
result.registerBean(HttpClient.class, () -> testHttpClient(baseUrl));
result.registerBean(URI.class, () -> baseUrl);
result.register(componentClasses);
result.refresh();
return result;
}
protected Retrofit buildAndSave(RetrofitContext context, Retrofit.Builder builder) {
Retrofit retrofit = builder.build();
// add retrofit to this.names context as a bean
AnnotationConfigApplicationContext applicationContext = context.getContext(this.name);
applicationContext.registerBean(Retrofit.class, () -> retrofit);
return retrofit;
}
private void customizeProjectGenerationContext(AnnotationConfigApplicationContext context,
InitializrMetadata metadata) {
context.setParent(this.parentApplicationContext);
context.registerBean(InitializrMetadata.class, () -> metadata);
context.registerBean(BuildItemResolver.class, () -> new MetadataBuildItemResolver(metadata,
context.getBean(ProjectDescription.class).getPlatformVersion()));
context.registerBean(MetadataProjectDescriptionCustomizer.class,
() -> new MetadataProjectDescriptionCustomizer(metadata));
}