스프링 부트 구성 요소스캔 제외제외하지 않는 필터
간단한 테스트를 받고 있습니다.
@RunWith(SpringRunner.class)
@SpringBootTest(classes = SimpleTestConfig.class)
public class SimpleTest {
@Test
public void test() {
assertThat(true);
}
}
및 이 테스트에 대한 구성:
@SpringBootApplication
@ComponentScan(basePackageClasses = {
SimpleTestConfig.class,
Application.class
},
excludeFilters = @ComponentScan.Filter(
type = FilterType.ASSIGNABLE_TYPE,
classes = Starter.class))
public class SimpleTestConfig {
}
스타터 클래스를 제외하려고 합니다.
package application.starters;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
@Component
public class Starter {
@PostConstruct
public void init(){
System.out.println("initializing");
}
}
애플리케이션 클래스는 다음과 같습니다.
package application;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import static org.springframework.boot.SpringApplication.run;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
run(Application.class, args);
}
}
그러나 매우 이상한 이유로 스타터 클래스가 여전히 초기화되고 있습니다.
그 이유를 설명할 수 있는 사람?ComponentScan excludeFilters나의 것을 제외하지 않습니다.Starter수업?
각 구성 요소 검사는 개별적으로 필터링합니다.제외하는 동안Starter.class부터SimpleTestConfig,SimpleTestConfig초기화Application어느 것이 그것의 소유입니까.@ComponentScan제외하지 않고StarterComponentScan을 깔끔하게 사용하는 방법은 각 ComponentScan이 개별 패키지를 스캔하는 것입니다. 그러면 각 필터가 제대로 작동합니다.테스트에서와 같이 두 개의 개별 구성 요소 스캔이 동일한 패키지를 스캔하는 경우에는 이 작업이 수행되지 않습니다.
이것을 속이는 한 가지 방법은 모의실험을 제공하는 것입니다.Starter콩:
import org.springframework.boot.test.mock.mockito.MockBean;
public class SimpleTest {
@MockBean
private Starter myTestBean;
...
}
봄은 실제 수업 대신에 그 모의 수업을 사용할 것이고, 따라서.@PostConstruct메서드가 호출되지 않습니다.
기타 일반적인 솔루션:
- 직접 사용 안 함
Application.class어떤 단위의 시험에서도. - 스프링 종단 및 다음과 같은 주석 사용
@Profile("!TEST")에서Starter학급 - 스프링 부츠 사용
@ConditionalOn...에 대한 주석Starter학급
제외할 사용자 정의 구성 요소 검색 필터를 정의할 수 있습니다.
예제 코드는 다음과 같습니다.
@SpringBootApplication()
@ComponentScan(excludeFilters=@Filter(type = FilterType.REGEX, pattern="com.wyn.applications.starter.Starter*"))
public class SimpleTestConfig {
}
이것은 나에게 효과가 있습니다.
자세한 내용은 이 블로그를 참조하십시오.
언급URL : https://stackoverflow.com/questions/48102883/spring-boot-componentscan-excludefilters-not-excluding
'programing' 카테고리의 다른 글
| redistrepect_cast의 C에 해당하는 것은 무엇입니까? (0) | 2023.07.06 |
|---|---|
| Flask dev 서버를 실행하는 이유는 무엇입니까? (0) | 2023.07.06 |
| 새로운 문서의 Mongodb php get id? (0) | 2023.07.06 |
| Count에 기반한 IFELL 문을 사용하여 다른 Insert 문 (0) | 2023.07.06 |
| CURL을 통해 모든 장치에 Firebase 알림을 보내는 방법은 무엇입니까? (0) | 2023.07.06 |