类org.springframework.context.annotation.ComponentScan源码实例Demo

下面列出了怎么用org.springframework.context.annotation.ComponentScan的API类实例代码及写法,或者点击链接到github查看源代码。

源代码1 项目: panama   文件: Panama.java
public static void load(Object ...args) {
    log.info("panama server loading");
    Class mainClass = deduceMainApplicationClass();
    List<String> basePackageList = new ArrayList<>();
    ComponentScan componentScan = (ComponentScan)mainClass.getAnnotation(ComponentScan.class);
    if (null != componentScan) {
        for (String basePackage : componentScan.basePackages()) {
            if (basePackage.length() > 0) {
                basePackageList.add(basePackage);
            }
        }
    }

    if (basePackageList.size() == 0) {
        basePackageList.add(mainClass.getPackage().getName() + ".*");
    }

    basePackageList.add(PANAMA_SPRING_SCAN_PACKAGE);
    applicationContext = new AnnotationConfigApplicationContext(basePackageList.toArray(new String[basePackageList.size()]));
    log.info("panama server loading success");
}
 
源代码2 项目: cloudstack   文件: IntegrationTestConfiguration.java
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;
}
 
源代码3 项目: dorado   文件: DoradoAutoConfiguration.java
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()));
		}
	}
}
 
源代码4 项目: butterfly   文件: AnnotatedWithTest.java
@Test
public void simpleNameTest() throws ParseException {
    AnnotatedWith annotatedWith = new AnnotatedWith(ComponentScan.class);
    Assert.assertTrue(annotatedWith.evaluate(compilationUnit));
}
 
源代码5 项目: cosmic   文件: SpringUtils.java
/**
 * 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.
 *
 * <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.
 *
 * <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;
}
 
源代码6 项目: cosmic   文件: VMSnapshotStrategyTest.java
@Override
public boolean match(final MetadataReader mdr, final MetadataReaderFactory arg1) throws IOException {
    mdr.getClassMetadata().getClassName();
    final ComponentScan cs = TestConfiguration.class.getAnnotation(ComponentScan.class);
    return SpringUtils.includedInBasePackageClasses(mdr.getClassMetadata().getClassName(), cs);
}
 
源代码7 项目: cosmic   文件: FirstFitPlannerTest.java
@Override
public boolean match(final MetadataReader mdr, final MetadataReaderFactory arg1) throws IOException {
    final ComponentScan cs = TestConfiguration.class.getAnnotation(ComponentScan.class);
    return SpringUtils.includedInBasePackageClasses(mdr.getClassMetadata().getClassName(), cs);
}
 
源代码8 项目: cosmic   文件: AffinityGroupServiceImplTest.java
@Override
public boolean match(final MetadataReader mdr, final MetadataReaderFactory arg1) throws IOException {
    final ComponentScan cs = TestConfiguration.class.getAnnotation(ComponentScan.class);
    return SpringUtils.includedInBasePackageClasses(mdr.getClassMetadata().getClassName(), cs);
}
 
源代码9 项目: cosmic   文件: AffinityApiUnitTest.java
@Override
public boolean match(final MetadataReader mdr, final MetadataReaderFactory arg1) throws IOException {
    final ComponentScan cs = TestConfiguration.class.getAnnotation(ComponentScan.class);
    return SpringUtils.includedInBasePackageClasses(mdr.getClassMetadata().getClassName(), cs);
}
 
源代码10 项目: cosmic   文件: ImplicitPlannerTest.java
@Override
public boolean match(final MetadataReader mdr, final MetadataReaderFactory arg1) throws IOException {
    final ComponentScan cs = TestConfiguration.class.getAnnotation(ComponentScan.class);
    return SpringUtils.includedInBasePackageClasses(mdr.getClassMetadata().getClassName(), cs);
}
 
源代码11 项目: cosmic   文件: TemplateManagerImplTest.java
@Override
public boolean match(final MetadataReader mdr, final MetadataReaderFactory arg1) throws IOException {
    final ComponentScan cs = TestConfiguration.class.getAnnotation(ComponentScan.class);
    return SpringUtils.includedInBasePackageClasses(mdr.getClassMetadata().getClassName(), cs);
}
 
源代码12 项目: cosmic   文件: NetworkACLManagerTest.java
@Override
public boolean match(final MetadataReader mdr, final MetadataReaderFactory arg1) throws IOException {
    mdr.getClassMetadata().getClassName();
    final ComponentScan cs = NetworkACLTestConfiguration.class.getAnnotation(ComponentScan.class);
    return SpringUtils.includedInBasePackageClasses(mdr.getClassMetadata().getClassName(), cs);
}
 
源代码13 项目: cosmic   文件: VpcTestConfiguration.java
@Override
public boolean match(final MetadataReader mdr, final MetadataReaderFactory arg1) throws IOException {
    mdr.getClassMetadata().getClassName();
    final ComponentScan cs = VpcTestConfiguration.class.getAnnotation(ComponentScan.class);
    return SpringUtils.includedInBasePackageClasses(mdr.getClassMetadata().getClassName(), cs);
}
 
源代码14 项目: cosmic   文件: NetworkACLServiceTest.java
@Override
public boolean match(final MetadataReader mdr, final MetadataReaderFactory arg1) throws IOException {
    mdr.getClassMetadata().getClassName();
    final ComponentScan cs = NetworkACLTestConfiguration.class.getAnnotation(ComponentScan.class);
    return SpringUtils.includedInBasePackageClasses(mdr.getClassMetadata().getClassName(), cs);
}
 
源代码15 项目: cosmic   文件: AclOnPrivateGwTest.java
@Override
public boolean match(final MetadataReader mdr, final MetadataReaderFactory arg1) throws IOException {
    final ComponentScan cs = TestConfiguration.class.getAnnotation(ComponentScan.class);
    return SpringUtils.includedInBasePackageClasses(mdr.getClassMetadata().getClassName(), cs);
}
 
源代码16 项目: cosmic   文件: DedicatedApiUnitTest.java
@Override
public boolean match(final MetadataReader mdr, final MetadataReaderFactory arg1) throws IOException {
    final ComponentScan cs = TestConfiguration.class.getAnnotation(ComponentScan.class);
    return SpringUtils.includedInBasePackageClasses(mdr.getClassMetadata().getClassName(), cs);
}
 
源代码17 项目: cosmic   文件: SnapshotDaoTestConfiguration.java
@Override
public boolean match(final MetadataReader mdr, final MetadataReaderFactory arg1) throws IOException {
    mdr.getClassMetadata().getClassName();
    final ComponentScan cs = SnapshotDaoTestConfiguration.class.getAnnotation(ComponentScan.class);
    return SpringUtils.includedInBasePackageClasses(mdr.getClassMetadata().getClassName(), cs);
}
 
源代码18 项目: cosmic   文件: StoragePoolDaoTestConfiguration.java
@Override
public boolean match(final MetadataReader mdr, final MetadataReaderFactory arg1) throws IOException {
    mdr.getClassMetadata().getClassName();
    final ComponentScan cs = StoragePoolDaoTestConfiguration.class.getAnnotation(ComponentScan.class);
    return SpringUtils.includedInBasePackageClasses(mdr.getClassMetadata().getClassName(), cs);
}
 
源代码19 项目: cosmic   文件: ChildTestConfiguration.java
@Override
public boolean match(final MetadataReader mdr, final MetadataReaderFactory arg1) {
    mdr.getClassMetadata().getClassName();
    final ComponentScan cs = ChildTestConfiguration.class.getAnnotation(ComponentScan.class);
    return SpringUtils.includedInBasePackageClasses(mdr.getClassMetadata().getClassName(), cs);
}
 
源代码20 项目: cloudstack   文件: SpringUtils.java
/**
 * 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.
 *
 * 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.
 *
 * 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.
 *
 * <pre>
 *     @ComponentScan(basePackageClasses={AffinityGroupServiceImpl.class, EventUtils.class},
 *     includeFilters={@Filter(value=TestConfiguration.Library.class, type=FilterType.CUSTOM)},
 *     useDefaultFilters=false)
 * </pre>
 *
 *   - 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.
 *
 * <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(String clazzName, ComponentScan cs) {
    Class<?> clazzToCheck;
    try {
        clazzToCheck = Class.forName(clazzName);
    } catch (ClassNotFoundException e) {
        throw new CloudRuntimeException("Unable to find " + clazzName);
    }
    Class<?>[] clazzes = cs.basePackageClasses();
    for (Class<?> clazz : clazzes) {
        if (clazzToCheck.isAssignableFrom(clazz)) {
            return true;
        }
    }
    return false;
}
 
@Override
public boolean match(MetadataReader mdr, MetadataReaderFactory arg1) throws IOException {
    mdr.getClassMetadata().getClassName();
    ComponentScan cs = StorageAllocatorTestConfiguration.class.getAnnotation(ComponentScan.class);
    return SpringUtils.includedInBasePackageClasses(mdr.getClassMetadata().getClassName(), cs);
}
 
源代码22 项目: cloudstack   文件: ChildTestConfiguration.java
@Override
public boolean match(MetadataReader mdr, MetadataReaderFactory arg1) throws IOException {
    mdr.getClassMetadata().getClassName();
    ComponentScan cs = ChildTestConfiguration.class.getAnnotation(ComponentScan.class);
    return SpringUtils.includedInBasePackageClasses(mdr.getClassMetadata().getClassName(), cs);
}
 
源代码23 项目: cloudstack   文件: VMSnapshotStrategyTest.java
@Override
public boolean match(MetadataReader mdr, MetadataReaderFactory arg1) throws IOException {
    mdr.getClassMetadata().getClassName();
    ComponentScan cs = TestConfiguration.class.getAnnotation(ComponentScan.class);
    return SpringUtils.includedInBasePackageClasses(mdr.getClassMetadata().getClassName(), cs);
}
 
源代码24 项目: cloudstack   文件: UsageManagerTestConfiguration.java
@Override
public boolean match(MetadataReader mdr, MetadataReaderFactory arg1) throws IOException {
    mdr.getClassMetadata().getClassName();
    ComponentScan cs = UsageManagerTestConfiguration.class.getAnnotation(ComponentScan.class);
    return SpringUtils.includedInBasePackageClasses(mdr.getClassMetadata().getClassName(), cs);
}
 
源代码25 项目: cloudstack   文件: ApplicationLoadBalancerTest.java
@Override
public boolean match(MetadataReader mdr, MetadataReaderFactory arg1) throws IOException {
    mdr.getClassMetadata().getClassName();
    ComponentScan cs = TestConfiguration.class.getAnnotation(ComponentScan.class);
    return SpringUtils.includedInBasePackageClasses(mdr.getClassMetadata().getClassName(), cs);
}
 
源代码26 项目: cloudstack   文件: AffinityGroupServiceImplTest.java
@Override
public boolean match(MetadataReader mdr, MetadataReaderFactory arg1) throws IOException {
    ComponentScan cs = TestConfiguration.class.getAnnotation(ComponentScan.class);
    return SpringUtils.includedInBasePackageClasses(mdr.getClassMetadata().getClassName(), cs);
}
 
源代码27 项目: cloudstack   文件: AffinityApiUnitTest.java
@Override
public boolean match(MetadataReader mdr, MetadataReaderFactory arg1) throws IOException {
    ComponentScan cs = TestConfiguration.class.getAnnotation(ComponentScan.class);
    return SpringUtils.includedInBasePackageClasses(mdr.getClassMetadata().getClassName(), cs);
}
 
源代码28 项目: cloudstack   文件: AclOnPrivateGwTest.java
@Override
public boolean match(MetadataReader mdr, MetadataReaderFactory arg1) throws IOException {
    ComponentScan cs = TestConfiguration.class.getAnnotation(ComponentScan.class);
    return SpringUtils.includedInBasePackageClasses(mdr.getClassMetadata().getClassName(), cs);
}
 
源代码29 项目: cloudstack   文件: ChildTestConfiguration.java
@Override
public boolean match(MetadataReader mdr, MetadataReaderFactory arg1) throws IOException {
    mdr.getClassMetadata().getClassName();
    ComponentScan cs = ChildTestConfiguration.class.getAnnotation(ComponentScan.class);
    return SpringUtils.includedInBasePackageClasses(mdr.getClassMetadata().getClassName(), cs);
}
 
源代码30 项目: cloudstack   文件: FirstFitPlannerTest.java
@Override
public boolean match(MetadataReader mdr, MetadataReaderFactory arg1) throws IOException {
    ComponentScan cs = TestConfiguration.class.getAnnotation(ComponentScan.class);
    return SpringUtils.includedInBasePackageClasses(mdr.getClassMetadata().getClassName(), cs);
}
 
 类方法
 同包方法