| 
1 |  | -package com.vaadin.flow.spring;  | 
 | 1 | +package com.vaadin.flow.spring.springnative;  | 
2 | 2 | 
 
  | 
3 | 3 | import java.util.ArrayList;  | 
4 | 4 | import java.util.Collection;  | 
5 | 5 | import java.util.HashSet;  | 
6 | 6 | import java.util.List;  | 
 | 7 | +import java.util.Set;  | 
7 | 8 | 
 
  | 
8 | 9 | import org.reflections.Reflections;  | 
9 | 10 | import org.slf4j.Logger;  | 
 | 
13 | 14 | import org.springframework.beans.factory.BeanFactory;  | 
14 | 15 | import org.springframework.beans.factory.aot.BeanFactoryInitializationAotContribution;  | 
15 | 16 | import org.springframework.beans.factory.aot.BeanFactoryInitializationAotProcessor;  | 
 | 17 | +import org.springframework.beans.factory.config.BeanDefinition;  | 
16 | 18 | import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;  | 
 | 19 | +import org.springframework.beans.factory.support.AbstractBeanDefinition;  | 
 | 20 | +import org.springframework.beans.factory.support.BeanDefinitionBuilder;  | 
 | 21 | +import org.springframework.beans.factory.support.BeanDefinitionRegistry;  | 
17 | 22 | import org.springframework.boot.autoconfigure.AutoConfigurationPackages;  | 
18 | 23 | 
 
  | 
19 | 24 | import com.vaadin.flow.component.Component;  | 
 | 
26 | 31 | import com.vaadin.flow.router.RouteAlias;  | 
27 | 32 | import com.vaadin.flow.router.RouterLayout;  | 
28 | 33 | 
 
  | 
29 |  | -class VaadinBeanFactoryInitializationAotProcessor  | 
 | 34 | +public class VaadinBeanFactoryInitializationAotProcessor  | 
30 | 35 |         implements BeanFactoryInitializationAotProcessor {  | 
31 | 36 | 
 
  | 
32 | 37 |     private final Logger logger = LoggerFactory.getLogger(getClass());  | 
33 | 38 | 
 
  | 
 | 39 | +    public static class Marker {  | 
 | 40 | + | 
 | 41 | +    }  | 
 | 42 | + | 
34 | 43 |     @Override  | 
35 | 44 |     public BeanFactoryInitializationAotContribution processAheadOfTime(  | 
36 | 45 |             ConfigurableListableBeanFactory beanFactory) {  | 
 | 46 | +        // Find and register @Route classes so they can be created as beans at  | 
 | 47 | +        // runtime  | 
 | 48 | +        if (beanFactory instanceof BeanDefinitionRegistry) {  | 
 | 49 | +            findAndRegisterRoutes(  | 
 | 50 | +                    (BeanDefinitionRegistry & BeanFactory) beanFactory);  | 
 | 51 | +        } else {  | 
 | 52 | +            logger.error(  | 
 | 53 | +                    "Unable to register @Route classes as beans because the used bean factory is of type {} which does not implement {}",  | 
 | 54 | +                    beanFactory.getClass().getName(),  | 
 | 55 | +                    BeanDefinitionRegistry.class.getName());  | 
 | 56 | +        }  | 
 | 57 | + | 
37 | 58 |         return (generationContext, beanFactoryInitializationCode) -> {  | 
38 | 59 |             var hints = generationContext.getRuntimeHints();  | 
39 | 60 |             for (var pkg : getPackages(beanFactory)) {  | 
@@ -78,6 +99,60 @@ public BeanFactoryInitializationAotContribution processAheadOfTime(  | 
78 | 99 |         };  | 
79 | 100 |     }  | 
80 | 101 | 
 
  | 
 | 102 | +    private static List<String> getPackagesWithRoutes(BeanFactory beanFactory) {  | 
 | 103 | +        List<String> packages = new ArrayList<String>();  | 
 | 104 | +        packages.add("com.vaadin");  | 
 | 105 | +        packages.addAll(AutoConfigurationPackages.get(beanFactory));  | 
 | 106 | +        return packages;  | 
 | 107 | +    }  | 
 | 108 | + | 
 | 109 | +    private <T extends BeanFactory & BeanDefinitionRegistry> void findAndRegisterRoutes(  | 
 | 110 | +            T beanFactory) {  | 
 | 111 | +        String markerBeanName = Marker.class.getName();  | 
 | 112 | +        logger.debug("Finding and registering routes");  | 
 | 113 | + | 
 | 114 | +        if (beanFactory.containsBeanDefinition(markerBeanName)) {  | 
 | 115 | +            logger.debug("Routes already registered");  | 
 | 116 | +            return;  | 
 | 117 | +        }  | 
 | 118 | + | 
 | 119 | +        Set<String> registeredClasses = new HashSet<>();  | 
 | 120 | +        for (String beanName : beanFactory.getBeanDefinitionNames()) {  | 
 | 121 | +            // Routes can be manually registered using @Component.  | 
 | 122 | +            // We should not register those again  | 
 | 123 | +            BeanDefinition def = beanFactory.getBeanDefinition(beanName);  | 
 | 124 | +            if (def.getBeanClassName() != null) {  | 
 | 125 | +                registeredClasses.add(def.getBeanClassName());  | 
 | 126 | +            }  | 
 | 127 | +        }  | 
 | 128 | + | 
 | 129 | +        for (String pkg : getPackagesWithRoutes(beanFactory)) {  | 
 | 130 | +            logger.debug("Scanning for @{} or @{} annotated beans in {}",  | 
 | 131 | +                    Route.class.getSimpleName(),  | 
 | 132 | +                    RouteAlias.class.getSimpleName(), pkg);  | 
 | 133 | +            var reflections = new Reflections(pkg);  | 
 | 134 | +            for (var c : getRouteTypesFor(reflections, pkg)) {  | 
 | 135 | +                if (registeredClasses.contains(c.getName())) {  | 
 | 136 | +                    logger.debug(  | 
 | 137 | +                            "Skipping route class {} as it has already been registered as a bean",  | 
 | 138 | +                            c.getName());  | 
 | 139 | +                    continue;  | 
 | 140 | +                }  | 
 | 141 | + | 
 | 142 | +                logger.debug("Registering a bean for route class {}",  | 
 | 143 | +                        c.getName());  | 
 | 144 | +                AbstractBeanDefinition beanDefinition = BeanDefinitionBuilder  | 
 | 145 | +                        .rootBeanDefinition(c).setScope("prototype")  | 
 | 146 | +                        .getBeanDefinition();  | 
 | 147 | +                beanFactory.registerBeanDefinition(c.getName(), beanDefinition);  | 
 | 148 | +            }  | 
 | 149 | +        }  | 
 | 150 | + | 
 | 151 | +        beanFactory.registerBeanDefinition(markerBeanName, BeanDefinitionBuilder  | 
 | 152 | +                .rootBeanDefinition(Marker.class).getBeanDefinition());  | 
 | 153 | + | 
 | 154 | +    }  | 
 | 155 | + | 
81 | 156 |     private static Collection<Class<?>> getRouteTypesFor(  | 
82 | 157 |             Reflections reflections, String packageName) {  | 
83 | 158 |         var routeTypes = new HashSet<Class<?>>();  | 
 | 
0 commit comments