当前位置: 首页>>代码示例>>Java>>正文


Java ComponentScan类代码示例

本文整理汇总了Java中org.springframework.context.annotation.ComponentScan的典型用法代码示例。如果您正苦于以下问题:Java ComponentScan类的具体用法?Java ComponentScan怎么用?Java ComponentScan使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


ComponentScan类属于org.springframework.context.annotation包,在下文中一共展示了ComponentScan类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: getApplicationPackageName

import org.springframework.context.annotation.ComponentScan; //导入依赖的package包/类
/**
 * Return root package of spring boot application.
 *
 * @param applicationContext
 * @return list of packages
 */
public static List<String> getApplicationPackageName(final ApplicationContext applicationContext) {

    Set<String> candidateClasses = new HashSet<>();
    candidateClasses.addAll(Arrays.asList(applicationContext.getBeanNamesForAnnotation(SpringBootApplication.class)));
    candidateClasses.addAll(Arrays.asList(applicationContext.getBeanNamesForAnnotation(EnableAutoConfiguration.class)));
    candidateClasses.addAll(Arrays.asList(applicationContext.getBeanNamesForAnnotation(ComponentScan.class)));

    if (candidateClasses.isEmpty()) {
        throw new RuntimeException("Is mandatory for the starter have @SpringBootApplication, @EnableAutoConfiguration or @ComponentScan annotation");
    } else {
        return candidateClasses.parallelStream()
                .map(candidateClazz -> applicationContext.getBean(candidateClazz).getClass().getPackage().getName())
                .distinct()
                .collect(Collectors.toList());
    }

}
 
开发者ID:ganchix,项目名称:morphia-spring-boot-starter,代码行数:24,代码来源:MorphiaUtils.java

示例2: buildTypeSpec

import org.springframework.context.annotation.ComponentScan; //导入依赖的package包/类
private TypeSpec buildTypeSpec(List<MethodSpec> methodSpecs, List<FieldSpec> fieldSpecs, MethodSpec setUp) {

        return TypeSpec.classBuilder(this.restControllerModel.getSimpleClassName())
                .addAnnotation(Transactional.class)
                .addAnnotation(
                        AnnotationSpec.builder(RunWith.class)
                                      .addMember("value", "$T.class", SpringJUnit4ClassRunner.class)
                                      .build()
                )
                .addAnnotation(
                        AnnotationSpec.builder(ComponentScan.class)
                                      .addMember("basePackages", "{$S, $S}", "YOUR_DTOs_PACKAGE", "YOUR_SERVICEs_PACKAGE")
                                      .build()
                )
                .addAnnotation(SpringBootTest.class)
                .addModifiers(Modifier.PUBLIC)
                .addFields(fieldSpecs)
                .addMethod(setUp)
                .addMethods(methodSpecs)
                .build();
    }
 
开发者ID:HomoEfficio,项目名称:spring-web-api-test-stubber,代码行数:22,代码来源:SpringBootRestControllerTesterStubGenerator.java

示例3: includedInBasePackageClasses

import org.springframework.context.annotation.ComponentScan; //导入依赖的package包/类
public static boolean includedInBasePackageClasses(String clazzName, ComponentScan cs) {
    Class<?> clazzToCheck;
    try {
        clazzToCheck = Class.forName(clazzName);
    } catch (ClassNotFoundException e) {
        System.out.println("Unable to find " + clazzName);
        return false;
    }
    Class<?>[] clazzes = cs.basePackageClasses();
    for (Class<?> clazz : clazzes) {
        if (clazzToCheck.isAssignableFrom(clazz)) {
            return true;
        }
    }
    return false;
}
 
开发者ID:apache,项目名称:cloudstack,代码行数:17,代码来源:IntegrationTestConfiguration.java

示例4: springBootUseSpringBootApplicationGroovy

import org.springframework.context.annotation.ComponentScan; //导入依赖的package包/类
@Test
public void springBootUseSpringBootApplicationGroovy() {
	ProjectRequest request = createProjectRequest("web");
	request.setLanguage("groovy");
	request.setBootVersion("1.2.0.RC1");
	request.setName("MyDemo");
	request.setPackageName("foo");
	generateProject(request)
			.sourceCodeAssert("src/main/groovy/foo/MyDemoApplication.groovy")
			.hasImports(SpringBootApplication.class.getName())
			.doesNotHaveImports(EnableAutoConfiguration.class.getName(),
					ComponentScan.class.getName(), Configuration.class.getName())
			.contains("@SpringBootApplication").doesNotContain(
					"@EnableAutoConfiguration", "@Configuration", "@ComponentScan");
}
 
开发者ID:spring-io,项目名称:initializr,代码行数:16,代码来源:ProjectGeneratorTests.java

示例5: addComponentScanningPackages

import org.springframework.context.annotation.ComponentScan; //导入依赖的package包/类
private void addComponentScanningPackages(Set<String> packages,
		AnnotationMetadata metadata) {
	AnnotationAttributes attributes = AnnotationAttributes.fromMap(metadata
			.getAnnotationAttributes(ComponentScan.class.getName(), true));
	if (attributes != null) {
		addPackages(packages, attributes.getStringArray("value"));
		addPackages(packages, attributes.getStringArray("basePackages"));
		addClasses(packages, attributes.getStringArray("basePackageClasses"));
		if (packages.isEmpty()) {
			packages.add(ClassUtils.getPackageName(metadata.getClassName()));
		}
	}
}
 
开发者ID:vikrammane23,项目名称:https-github.com-g0t4-jenkins2-course-spring-boot,代码行数:14,代码来源:ConfigurationWarningsApplicationContextInitializer.java

示例6: getFilters

import org.springframework.context.annotation.ComponentScan; //导入依赖的package包/类
@Override
protected ComponentScan.Filter[] getFilters(FilterType type) {
  switch (type) {
    case INCLUDE:
      return this.annotation.includeFilters();
    case EXCLUDE:
      return this.annotation.excludeFilters();
  }
  throw new IllegalStateException("Unsupported type " + type);
}
 
开发者ID:mybatis,项目名称:spring-boot-starter,代码行数:11,代码来源:MybatisTypeExcludeFilter.java

示例7: isScanningDefaultPackage

import org.springframework.context.annotation.ComponentScan; //导入依赖的package包/类
private boolean isScanningDefaultPackage(AnnotationMetadata metadata) {
	AnnotationAttributes attributes = AnnotationAttributes.fromMap(metadata
			.getAnnotationAttributes(ComponentScan.class.getName(), true));
	if (attributes != null && hasNoScanPackageSpecified(attributes)) {
		if (isInDefaultPackage(metadata.getClassName())) {
			return true;
		}
	}
	return false;
}
 
开发者ID:Nephilim84,项目名称:contestparser,代码行数:11,代码来源:ConfigurationWarningsApplicationContextInitializer.java

示例8: afterPropertiesSet

import org.springframework.context.annotation.ComponentScan; //导入依赖的package包/类
@Override
public void afterPropertiesSet() throws Exception {
	packages = applicationContext
		.getBeansWithAnnotation(ComponentScan.class).values()
		.stream()
		.map(obj -> obj.getClass().getPackage().getName())
		.collect(Collectors.toList());
}
 
开发者ID:benneq,项目名称:spring-boot-javafx-sample,代码行数:9,代码来源:SpringJavaFXBuilderFactory.java

示例9: springBoot11UseEnableAutoConfigurationJava

import org.springframework.context.annotation.ComponentScan; //导入依赖的package包/类
@Test
public void springBoot11UseEnableAutoConfigurationJava() {
	ProjectRequest request = createProjectRequest("web");
	request.setBootVersion("1.1.9.RELEASE");
	request.setName("MyDemo");
	request.setPackageName("foo");
	generateProject(request)
			.sourceCodeAssert("src/main/java/foo/MyDemoApplication.java")
			.hasImports(EnableAutoConfiguration.class.getName(),
					ComponentScan.class.getName(), Configuration.class.getName())
			.doesNotHaveImports(SpringBootApplication.class.getName())
			.contains("@EnableAutoConfiguration", "@Configuration", "@ComponentScan")
			.doesNotContain("@SpringBootApplication");
}
 
开发者ID:spring-io,项目名称:initializr,代码行数:15,代码来源:ProjectGeneratorTests.java

示例10: springBootUseSpringBootApplicationJava

import org.springframework.context.annotation.ComponentScan; //导入依赖的package包/类
@Test
public void springBootUseSpringBootApplicationJava() {
	ProjectRequest request = createProjectRequest("web");
	request.setBootVersion("1.2.0.RC1");
	request.setName("MyDemo");
	request.setPackageName("foo");
	generateProject(request)
			.sourceCodeAssert("src/main/java/foo/MyDemoApplication.java")
			.hasImports(SpringBootApplication.class.getName())
			.doesNotHaveImports(EnableAutoConfiguration.class.getName(),
					ComponentScan.class.getName(), Configuration.class.getName())
			.contains("@SpringBootApplication").doesNotContain(
					"@EnableAutoConfiguration", "@Configuration", "@ComponentScan");
}
 
开发者ID:spring-io,项目名称:initializr,代码行数:15,代码来源:ProjectGeneratorTests.java

示例11: springBoot11UseEnableAutoConfigurationGroovy

import org.springframework.context.annotation.ComponentScan; //导入依赖的package包/类
@Test
public void springBoot11UseEnableAutoConfigurationGroovy() {
	ProjectRequest request = createProjectRequest("web");
	request.setLanguage("groovy");
	request.setBootVersion("1.1.9.RELEASE");
	request.setName("MyDemo");
	request.setPackageName("foo");
	generateProject(request)
			.sourceCodeAssert("src/main/groovy/foo/MyDemoApplication.groovy")
			.hasImports(EnableAutoConfiguration.class.getName(),
					ComponentScan.class.getName(), Configuration.class.getName())
			.doesNotHaveImports(SpringBootApplication.class.getName())
			.contains("@EnableAutoConfiguration", "@Configuration", "@ComponentScan")
			.doesNotContain("@SpringBootApplication");
}
 
开发者ID:spring-io,项目名称:initializr,代码行数:16,代码来源:ProjectGeneratorTests.java

示例12: springBoot11UseEnableAutoConfigurationKotlin

import org.springframework.context.annotation.ComponentScan; //导入依赖的package包/类
@Test
public void springBoot11UseEnableAutoConfigurationKotlin() {
	ProjectRequest request = createProjectRequest("web");
	request.setLanguage("kotlin");
	request.setBootVersion("1.1.9.RELEASE");
	request.setName("MyDemo");
	request.setPackageName("foo");
	generateProject(request)
			.sourceCodeAssert("src/main/kotlin/foo/MyDemoApplication.kt")
			.hasImports(EnableAutoConfiguration.class.getName(),
					ComponentScan.class.getName(), Configuration.class.getName())
			.doesNotHaveImports(SpringBootApplication.class.getName())
			.contains("@EnableAutoConfiguration", "@Configuration", "@ComponentScan")
			.doesNotContain("@SpringBootApplication");
}
 
开发者ID:spring-io,项目名称:initializr,代码行数:16,代码来源:ProjectGeneratorTests.java

示例13: springBootUseSpringBootApplicationKotlin

import org.springframework.context.annotation.ComponentScan; //导入依赖的package包/类
@Test
public void springBootUseSpringBootApplicationKotlin() {
	ProjectRequest request = createProjectRequest("web");
	request.setLanguage("kotlin");
	request.setBootVersion("1.2.0.RC1");
	request.setName("MyDemo");
	request.setPackageName("foo");
	generateProject(request)
			.sourceCodeAssert("src/main/kotlin/foo/MyDemoApplication.kt")
			.hasImports(SpringBootApplication.class.getName())
			.doesNotHaveImports(EnableAutoConfiguration.class.getName(),
					ComponentScan.class.getName(), Configuration.class.getName())
			.contains("@SpringBootApplication").doesNotContain(
					"@EnableAutoConfiguration", "@Configuration", "@ComponentScan");
}
 
开发者ID:spring-io,项目名称:initializr,代码行数:16,代码来源:ProjectGeneratorTests.java

示例14: simpleNameTest

import org.springframework.context.annotation.ComponentScan; //导入依赖的package包/类
@Test
public void simpleNameTest() throws ParseException {
    AnnotatedWith annotatedWith = new AnnotatedWith(ComponentScan.class);
    Assert.assertTrue(annotatedWith.evaluate(compilationUnit));
}
 
开发者ID:paypal,项目名称:butterfly,代码行数:6,代码来源:AnnotatedWithTest.java

示例15: includedInBasePackageClasses

import org.springframework.context.annotation.ComponentScan; //导入依赖的package包/类
/**
 * This method allows you to use @ComponentScan for your unit testing but
 * it limits the scope of the classes found to the class specified in
 * the @ComponentScan annotation.
 * <p>
 * Without using this method, the default behavior of @ComponentScan is
 * to actually scan in the package of the class specified rather than
 * only the class. This can cause extra classes to be loaded which causes
 * the classes these extra classes depend on to be loaded. The end effect
 * is often most of the project gets loaded.
 * <p>
 * In order to use this method properly, you must do the following: <li>
 * - Specify @ComponentScan with basePackageClasses, includeFilters, and
 * useDefaultFilters=true.  See the following example.
 * <p>
 * <pre>
 *     @ComponentScan(basePackageClasses={AffinityGroupServiceImpl.class, EventUtils.class},
 *     includeFilters={@Filter(value=TestConfiguration.Library.class, type=FilterType.CUSTOM)},
 *     useDefaultFilters=false)
 * </pre>
 * <p>
 * - Create a Library class and use that to call this method.  See the
 * following example.  The Library class you define here is the Library
 * class being added in the filter above.
 * <p>
 * <pre>
 * public static class Library implements TypeFilter {
 *      @Override
 *      public boolean match(MetadataReader mdr, MetadataReaderFactory arg1) throws IOException {
 *          ComponentScan cs = TestConfiguration.class.getAnnotation(ComponentScan.class);
 *          return SpringUtils.includedInBasePackageClasses(mdr.getClassMetadata().getClassName(), cs);
 *      }
 * }
 * </pre>
 *
 * @param clazzName name of the class that should be included in the Spring components
 * @param cs        ComponentScan annotation that was declared on the configuration
 * @return
 */
public static boolean includedInBasePackageClasses(final String clazzName, final ComponentScan cs) {
    final Class<?> clazzToCheck;
    try {
        clazzToCheck = Class.forName(clazzName);
    } catch (final ClassNotFoundException e) {
        throw new CloudRuntimeException("Unable to find " + clazzName);
    }
    final Class<?>[] clazzes = cs.basePackageClasses();
    for (final Class<?> clazz : clazzes) {
        if (clazzToCheck.isAssignableFrom(clazz)) {
            return true;
        }
    }
    return false;
}
 
开发者ID:MissionCriticalCloud,项目名称:cosmic,代码行数:55,代码来源:SpringUtils.java


注:本文中的org.springframework.context.annotation.ComponentScan类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。