Skip to content

Resolve POJO constructor arguments by name rather than position #721

Closed
@usethe4ce

Description

@usethe4ce

The docs say:

In order to inject the results into the constructor, MyBatis needs to identify the constructor by the type of its parameters. Java has no way to introspect (or reflect) on parameter names. So when creating a constructor element, ensure that the arguments are in order, and that the data types are specified.

But actually there are few ways commonly used to get at the constructor's parameter names. Spring can wire up immutable beans this way, for example. There's compiling with debug info (very common nowadays), the older @ConstructorProperties annotation, explicit annotations on each constructor parameter (e.g. MyBatis's own @Param), and in JDK8 the compilation option -parameters.

It's a huge advantage, of course, when dealing with immutables to be able to match constructor parameters to fields not positionally, which is quite fragile, but by name.

Metadata

Metadata

Assignees

Labels

enhancementImprove a feature or add a new feature

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions