Spring Tutorial - Spring Bean Auto Scan








Spring can auto scan, detect, wire and instantiate Java beans.

The following sections shows the difference between xml configuration vs auto scan auto wire configuration.

Old fashion

Here is a normal Java bean.

package com.java2s.common;

public class Printer 
{
}

Another Java bean with referencing the code above.

package com.java2s.common;

public class PrinterHelper 
{
  Printer printer;
  public void setPrinter(Printer myPrinter) {
    this.printer = myPrinter;
  }

  @Override
  public String toString() {
    return "PrinterHelper [myPrinter=" + printer + "]";
  }
}

XML Bean configuration file Spring-Customer.xml with a normal bean configuration in Spring.

<beans xmlns="http://www.springframework.org/schema/beans"
  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-2.5.xsd">
  <bean id="printerHelper" class="com.java2s.common.PrinterHelper">
    <property name="printer" ref="myPrinter" />
  </bean>
  <bean id="myPrinter" class="com.java2s.common.Printer" />
</beans>

Here is the code to run.

package com.java2s.common;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class App 
{
    public static void main( String[] args )
    {
      ApplicationContext context = 
        new ClassPathXmlApplicationContext(new String[] {"SpringBeans.xml"});
      PrinterHelper cust = (PrinterHelper)context.getBean("printerHelper");
      System.out.println(cust);
    }
}


Download Java2s_Spring_Not_Autowired.zip





Auto Java bean Scan and wire

To enable Spring auto component scanning features, add annotation @Component to class.

package com.java2s.common;

import org.springframework.stereotype.Component;
@Component
public class Printer 
{
}

The following Java bean uses not only @Component to indicate this is an auto scan component, it also marks the property as Autowired.

package com.java2s.common;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class PrinterHelper 
{
  @Autowired
  Printer myPrinter;
}

The following xml configuration file finally adds the package to enable the auto wire auto scan context.

<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:context="http://www.springframework.org/schema/context"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context/spring-context-2.5.xsd">
  <context:component-scan base-package="com.java2s.common" />
</beans>


Download Java2s_Spring_Autowire.zip





Note

By default, Spring will lower case the first character of the component's simple class name as the bean id.

The following code shows how to use the auto-generated id to retrieve the component.

PrinterHelper cust = (PrinterHelper)context.getBean("printerHelper");

We can also name a component when declaring it.

@Component("myService")
public class PrinterHelper {
}

Once we define the name in @Component, we can retrieve it with this name 'myService'.

PrinterHelper cust = (PrinterHelper)context.getBean("myService");

Filter to include

The following code shows how to use Spring filter to scan and register components by regular expressions, even the class is not annotated with @Component.

package com.java2s.common;
public class Printer 
{
}

Helper class

package com.java2s.common;

import org.springframework.beans.factory.annotation.Autowired;

public class PrinterHelper 
{
  @Autowired
  Printer myPrinter;
}

The following xml configuration uses the Spring filter to include classes.

<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:context="http://www.springframework.org/schema/context"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  http://www.springframework.org/schema/context
  http://www.springframework.org/schema/context/spring-context-2.5.xsd">

  <context:component-scan base-package="com.java2s" >
    <context:include-filter type="regex" 
                       expression="com.java2s.common.*DAO.*" />
    <context:include-filter type="regex" 
                       expression="com.java2s.common.*Service.*" />
  </context:component-scan>
</beans>

The following code includes any classes under com.java2s.customer.dao package with DAO in class name.

<context:include-filter type="regex" 
                     expression="com.java2s.common.*DAO.*" />

Run it

package com.java2s.common;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.java2s.customer.services.PrinterHelper;
public class App 
{
    public static void main( String[] args )
    {
      ApplicationContext context = 
    new ClassPathXmlApplicationContext(new String[] {"SpringBeans.xml"});
      PrinterHelper cust = (PrinterHelper)context.getBean("printerHelper");
      System.out.println(cust);
    }
}

Filter to exclude

The following code shows how to exclude specified components to stop Spring from auto registering for classes annotated with @Service.

<context:component-scan base-package="com.java2s.common" >
    <context:exclude-filter type="annotation" 
      expression="org.springframework.stereotype.Service" />    
</context:component-scan>

Exclude classes with DAO letters in class name.

<context:component-scan base-package="com.java2s" >
    <context:exclude-filter type="regex" 
      expression="com.java2s.customer.dao.*DAO.*" />    
</context:component-scan>