Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a naming convention for @ComponentScan basePackageClasses? [closed]

Spring's @ComponentScan offers a type-safe basePackageClasses attribute - seems a good thing to use especially as it isn't uncommon for packages to be renamed on the project I'm working on. The documentation says:

Consider creating a special no-op marker class or interface in each package that serves no purpose other than being referenced by this attribute.

... but offers no further guidance on the name of such a class. Am wondering if there are any conventions around this. package-info.java already exists in all packages (as enforced by Checkstyle) - would have preferred to reuse this but sadly Java doesn't allow a class of this name.

(If no such standards exist am thinking perhaps PackageInfo, BasePackage, PackageMarker or similar but would prefer to follow convention if there is one.)

like image 977
Steve Chambers Avatar asked Dec 02 '14 16:12

Steve Chambers


People also ask

What is basePackageClasses?

The basePackageClasses attribute is the type-safe alternative to basePackages() in order to specify the packages to scan for the annotated components. The package of each specified class will be scanned. This will ensure that even when the package is renamed or moved, the component scan would work as expected.

What is difference between @component and @ComponentScan?

@Component and @ComponentScan are for different purposes. @Component indicates that a class might be a candidate for creating a bean. It's like putting a hand up. @ComponentScan is searching packages for Components.

What is the use of @ComponentScan annotation?

The @ComponentScan annotation is used with the @Configuration annotation to tell Spring the packages to scan for annotated components. @ComponentScan also used to specify base packages and base package classes using thebasePackageClasses or basePackages attributes of @ComponentScan.

What is @ComponentScan annotation in Spring boot?

One of the most important annotations in spring is @ComponentScan which is used along with the @Configuration annotation to specify the packages that we want to be scanned. @ComponentScan without arguments tells Spring to scan the current package and all of its sub-packages.


1 Answers

No answers yet and had to make a decision so here it is:

Marker class:

package com.companyname.appname.projectname.package1name;

/**
 * Empty marker class used to identify this package when using type-safe basePackageClasses in Spring @ComponentScan.
 */
public class PackageMarker {
    // Empty marker class
}

Usage:

@ComponentScan(basePackageClasses = {
    com.companyname.appname.projectname.package1name.PackageMarker.class,
    com.companyname.appname.projectname.package2name.PackageMarker.class,
    /* ...etc... */
})

PackageMarker naming justification:

  1. Seems sensible for all such classes to have the same name so they can be easily identified.
  2. Seems sensible for it to start with "Package" (in the same way as package-info.java).
  3. Seems sensible for it to end with "Marker" since the documentation refers to a "marker class".
  4. Opted not to include the word "Base" so it isn't confused with base classes.
  5. Opted not to include the word "Info" as it doesn't contain any info like package-info.java does.
  6. Opted not to include any other words (e.g. "NoOp") to keep it snappy and flexible for other possible uses.

Would still be interested if anyone can give examples of marker classes used in a more trail-blazing context...

like image 188
Steve Chambers Avatar answered Sep 29 '22 02:09

Steve Chambers