Spring Book – Chapter 4 – Configuration Styles

Component Scanning

Using annotations in your Spring application will not trigger the Spring Framework to look into all the available class in your application and create appropriate beans and then maintain it in the Spring container. You will have to explicitly let know the Spring Container which are the classes which the Spring needs to load into the Spring container as beans. This is achieved by the component scanning. If you are using XML configuration, component scanning is done by declaring the following in your XML:

Listing. Component scanning in XML configuration

The scan element declared in the above code snippet forces the Spring container to search the package specified in the base-package attribute for all stereotype components. The base-package attribute specifies the parent package to look for stereotype components, but the Spring container scans all sub-packages as well. Use a comma-separated list of package names when you need to search multiple packages as given below:

By default, all components marked with the @Repository, @Service, @Controller, and @Component annotations are detected in the scan. However, you can configure the component-scan element with filters to include or exclude classes in the scan. In the example below, a regular expression include filter allows all classes that end in “Repository” to be included in the scan. Conversely, any component marked with a “@Service” annotation is excluded from the scan.

Five different types of filters are available for including or excluding classes in a scan. Below table describes each of them.

Table. Different filtering options available for including or excluding in component scan

Filter Type Description Expression Example
annotation Include or exclude those classes with a stereotype annotation org.springframework. stereotype.Repository
aspectj Use an AspectJ expression to specify classes to include or exclude (requires AspectJ libraries) com.mybook..*Controller
assignable Include or exclude classes that extend or implement this class or interface org.mybook.mvc.LoyaltyBaseController
custom A custom implementation of the org.springframework.core.type.TypeFilter interface com.mybook.MyTypeFilterImpl
regex Use a regular expression to specify classes to include or exclude .*Repository

Basically, the include filters scan the @Repository, @Service, @Controller, and @Component annotations, but you can disable this behavior very easily. To turn off the automatic inclusion of components with the stereotype annotations, add the use-default-filters=”false” attribute to the component-scan element. The example component-scan configuration below turns off this default behavior (to include all components with a stereotype annotation) and includes only those assignable from com.mybook.mvc.LoyaltyBaseController.

Auto registering beans using annotations is a great way to reduce the amount of XML required to configure Spring. If it bothers you that your auto registered classes are annotated with Spring-specific annotations and if you’re looking for a more non-intrusive way of doing this, you have the following options:

  • Create your own custom stereotype annotation. Doing so is as simple as creating a custom annotation that is itself annotated with @Component:

  • Add a filter to <context:component-scan> to scan for annotations that it normally would not:

In the above case, the @MyOwnComponent custom annotation has been added to the list of annotations that are scanned for, but @Component has been excluded (that is, @Component annotated classes will no longer be auto registered).

Even though component scanning is very advantageous for you as a programmer, there are certain drawbacks to the application. If you have a very large application which has too many files to be scanned by the container, it could result in slower startup time. But generally this slowness can be quantitatively measured as few extra seconds.

Best Practice: To speed up component scanning, the following best practices are strongly recommended:

1. The attribute base-package should be carefully configured such that only required packages are scanned by the Spring container for classes which have annotations. In the below code snippet, only specific packages namely com.mybook.loyalty.repositories and com.mybook.loyalty.services, in which annotated classes exists will be scanned by the container.

<context:component-scan base-package=”com.mybook.loyalty.repositories,com.mybook.loyalty.services”/>

2. Use include and exclude filters judiciously. In the below example classes ending with Repository and Service will be included and will exclude classes annotated with stereotype annotation @Controller.

<context:component-scan base-package=”com.mybook.loyalty”><context:include-filter type=”*Service,*Repository”/><context:exclude-filter type=”org.springframework.stereotype.Controller”/></ context:component-scan>

XML or Annotation or Java Style?

In Spring 1.0, when bean definition was introduced using XML, it was believed to be a simple, general purpose and flexible approach. But over the period XML way of configuring beans was seen as verbose and not type-safe.

In Spring 2.0, to address the disadvantages of using plain XML for bean configuration, namespace concept was brought in. Spring innovative XML namespace was seen as concise, powerful and easier to use. Again it didn’t address the developer’s expectation completely and some disadvantage was associated with XML namespace. It was thought to be opaque, non extensible and above all difficult to write your own.

In came Annotation-driven injection with Spring 2.5 to the rescue. It was thought to be really concise and convenient, especially using @Controller in Spring MVC. But again, there were still disabilities surrounding this. It can’t be used to wire up third-party code, ambiguities can arise if type-based injection is used (@Qualifier usage explained in earlier section) and above all it still requires XML for bootstrap purpose.

Yes, in came Spring 3.0 with innovation. This time again Spring eliminates more XML and uses Java language for many more configurations required for the Spring container. In came annotations like @Configuration, @Bean etc. Using Java configuration was thought to bring type-safety and object oriented principles into action. It can be used to configure any component (third-party). You do have complete programmatic control over what you want to achieve and for doing so it doesn’t require any special tooling. With such advantages in hand, still it has some limitations to overcome and become complete champion. Some notable mentions are; it lacked equivalent to <namespace:*/>. Complete elimination of XML not yet achieved as it was still required for transaction management, AOP etc.

Spring’s innovation never stops; example of this is Spring 3.1 with complete elimination of XML. With Spring 3.1, a vision envisaged over a period of time has been completed. It can be used to configure all major container features. However, developers still have a liberty of mixing and matching styles as desired according to the project requirement.

Page Visitors: 3391

The following two tabs change content below.
Tomcy John

Tomcy John

Blogger & Author at javacodebook
He is an Enterprise Java Specialist holding a degree in Engineering (B-Tech) with over 10 years of experience in several industries. He's currently working as Principal Architect at Emirates Group IT since 2005. Prior to this he has worked with Oracle Corporation and Ernst & Young. His main specialization is on various web technologies and acts as chief mentor and Architect to facilitate incorporating Spring as Corporate Standard in the organization.
Tomcy John

Latest posts by Tomcy John (see all)

2 thoughts on “Spring Book – Chapter 4 – Configuration Styles

  1. Hi there,

    There is one mistake repeated several times here. The value of the propertyInteger is being set using “ref” instead of “value” and you may want to remove the spaces before and after some of the values/properties to avoid others new to the framework copying and pasting and getting tripped up.

    Apart from that, thanks for making the material available for free.

    1. Sure will do the necessary changes. I wrote the material using MS Word and after that when i copied, these spaces automatically came in.

      Thanks for pointing it out.

      Tomcy John

Leave a Reply

Your email address will not be published. Required fields are marked *