2222import org .apache .commons .logging .Log ;
2323import org .apache .commons .logging .LogFactory ;
2424
25- import org .springframework .aop .support .AopUtils ;
26- import org .springframework .beans .factory .config .BeanDefinition ;
2725import org .springframework .boot .autoconfigure .AutoConfiguration ;
28- import org .springframework .boot .autoconfigure .SpringBootApplication ;
2926import org .springframework .boot .autoconfigure .condition .ConditionalOnMissingBean ;
3027import org .springframework .context .ApplicationContext ;
3128import org .springframework .context .annotation .Bean ;
32- import org .springframework .context .annotation .ClassPathScanningCandidateComponentProvider ;
3329import org .springframework .core .MethodIntrospector ;
3430import org .springframework .core .annotation .AnnotatedElementUtils ;
35- import org .springframework .shell .core .ShellConfigurationException ;
3631import org .springframework .shell .core .command .Command ;
3732import org .springframework .shell .core .command .CommandRegistry ;
3833import org .springframework .shell .core .command .annotation .support .CommandFactoryBean ;
3934import org .springframework .shell .core .utils .Utils ;
40- import org .springframework .util . ClassUtils ;
35+ import org .springframework .stereotype . Component ;
4136import org .springframework .util .ReflectionUtils ;
4237
4338@ AutoConfiguration
@@ -61,33 +56,21 @@ private void registerProgrammaticCommands(ApplicationContext applicationContext,
6156 }
6257
6358 private void registerAnnotatedCommands (ApplicationContext applicationContext , CommandRegistry commandRegistry ) {
64- Map <String , Object > springBootApps = applicationContext .getBeansWithAnnotation (SpringBootApplication .class );
65- Class <?> mainClass = AopUtils .getTargetClass (springBootApps .values ().iterator ().next ());
66- String mainPackage = ClassUtils .getPackageName (mainClass );
67- ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider (true );
68- Set <BeanDefinition > candidateComponents = scanner .findCandidateComponents (mainPackage );
69- for (BeanDefinition candidateComponent : candidateComponents ) {
70- String className = candidateComponent .getBeanClassName ();
71- if (className == null ) {
72- log .warn (String .format ("Skipping candidate component %s with null class name" , candidateComponent ));
59+ Map <String , Object > components = applicationContext .getBeansWithAnnotation (Component .class );
60+ for (Object candidateComponent : components .values ()) {
61+ Class <?> type = candidateComponent .getClass ();
62+ String className = type .getName ();
63+ if (className .startsWith ("org.springframework.boot" )) {
7364 continue ;
7465 }
75- else {
76- log .debug ("Registering commands from component: " + className );
77- }
78- try {
79- Class <?> cls = ClassUtils .forName (className , applicationContext .getClassLoader ());
80- ReflectionUtils .MethodFilter filter = method -> AnnotatedElementUtils .hasAnnotation (method ,
81- org .springframework .shell .core .command .annotation .Command .class );
82- Set <Method > methods = MethodIntrospector .selectMethods (cls , filter );
83- for (Method method : methods ) {
84- CommandFactoryBean factoryBean = new CommandFactoryBean (method );
85- factoryBean .setApplicationContext (applicationContext );
86- commandRegistry .registerCommand (factoryBean .getObject ());
87- }
88- }
89- catch (ClassNotFoundException e ) {
90- throw new ShellConfigurationException ("Unable to configure commands from class " + className , e );
66+ log .debug ("Registering commands from component: " + className );
67+ ReflectionUtils .MethodFilter filter = method -> AnnotatedElementUtils .hasAnnotation (method ,
68+ org .springframework .shell .core .command .annotation .Command .class );
69+ Set <Method > methods = MethodIntrospector .selectMethods (type , filter );
70+ for (Method method : methods ) {
71+ CommandFactoryBean factoryBean = new CommandFactoryBean (method );
72+ factoryBean .setApplicationContext (applicationContext );
73+ commandRegistry .registerCommand (factoryBean .getObject ());
9174 }
9275 }
9376 }
0 commit comments