Spring Component Scanning

This commit is contained in:
Nandan Bn
2020-09-23 15:36:39 +05:30
parent ef9ad9a6fd
commit ab9aa60613
20 changed files with 801 additions and 0 deletions

View File

@@ -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 {
}

View File

@@ -0,0 +1,8 @@
package io.reflectoring.birds;
import org.springframework.stereotype.Component;
@Component
class Eagle {
}

View File

@@ -0,0 +1,8 @@
package io.reflectoring.birds;
import org.springframework.stereotype.Component;
@Component
class Sparrow {
}

View File

@@ -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);
}
}
}

View File

@@ -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 {
}

View File

@@ -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);
}
}

View File

@@ -0,0 +1,8 @@
package io.reflectoring.componentscan;
import org.springframework.stereotype.Service;
@Service
class UserService {
}

View File

@@ -0,0 +1,5 @@
package io.reflectoring.vehicles;
public class Car {
}

View File

@@ -0,0 +1,8 @@
package io.reflectoring.vehicles;
import org.springframework.stereotype.Component;
@Component
class Hyundai extends Car {
}

View File

@@ -0,0 +1,8 @@
package io.reflectoring.vehicles;
import org.springframework.stereotype.Component;
@Component
class SpaceX {
}

View File

@@ -0,0 +1,8 @@
package io.reflectoring.vehicles;
import org.springframework.stereotype.Component;
@Component
class Tesla extends Car {
}

View File

@@ -0,0 +1,8 @@
package io.reflectoring.vehicles;
import org.springframework.stereotype.Component;
@Component
class Train {
}

View File

@@ -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() {
}
}