Last updated Sep 6, 2019

Spring Java configuration - registering configuration classes

First, add Spring’s context namespace to your Spring XML file if it’s not already there. For example:

1
2
<?xml version="1.0" encoding="UTF-8"?>
<beans
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://www.springframework.org/schema/beans"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="
            http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd">
    <!-- ... the rest of the configuration file -->
</beans>

Next, register your new config classes in this file in one of two ways:

Option 1

  1. Add a <context:annotation-config /> element (to enable the use of @Configuration classes).
  2. Then, add your new Java config class(es) as anonymous beans, for example <bean class="com.example.myplugin.spring.SpringBeans" />.

Option 2

Please avoid this method! This method severely impacts app startup time due to an interaction between the OSGi container and Spring which results in traversing a huge number of packages.

Add a <context:component-scan base-package="..." /> element that points to the package containing your new @Configuration classes (this implicitly includes everything that the context:annotation-config element does). In this scenario, it's a good idea to ensure that this package contains only your @Configuration classes, to avoid scanning any classes unnecessarily.

Rate this page: