下面列出了怎么用org.springframework.context.annotation.ComponentScan的API类实例代码及写法,或者点击链接到github查看源代码。
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");
}
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;
}
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()));
}
}
}
@Test
public void simpleNameTest() throws ParseException {
AnnotatedWith annotatedWith = new AnnotatedWith(ComponentScan.class);
Assert.assertTrue(annotatedWith.evaluate(compilationUnit));
}
/**
* 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;
}
@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);
}
@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);
}
@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);
}
@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);
}
@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);
}
@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);
}
@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);
}
@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);
}
@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);
}
@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);
}
@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);
}
@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);
}
@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);
}
@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);
}
/**
* 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);
}
@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);
}
@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);
}
@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);
}
@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);
}
@Override
public boolean match(MetadataReader mdr, MetadataReaderFactory arg1) throws IOException {
ComponentScan cs = TestConfiguration.class.getAnnotation(ComponentScan.class);
return SpringUtils.includedInBasePackageClasses(mdr.getClassMetadata().getClassName(), cs);
}
@Override
public boolean match(MetadataReader mdr, MetadataReaderFactory arg1) throws IOException {
ComponentScan cs = TestConfiguration.class.getAnnotation(ComponentScan.class);
return SpringUtils.includedInBasePackageClasses(mdr.getClassMetadata().getClassName(), cs);
}
@Override
public boolean match(MetadataReader mdr, MetadataReaderFactory arg1) throws IOException {
ComponentScan cs = TestConfiguration.class.getAnnotation(ComponentScan.class);
return SpringUtils.includedInBasePackageClasses(mdr.getClassMetadata().getClassName(), cs);
}
@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);
}
@Override
public boolean match(MetadataReader mdr, MetadataReaderFactory arg1) throws IOException {
ComponentScan cs = TestConfiguration.class.getAnnotation(ComponentScan.class);
return SpringUtils.includedInBasePackageClasses(mdr.getClassMetadata().getClassName(), cs);
}