Spring Component Scanning
This commit is contained in:
@@ -0,0 +1,10 @@
|
||||
package io.reflectoring.birds;
|
||||
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
@ComponentScan
|
||||
public class BirdsExplicitScan {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package io.reflectoring.birds;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
class Eagle {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package io.reflectoring.birds;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
class Sparrow {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package io.reflectoring.componentscan;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.boot.context.event.ApplicationReadyEvent;
|
||||
import org.springframework.context.event.EventListener;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class BeanViewer {
|
||||
|
||||
private final Logger LOG = LoggerFactory.getLogger(getClass());
|
||||
|
||||
@EventListener
|
||||
public void showBeansRegistered(ApplicationReadyEvent event) {
|
||||
String[] beanNames = event.getApplicationContext().getBeanDefinitionNames();
|
||||
for(String beanName: beanNames) {
|
||||
LOG.info("{}", beanName);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package io.reflectoring.componentscan;
|
||||
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.FilterType;
|
||||
|
||||
import io.reflectoring.vehicles.Car;
|
||||
|
||||
@Configuration
|
||||
@ComponentScan(basePackages= "io.reflectoring.vehicles",
|
||||
includeFilters=@ComponentScan.Filter(type=FilterType.ASSIGNABLE_TYPE, classes=Car.class)
|
||||
, useDefaultFilters=false
|
||||
// Uncomment next line and comment out include filter to checkout exclude filter
|
||||
//excludeFilters=@ComponentScan.Filter(type=FilterType.ASSIGNABLE_TYPE, classes=Car.class)
|
||||
)
|
||||
public class ExplicitScan {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package io.reflectoring.componentscan;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
||||
import io.reflectoring.birds.BirdsExplicitScan;
|
||||
|
||||
@SpringBootApplication
|
||||
@Import(value= {BirdsExplicitScan.class})
|
||||
public class SpringComponentScanningApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SpringComponentScanningApplication.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package io.reflectoring.componentscan;
|
||||
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
class UserService {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
package io.reflectoring.vehicles;
|
||||
|
||||
public class Car {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package io.reflectoring.vehicles;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
class Hyundai extends Car {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package io.reflectoring.vehicles;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
class SpaceX {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package io.reflectoring.vehicles;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
class Tesla extends Car {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package io.reflectoring.vehicles;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
class Train {
|
||||
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
package io.reflectoring.componentscan;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
@SpringBootTest
|
||||
class SpringComponentScanningApplicationTests {
|
||||
|
||||
@Test
|
||||
void contextLoads() {
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user