BAEL-2940 Update Spring ComponentScanning. Moved componentscan code to new module.

This commit is contained in:
Erik Pragt
2019-07-08 18:16:25 -05:00
parent e5d3e34725
commit 76c32dcd9b
13 changed files with 68 additions and 7 deletions
+55
View File
@@ -0,0 +1,55 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-boot-mvc</artifactId>
<name>spring-boot-mvc</name>
<packaging>jar</packaging>
<description>Module For Spring Boot DI</description>
<parent>
<artifactId>parent-boot-2</artifactId>
<groupId>com.baeldung</groupId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../parent-boot-2</relativePath>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.baeldung.SpringBootDiApplication</mainClass>
<layout>JAR</layout>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<start-class>com.baeldung.SpringBootDiApplication</start-class>
</properties>
</project>
@@ -0,0 +1,12 @@
package com.baeldung;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBootDiApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootDiApplication.class, args);
}
}
@@ -0,0 +1,5 @@
package com.baeldung.componentscan;
public class ExampleBean {
}
@@ -0,0 +1,34 @@
package com.baeldung.componentscan.springapp;
import com.baeldung.componentscan.ExampleBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan
//@ComponentScan(excludeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = Rose.class))
//@ComponentScan(basePackages = "com.baeldung.componentscan.springapp")
//@ComponentScan(basePackages = "com.baeldung.componentscan.springapp.animals")
//@ComponentScan (excludeFilters = @ComponentScan.Filter(type=FilterType.REGEX,pattern="com\\.baeldung\\.componentscan\\.springapp\\.flowers\\..*"))
public class SpringComponentScanApp {
private static ApplicationContext applicationContext;
@Bean
public ExampleBean exampleBean() {
return new ExampleBean();
}
public static void main(String[] args) {
applicationContext = new AnnotationConfigApplicationContext(SpringComponentScanApp.class);
for (String beanName : applicationContext.getBeanDefinitionNames()) {
System.out.println(beanName);
}
}
}
@@ -0,0 +1,8 @@
package com.baeldung.componentscan.springapp.animals;
import org.springframework.stereotype.Component;
@Component
public class Cat {
}
@@ -0,0 +1,8 @@
package com.baeldung.componentscan.springapp.animals;
import org.springframework.stereotype.Component;
@Component
public class Dog {
}
@@ -0,0 +1,8 @@
package com.baeldung.componentscan.springapp.flowers;
import org.springframework.stereotype.Component;
@Component
public class Rose {
}
@@ -0,0 +1,34 @@
package com.baeldung.componentscan.springbootapp;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import com.baeldung.componentscan.ExampleBean;
@SpringBootApplication
//@ComponentScan(basePackages = "com.baeldung.componentscan.springbootapp.animals")
//@ComponentScan ( excludeFilters = @ComponentScan.Filter(type=FilterType.REGEX,pattern="com\\.baeldung\\.componentscan\\.springbootapp\\.flowers\\..*"))
//@ComponentScan(excludeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = Rose.class))
public class SpringBootComponentScanApp {
private static ApplicationContext applicationContext;
@Bean
public ExampleBean exampleBean() {
return new ExampleBean();
}
public static void main(String[] args) {
applicationContext = SpringApplication.run(SpringBootComponentScanApp.class, args);
checkBeansPresence("cat", "dog", "rose", "exampleBean", "springBootApp");
}
private static void checkBeansPresence(String... beans) {
for (String beanName : beans) {
System.out.println("Is " + beanName + " in ApplicationContext: " + applicationContext.containsBean(beanName));
}
}
}
@@ -0,0 +1,8 @@
package com.baeldung.componentscan.springbootapp.animals;
import org.springframework.stereotype.Component;
@Component
public class Cat {
}
@@ -0,0 +1,8 @@
package com.baeldung.componentscan.springbootapp.animals;
import org.springframework.stereotype.Component;
@Component
public class Dog {
}
@@ -0,0 +1,8 @@
package com.baeldung.componentscan.springbootapp.flowers;
import org.springframework.stereotype.Component;
@Component
public class Rose {
}