In
this post, you are going to learn
EmployeeService.java
Test.java
a.
What
is component scan?
b.
How
can we communicate component scan information to spring boot?
c.
Is
SpringBootApplication annotation trigger component scan?
d.
Example
Application using SpringBootApplication annotation
What is Component
Scan?
Spring
is a dependency injection framework, where objects define their dependencies
using spring annotations. Suppose an object ‘obj1’ is using other objects obj2,
obj3. In the source code, ‘obj1’ mention its dependencies obj2, obj3 through
some annotations like @Bean constructor arguments, arguments to a factory
method, or properties that are set on the object instance after it is
constructed or returned from a factory method.
The
basic step in spring dependency injection is, to add right annotations
@Component or @Service or @Repository, and tell spring on what packages to be
scanned to get these components.
package
com.sample.model;
import
org.springframework.stereotype.Component;
@Component
public
class Employee {
}
For
example, I added @Component annotation on class Employee, it indicates that an
annotated class is a "component". Such classes are considered as
candidates for auto-detection, when using annotation-based configuration and
class path scanning.
How can we
communicate component scan information to spring boot?
By
using '@ComponentScan' annotation, we can specify the packages to be scanned
for components. Spring scan all the packages mentioned in @ComponentScan
annotation and automatically create beans for every class annotated with
@Component, @Service, @Controller, @RestController, @Repository.
Example
@ComponentScan({"com.sample.model",
"com.sample.service"})
Is
SpringBootApplication annotation trigger component scan?
Yes,
SpringBootApplication trigger component scan. When you see the definition of
SpringBootApplication annotation, it looks like below.
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters
= {
@Filter(type =
FilterType.CUSTOM, classes = TypeExcludeFilter.class),
@Filter(type =
FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
public
@interface SpringBootApplication {
}
As
you observe, SpringBootApplication annotation is annotated with ComponentScan
annotation.
If
the package hierarchies are below your main application class (@SpringBootApplication annotated class), then you’re covered by implicit components scan.
For
example,
package
com.sample;
@SpringBootApplication
public
class Test {
}
‘Test’
class is annotated with ‘SpringBootApplication’ annotation and it is defined
under the package com.sample. So Spring scan all the components under the package
com.sample, and in all the sub packages (com.sample.*).
Example Application
using SpringBootApplication annotation
Employee.java
package com.sample.model; import org.springframework.stereotype.Component; @Component public class Employee { }
EmployeeService.java
package com.sample.service; import org.springframework.stereotype.Component; @Component public class EmployeeService { }
Test.java
package com.sample; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.ApplicationContext; @SpringBootApplication public class Test { public static void main(String args[]) { ApplicationContext applicationContext = SpringApplication.run(Test.class, args); for (String name : applicationContext.getBeanDefinitionNames()) { System.out.println(name); } } }
Run
the application Test.java, you can able to see the bean names, employee,
employeeService in the output.
No comments:
Post a Comment