Step 9 of 10 Integrating code into your XNAT plugin


In this step, you'll configure your @XnatPlugin class to configure and provision all of the components you've created in the rest of the practical.

Since creating your original plugin class back in Step 2 of 10 Defining your XNAT plugin, you've created a number of different resources in your plugin:

  • A couple new data types
  • A new data entity with management services
  • A preferences bean
  • A REST API controller

Now you just need to get all of this new functionality connected to XNAT. For this, we'll go back to the @XnatPlugin-annotated class you created earlier.

Configuring plugin data types

You can configure data types for XNAT by setting some information about the data type on the plugin class. The @XnatPlugin annotation provides the dataModels attribute to let you define some attributes about each data model you add. The dataModels attribute takes one or more instances of the @XnatDataModel annotation, which in turn lets you set the data type (e.g. workshop:biosampleCollection), configure the singular and plural names of the data type for display purposes as well as the modality code, and indicate whether the data type should be secured (i.e. published).

The @XnatPlugin annotation should now look something like this:

@XnatDataModel(value = WorkshopBiosamplecollectionBean.SCHEMA_ELEMENT_NAME,
               singular = "Biosample Collection",
               plural = "Biosample Collections")

The information from this annotation is read by XNAT as the system starts and used to configure and initialize the data type when it's newly added.

Integrating data entities

XNAT's Hibernate transaction and session management system needs to know where to find all of the entity classes in the system. The @XnatPlugin provides an attribute named entityPackages that lets you specify one or more Java packages that contain classes annotated with the @Entity annotation. For this implementation, you should add the package containing your SubjectMapping entity to the entityPackages attribute.

Loading Spring services

You created a number of classes that are annotated with what Spring calls component services. This wasn't obvious at the time, but the @Service and @Repository annotations are both @Component-based. The @XapiRestController annotation is based on the Spring @RestController annotation, which is also @Component-based, and the @NrgPreferenceBean contains @Component directly. Spring makes it easy to load these services using the @ComponentScan annotation. That annotation goes on the WorkshopXnatPlugin class declaration separately from the @XnatPlugin annotation. You just need to add each package containing @Component-based services to the @ComponentScan values.

After you revise the WorkshopXnatPlugin class, it should look something like this:

@XnatPlugin(value = "workshopPlugin", name = "XNAT 1.7 Workshop 2016 Plugin",
            entityPackages = "org.nrg.xnat.workshop.entities",
            dataModels = {@XnatDataModel(value = WorkshopBiosamplecollectionBean.SCHEMA_ELEMENT_NAME,
                                         singular = "Biosample Collection",
                                         plural = "Biosample Collections"),
                          @XnatDataModel(value = RadRadiologyreaddataBean.SCHEMA_ELEMENT_NAME,
                                         singular = "Radiology Read",
                                         plural = "Radiology Reads")})
public class WorkshopXnatPlugin {
    public String workshopPluginMessage() {
        return "Hello there from the workshop plugin!";


You've configured your plugin and are ready to build and deploy it.

Go to the next step