org.springframework.context.annotation.ComponentScan Java Examples

The following examples show how to use org.springframework.context.annotation.ComponentScan. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Panama.java    From panama with MIT License 6 votes vote down vote up
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");
}
 
Example #2
Source File: IntegrationTestConfiguration.java    From cloudstack with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example #3
Source File: DoradoAutoConfiguration.java    From dorado with Apache License 2.0 5 votes vote down vote up
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()));
		}
	}
}
 
Example #4
Source File: AffinityApiUnitTest.java    From cloudstack with Apache License 2.0 4 votes vote down vote up
@Override
public boolean match(MetadataReader mdr, MetadataReaderFactory arg1) throws IOException {
    ComponentScan cs = TestConfiguration.class.getAnnotation(ComponentScan.class);
    return SpringUtils.includedInBasePackageClasses(mdr.getClassMetadata().getClassName(), cs);
}
 
Example #5
Source File: ImplicitPlannerTest.java    From cloudstack with Apache License 2.0 4 votes vote down vote up
@Override
public boolean match(MetadataReader mdr, MetadataReaderFactory arg1) throws IOException {
    ComponentScan cs = TestConfiguration.class.getAnnotation(ComponentScan.class);
    return SpringUtils.includedInBasePackageClasses(mdr.getClassMetadata().getClassName(), cs);
}
 
Example #6
Source File: AclOnPrivateGwTest.java    From cloudstack with Apache License 2.0 4 votes vote down vote up
@Override
public boolean match(MetadataReader mdr, MetadataReaderFactory arg1) throws IOException {
    ComponentScan cs = TestConfiguration.class.getAnnotation(ComponentScan.class);
    return SpringUtils.includedInBasePackageClasses(mdr.getClassMetadata().getClassName(), cs);
}
 
Example #7
Source File: ChildTestConfiguration.java    From cloudstack with Apache License 2.0 4 votes vote down vote up
@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);
}
 
Example #8
Source File: FirstFitPlannerTest.java    From cloudstack with Apache License 2.0 4 votes vote down vote up
@Override
public boolean match(MetadataReader mdr, MetadataReaderFactory arg1) throws IOException {
    ComponentScan cs = TestConfiguration.class.getAnnotation(ComponentScan.class);
    return SpringUtils.includedInBasePackageClasses(mdr.getClassMetadata().getClassName(), cs);
}
 
Example #9
Source File: UserVmCloneSettingDaoTestConfiguration.java    From cloudstack with Apache License 2.0 4 votes vote down vote up
@Override
public boolean match(MetadataReader mdr, MetadataReaderFactory arg1) throws IOException {
    mdr.getClassMetadata().getClassName();
    ComponentScan cs = UserVmCloneSettingDaoTestConfiguration.class.getAnnotation(ComponentScan.class);
    return SpringUtils.includedInBasePackageClasses(mdr.getClassMetadata().getClassName(), cs);
}
 
Example #10
Source File: UserVmDaoTestConfiguration.java    From cloudstack with Apache License 2.0 4 votes vote down vote up
@Override
public boolean match(MetadataReader mdr, MetadataReaderFactory arg1) throws IOException {
    mdr.getClassMetadata().getClassName();
    ComponentScan cs = UserVmDaoTestConfiguration.class.getAnnotation(ComponentScan.class);
    return SpringUtils.includedInBasePackageClasses(mdr.getClassMetadata().getClassName(), cs);
}
 
Example #11
Source File: DeploymentPlanningManagerImplTest.java    From cloudstack with Apache License 2.0 4 votes vote down vote up
@Override
public boolean match(MetadataReader mdr, MetadataReaderFactory arg1) throws IOException {
    ComponentScan cs = TestConfiguration.class.getAnnotation(ComponentScan.class);
    return SpringUtils.includedInBasePackageClasses(mdr.getClassMetadata().getClassName(), cs);
}
 
Example #12
Source File: SecurityGroupManagerTestConfiguration.java    From cloudstack with Apache License 2.0 4 votes vote down vote up
@Override
public boolean match(MetadataReader mdr, MetadataReaderFactory arg1) throws IOException {
    mdr.getClassMetadata().getClassName();
    ComponentScan cs = SecurityGroupManagerTestConfiguration.class.getAnnotation(ComponentScan.class);
    return SpringUtils.includedInBasePackageClasses(mdr.getClassMetadata().getClassName(), cs);
}
 
Example #13
Source File: TemplateManagerImplTest.java    From cloudstack with Apache License 2.0 4 votes vote down vote up
@Override
public boolean match(MetadataReader mdr, MetadataReaderFactory arg1) throws IOException {
    ComponentScan cs = TestConfiguration.class.getAnnotation(ComponentScan.class);
    return SpringUtils.includedInBasePackageClasses(mdr.getClassMetadata().getClassName(), cs);
}
 
Example #14
Source File: NetworkACLManagerTest.java    From cloudstack with Apache License 2.0 4 votes vote down vote up
@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);
}
 
Example #15
Source File: VpcTestConfiguration.java    From cloudstack with Apache License 2.0 4 votes vote down vote up
@Override
public boolean match(MetadataReader mdr, MetadataReaderFactory arg1) throws IOException {
    mdr.getClassMetadata().getClassName();
    ComponentScan cs = VpcTestConfiguration.class.getAnnotation(ComponentScan.class);
    return SpringUtils.includedInBasePackageClasses(mdr.getClassMetadata().getClassName(), cs);
}
 
Example #16
Source File: NetworkACLServiceTest.java    From cloudstack with Apache License 2.0 4 votes vote down vote up
@Override
public boolean match(MetadataReader mdr, MetadataReaderFactory arg1) throws IOException {
    mdr.getClassMetadata().getClassName();
    ComponentScan cs = NetworkACLTestConfiguration.class.getAnnotation(ComponentScan.class);
    return SpringUtils.includedInBasePackageClasses(mdr.getClassMetadata().getClassName(), cs);
}
 
Example #17
Source File: SnapshotDaoTestConfiguration.java    From cloudstack with Apache License 2.0 4 votes vote down vote up
@Override
public boolean match(MetadataReader mdr, MetadataReaderFactory arg1) throws IOException {
    mdr.getClassMetadata().getClassName();
    ComponentScan cs = SnapshotDaoTestConfiguration.class.getAnnotation(ComponentScan.class);
    return SpringUtils.includedInBasePackageClasses(mdr.getClassMetadata().getClassName(), cs);
}
 
Example #18
Source File: StoragePoolDaoTestConfiguration.java    From cloudstack with Apache License 2.0 4 votes vote down vote up
@Override
public boolean match(MetadataReader mdr, MetadataReaderFactory arg1) throws IOException {
    mdr.getClassMetadata().getClassName();
    ComponentScan cs = StoragePoolDaoTestConfiguration.class.getAnnotation(ComponentScan.class);
    return SpringUtils.includedInBasePackageClasses(mdr.getClassMetadata().getClassName(), cs);
}
 
Example #19
Source File: VmwareStorageMotionStrategyTest.java    From cloudstack with Apache License 2.0 4 votes vote down vote up
@Override
public boolean match(MetadataReader mdr, MetadataReaderFactory arg1) throws IOException {
    ComponentScan cs = TestConfiguration.class.getAnnotation(ComponentScan.class);
    return SpringUtils.includedInBasePackageClasses(mdr.getClassMetadata().getClassName(), cs);
}
 
Example #20
Source File: VmwareDatacenterApiUnitTest.java    From cloudstack with Apache License 2.0 4 votes vote down vote up
@Override
public boolean match(MetadataReader mdr, MetadataReaderFactory arg1) throws IOException {
    ComponentScan cs = TestConfiguration.class.getAnnotation(ComponentScan.class);
    return SpringUtils.includedInBasePackageClasses(mdr.getClassMetadata().getClassName(), cs);
}
 
Example #21
Source File: DedicatedApiUnitTest.java    From cloudstack with Apache License 2.0 4 votes vote down vote up
@Override
public boolean match(MetadataReader mdr, MetadataReaderFactory arg1) throws IOException {
    ComponentScan cs = TestConfiguration.class.getAnnotation(ComponentScan.class);
    return SpringUtils.includedInBasePackageClasses(mdr.getClassMetadata().getClassName(), cs);
}
 
Example #22
Source File: GloboDnsElementTest.java    From cloudstack with Apache License 2.0 4 votes vote down vote up
@Override
public boolean match(MetadataReader mdr, MetadataReaderFactory arg1) throws IOException {
    ComponentScan cs = TestConfiguration.class.getAnnotation(ComponentScan.class);
    return SpringUtils.includedInBasePackageClasses(mdr.getClassMetadata().getClassName(), cs);
}
 
Example #23
Source File: LbChildTestConfiguration.java    From cloudstack with Apache License 2.0 4 votes vote down vote up
@Override
public boolean match(MetadataReader mdr, MetadataReaderFactory arg1) throws IOException {
    mdr.getClassMetadata().getClassName();
    ComponentScan cs = LbChildTestConfiguration.class.getAnnotation(ComponentScan.class);
    return SpringUtils.includedInBasePackageClasses(mdr.getClassMetadata().getClassName(), cs);
}
 
Example #24
Source File: ElementChildTestConfiguration.java    From cloudstack with Apache License 2.0 4 votes vote down vote up
@Override
public boolean match(MetadataReader mdr, MetadataReaderFactory arg1) throws IOException {
    mdr.getClassMetadata().getClassName();
    ComponentScan cs = ElementChildTestConfiguration.class.getAnnotation(ComponentScan.class);
    return SpringUtils.includedInBasePackageClasses(mdr.getClassMetadata().getClassName(), cs);
}
 
Example #25
Source File: IntegrationTestConfiguration.java    From cloudstack with Apache License 2.0 4 votes vote down vote up
@Override
public boolean match(MetadataReader mdr, MetadataReaderFactory arg1) throws IOException {
    String clsname = mdr.getClassMetadata().getClassName();
    ComponentScan cs = IntegrationTestConfiguration.class.getAnnotation(ComponentScan.class);
    return includedInBasePackageClasses(clsname, cs);
}
 
Example #26
Source File: AclOnPrivateGwTest.java    From cosmic with Apache License 2.0 4 votes vote down vote up
@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);
}
 
Example #27
Source File: AnnotatedWithTest.java    From butterfly with MIT License 4 votes vote down vote up
@Test
public void simpleNameTest() throws ParseException {
    AnnotatedWith annotatedWith = new AnnotatedWith(ComponentScan.class);
    Assert.assertTrue(annotatedWith.evaluate(compilationUnit));
}
 
Example #28
Source File: SpringUtils.java    From cosmic with Apache License 2.0 4 votes vote down vote up
/**
 * 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;
}
 
Example #29
Source File: VMSnapshotStrategyTest.java    From cosmic with Apache License 2.0 4 votes vote down vote up
@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);
}
 
Example #30
Source File: FirstFitPlannerTest.java    From cosmic with Apache License 2.0 4 votes vote down vote up
@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);
}