Your First Spring Program Using Component and ComponentScan Annotations

 We will create a simple class, configure it and test it. You can configure a Spring application through xml, or Java configuration or annotations along with xml or java configuration. In this example we will use @Component and @ComponentScan annotations along with Java configuration. Note that these annotations can also be used along with xml configuration and we will see how to do that in the end.

@Component can be used over a class to tell spring to consider it as a bean component. @ComponentScan annotation  can be used along with @Configuration classes to specify the packages that need to be scanned for components. 

 

Prerequisites

If you are using Maven, just copy paste below dependency into your pom file and all other required dependencies will be added automatically, as they are the transitive dependencies for below dependency.

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.2.6.RELEASE</version>
    </dependency>
</dependencies>

Please use the latest version as available in the Spring website @ http://projects.spring.io/spring-framework.

PS: There is a separate note for getting started with Gradle build, but notes in this section will be using Maven build tool in general. If you are manually adding jars to classpath, you should have already downloaded the spring jars and commons logging jar as described in downloading-and-setting-up-the-spring-framework and added to classpath. We however prefer using a build tool like Maven/Gradle.

 

Bean component class

We will use a similar class as in prevoius examples, but with @Component annotation.

JJWriterComp.java

package com.javajee.spring;

import org.springframework.stereotype.Component;

@Component("writer")
public class JJWriterComp {
    public void write() {
        System.out.println("Default Writer");
    }
}

 

Config class

We need to have a class that is annotated with @Configuration, with @ComponentScan annotation as:

DemoConfig.java

package com.javajee.spring;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@Configuration
@ComponentScan(basePackages="com.javajee.spring")
public class DemoConfig {

}

Instead of using @ComponentScan annotation, we can also use <context:component> element in an spring config xml.

 

Test Class

If we are using the Java configuration class, you can use the below class to test.

JJWriterCompMain.java

package com.javajee.spring;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class JJWriterCompMain {
    
    public static void main(String[] args) {

        ApplicationContext context = new AnnotationConfigApplicationContext(DemoConfig.class);

        JJWriterComp writer = context.getBean("writer", JJWriterComp.class);
        
        writer.write();

    }

}

 

Auto bean naming with @Component

Generally, if you don't specify a name as in this case (e.g. writer) for @Component, the bean name will be automatically generated. This will be the same name as that of the class, but with first letter made small. MyClass becomes myClass. 

However, I saw a strange behaviour. If my class name start with two capital letters and I annotate with @Component, the auto generated name starts with capital letter. Else it starts with small letter.

JJwriterComp gives JJwriterComp

JaaJwriterComp gives jaaJwriterComp

I could not find any reference for this difference in any documentation.

 

Specifying base package in spring xml config file

Instead of using @ComponentScan annotation, we can also use <context:component-scan> element in an spring config xml.

spring.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xsi:schemaLocation="http://www.springframework.org/schema/beans

    http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-4.0.xsd">

    <context:component-scan base-package="com.javajee.spring" />

</beans>

You then need to use ClassPathXmlApplicationContext to get the context:

ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");

Search the Web

Custom Search

Searches whole web. Use the search in the right sidebar to search only within javajee.com!!!