Skip to content

Simplify always-override resource registration in AOT #35855

@odrotbohm

Description

@odrotbohm

Assuming a BeanFactoryInitializationAotProcessor would like to register a resource that's not dependent on the ConfigurableListableBeanFactory handed into the method but globally applicable. It would return a BeanFactoryInitializationAotContribution that'd do something like this with the GenerationContext provided:

context.getGeneratedFiles().addResourceFile("someLocation", "someContent");

This fails for a subsequent run of the AOT process if the build hasn't cleaned out someLocation beforehand, as the resource creation rejects overwriting files by default. To reliably always create or replace the file to be created, one has to resort to this:

context.getGeneratedFiles().handleFile(Kind.RESOURCE, "someLocation", it -> {

	var resource = new ByteArrayResource("someContent".getBytes(StandardCharsets.UTF_8));

	if (it.exists()) {
		it.override(resource);
	} else {
		it.create(resource);
	}
});

This is pretty involved compared to the simple call to ….addResourceFile(…). I wonder if it makes sense to add method overloads that allow triggering the always-override behavior. Or, alternatively, flip the default to allow the override and rely on users with more advanced needs to simply use the already existing API that gives them more control over the file creation.

Related tickets

Metadata

Metadata

Assignees

No one assigned

    Labels

    in: coreIssues in core modules (aop, beans, core, context, expression)status: supersededAn issue that has been superseded by anothertheme: aotAn issue related to Ahead-of-time processing

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions