Spring Components & Auto Scanning & Configuration

Spring managed beans are declared with the stereotype annotations specialized from @Component or with the @Component annotation itself.
When you declare a class with @Component[+derived] annotations, that bean is detected by Spring, via component scanning or explicitly wiring,
and put into the ApplicationContext.

1. Component Scanning

In a Spring application, you can declare your beans manually inside the bean configuration file,
but most probably, auto scanning of components is the preferred way of registering your beans into the application context.

You can configure auto scanning in bean configuration file or with the annotation.

1.1 Enable Auto-Scanning in Bean Configuration File

This is available since Spring 2.5:
<beans xmlns="http://www.springframework.org/schema/beans"
    <context:component-scan base-package="com.jtudy.springworkshop" />

1.2 Enable Auto Scanning with Annotation

We can use @ComponentScan to enable auto scanning since Spring 3.1.
@ComponentScan(basePackages= { "com.jtudy.springworkshop" })
public class SampleApplication

1.3 Wiring With @Import

Instead of using @ComponentScan, components can manually be registered by using @Import.
@Import({ComponentA.class, ComponentB.class})
public class SampleApplication

2. @Configuration

@Configuration can declare that a class having one or more Spring-manageable beans decorated with @Bean.
@Configuration also can declare that a class having auto-scannable Spring components decorated with @Component annotations.

2.1 Wiring of @Bean with @Configuration

To make a Spring-managed bean manually, we can annotate a method with @Bean in a class annotated with @Configuration.
By doing this, the returning bean from the @Bean method becomes autowirable Spring component that is manually defined instead of using auto-scanable @Component annotations.
public class ManuelBeanConfig {

 public ManuelBean manuelBean() {
  // configure and return your bean instance..

2.2 Wiring of @Component beans with @Configuration

If we annotate a configuration class in a default package with @ComponentScan annotation then we do make all @Component beans auto-scannable.
However, sometimes we need to group of components under specific configurations. 
For example, for different layers/interfaces of the application we can declare different @Configuration classes which themselves having their own @ComponentScan.
Then we can import these configurations inside a parent configuration using @Import annotation.
Let's see an example:

Here is the parent configuration declaring the child configurations each of which having their own configuration for component scanning:
@Import({BookManagementConfiguration.class, CustomerManagementConfiguration.class })
public class SpringComponentsApplication

And here is the child configurations:
public class CustomerManagementConfiguration

public class BookManagementConfiguration

3. Spring Components

Basically, you can annotate a class with @Component to make it Spring-managed.
However, for more specialized managment of beans, the following stereotype annotations, derived from @Component, should be prefered in specific layers of the application.
These annotations are @Service, @Repository, @Controller and @RestController.

This is the stereotype to declare a managed bean for service layer.

This is the stereotype to declare a managed bean for persistence layer.

This is the stereotype to declare a managed bean for presentation layer which is of Spring MVC.

This is the stereotype to declare a managed bean for restful service layer.



Java EE Technology Stack

Dependency Injection In Java and CDI

Proxy Server