Sunday, April 19, 2020

[Spring] Register an empty object with the @Component

Component Scanning

 To inject the dependency, you first had to register the classes as bins, and you had to create an xml file or setup class to register them as bins. Here, you can get a blank registration with two different annunciations: @ComponentScan and @ComponentScan.

A.java, B.java
@Component("a")
public class A {
 ...
}

@Component("b")
public class B {
 ...
}
 Attach @Component to the classes you want to register as bins. If you specify a property value, you can also name an empty object.

Config.java
@Configuration
@ComponentScan(basePackages = {"example"})
public class Config {
 ...
}
 If you attach @ComponentScan to a setup class, it will navigate to the classes with @Component and register them as bins. It's easier to set because @Bean is registered as a blank. The basePackage property can specify the scope of the navigation, meaning that you want to navigate in the sample package and its subpackages.

Component Scan Target

 Applying @Controller, @Service, @Repository, @Aspect, @Configuration, as the component skin target is also a scan target and is an empty registration. Except for @Aspect, the other annotations are sub-annotations of @Component.

No comments:

Post a Comment