Introduce aliases for 'value' annotation attributes [SPR-11393] #16020
Labels
in: core
Issues in core modules (aop, beans, core, context, expression)
in: data
Issues in data modules (jdbc, orm, oxm, tx)
in: messaging
Issues in messaging modules (jms, messaging)
in: test
Issues in the test module
in: web
Issues in web modules (web, webmvc, webflux, websocket)
type: enhancement
A general enhancement
Milestone
Sam Brannen opened SPR-11393 and commented
Background
Java supports a special annotation attribute named
value
that can be used without being named if an annotation is declared with a single attribute. Due to the convenience it provides, thevalue
attribute is used frequently in annotations across the Spring Framework. However, there are some downsides to making an attribute available only via thevalue
attribute name:value
attribute is used without being named (i.e., as a single attribute), the readability is enhanced due to the reduction of clutter, but if multiple attributes are declared, the readability and usability of the code base suffers. Seeing "value=..." repeated across annotations makes the intent of the code unclear, especially to newcomers to the framework.value
attribute is (for good reasons) not supported as a candidate for overriding.Example: Multiple Attribute Declarations
Consider the following annotated handler method from a Spring MVC controller for JUG events.
In the above example, only the path for the request mapping is declared as an annotation attribute. In such use cases, the use of the unnamed
value
attribute is ideal. But how does the code look if multiple attributes must be declared?In the above example, supplying the path on its own is insufficient for the use case at hand. Thus, both the path and method must be supplied. Since the path can only be specified via the
value
attribute, the code becomes unclear. What does "value" mean in this context?Wouldn't it be better to be able to be very explicit?
The above example introduces an alias for the
value
attribute. By specifying the path via apath
attribute, the meaning of the code becomes clear, even to readers who are not familiar with Spring MVC. Although this annotation is supported in both Servlet and Portlet environments, it has been decided to name the aliaspath
since the Servlet use case is more common.Example: Meta-annotation Attribute Overrides
Consider the
@ContextConfiguration
from the Spring TestContext Framework (TCF). When it was introduced in Spring 2.5, it only supported alocations
attribute for specifying the resource locations to use for loading anApplicationContext
. After observing common use cases for this annotation in the developer community it became apparent that developers typically only need to declare the locations and none of the other available attributes. So in Spring 3.0, thevalue
attribute was introduced in@ContextConfiguration
as an alias for the existinglocations
attribute.The fact that both the
value
andlocations
attributes are supported in@ContextConfiguration
not only overcomes the aforementioned readability issue, but it also allows for@ContextConfiguration
to be used as a meta-annotation on a composed annotation with attribute overrides for the locations.The above example demonstrates how to override the
locations
attribute of@ContextConfiguration
in a custom composed annotation (@TransactionalTestConfig
).Without the
locations
alias, this overriding would not be possible.Deliverables
value
attribute in@Component
classes or any class meta-annotated with@Component
; however, it may prove useful to declare an alias for component names (e.g.,componentName
) in order to allow the name of a component to be specified via a custom composed annotation without the ambiguity of thevalue
attribute.@Component
@Service
@Repository
@Controller
@ControllerAdvice
@RestController
@Configuration
value
attribute with a meaningful name.value
attribute and its alias and revise as necessary in order to ensure clarity of purpose.value
of said annotation now uses the newly introduced functionality for looking up annotation attributes with aliases (see Introduce unified support for declaring and looking up annotation attribute aliases [SPR-11512] #16137).Candidate Annotations
The tables below contain annotations in the Spring Framework that fall into one of two categories:
value
attribute in addition to other attributes.value
attribute (i.e., no additional attributes), but it might still benefit from having an alias so that it can be overridden in custom composed annotations (e.g., using the simple name-based convention).Annotations with a
value
attribute and other attributesspring-context
@Cacheable
cacheNames
spring-context
@CacheEvict
cacheNames
spring-context
@CachePut
cacheNames
spring-context
@ComponentScan
basePackages
spring-context
@ComponentScan.Filter
classes
spring-context
@ImportResource
locations
spring-context
@ManagedResource
objectName
spring-context
@Scope
name
spring-messaging
@Header
name
spring-messaging
@Payload
expression
spring-messaging
@SendToUser
destinations
spring-test
@ActiveProfiles
profiles
spring-test
@ContextConfiguration
locations
spring-test
@Sql
scripts
spring-test
@TestExecutionListeners
listeners
spring-test
@TestPropertySource
locations
spring-tx
@Transactional
transactionManager
spring-web
@ControllerAdvice
basePackages
spring-web
@CookieValue
name
spring-web
@CrossOrigin
origins
spring-web
@MatrixVariable
name
spring-web
@RequestHeader
name
spring-web
@RequestMapping
path
spring-web
@RequestParam
name
spring-web
@RequestPart
name
spring-web
@ResponseStatus
code
spring-web
@SessionAttributes
names
spring-webmvc-portlet
@ActionMapping
name
spring-webmvc-portlet
@RenderMapping
windowState
Rejected Candidates
The following table contains annotations that in fact have a
value
attribute (and typically no other attributes) but have been rejected as candidates for receiving aliases.Note, however, that just because an annotation has been rejected as a candidate for receiving a value-alias, this does not mean that the
value
attribute will not be able to be used in composed annotations. On the contrary, once the work for #16138 is complete, there will be an annotation-based mechanism for overridingvalue
annotation attributes in meta-annotations.spring-beans
@Qualifier
spring-context
@Async
spring-context
@DependsOn
spring-context
@Lazy
spring-context
@Profile
spring-context
@Validated
spring-core
@Order
spring-messaging
@DestinationVariable
spring-messaging
@MessageExceptionHandler
spring-messaging
@MessageMapping
spring-messaging
@SendTo
spring-messaging
@SubscribeMapping
spring-test
@BootstrapWith
spring-test
@Repeat
spring-test
@Rollback
spring-test
@WebAppConfiguration
spring-webmvc-portlet
@ResourceMapping
Affects: 4.0 GA
Issue Links:
1 votes, 11 watchers
The text was updated successfully, but these errors were encountered: