private String getBeanName(Class<?> clazz) { Component component = clazz.getAnnotation(Component.class); if (component != null) return component.value(); Repository repository = clazz.getAnnotation(Repository.class); if (repository != null) return repository.value(); Service service = clazz.getAnnotation(Service.class); if (service != null) return service.value(); Controller controller = clazz.getAnnotation(Controller.class); if (controller != null) return controller.value(); return null; }
@Override public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { Repository annotation = AnnotationUtils.findAnnotation(bean.getClass(), Repository.class); if (annotation == null) { return bean; } Class<?> supperClass = bean.getClass().getSuperclass(); if (!LodsveMongoRepository.class.equals(supperClass)) { return bean; } Class<?> domainClass = GenericUtils.getGenericParameter0(bean.getClass()); MongoRepositoryFactory repositoryFactory = context.getBean(MongoRepositoryFactory.class); MongoEntityInformation information = repositoryFactory.getEntityInformation(domainClass); BeanWrapper beanWrapper = new BeanWrapperImpl(bean); beanWrapper.setPropertyValue("entityInformation", information); return bean; }
public void resetAndReconfigure(boolean debug) { SingularContextSetup.reset(); ApplicationContextMock applicationContext = new ApplicationContextMock(); ServiceRegistryLocator.setup(new SpringServiceRegistry()); new ApplicationContextProvider().setApplicationContext(applicationContext); registerBeanFactories(applicationContext); registerAnnotated(applicationContext, Named.class); registerAnnotated(applicationContext, Service.class); registerAnnotated(applicationContext, Component.class); registerAnnotated(applicationContext, Repository.class); registerMockitoTestClassMocksAndSpies(applicationContext); getLogger().info("Contexto configurado com os beans: "); if (debug) { applicationContext.listAllBeans().forEach( b -> getLogger().info(b) ); } }
private String getBeanName(final Class<?> clazz) { final Component componentAnno = clazz.getAnnotation(Component.class); if(componentAnno != null && !componentAnno.value().isEmpty()) { return componentAnno.value(); } final Service serviceAnno = clazz.getAnnotation(Service.class); if(serviceAnno != null && !serviceAnno.value().isEmpty()) { return serviceAnno.value(); } final Repository repositoryAnno = clazz.getAnnotation(Repository.class); if(repositoryAnno != null && !repositoryAnno.value().isEmpty()) { return repositoryAnno.value(); } final Controller controllerAnno = clazz.getAnnotation(Controller.class); if(controllerAnno != null && !controllerAnno.value().isEmpty()) { return controllerAnno.value(); } // ステレオタイプのアノテーションでBean名の指定がない場合は、クラス名の先頭を小文字にした名称とする。 return uncapitalize(clazz.getSimpleName()); }
private Annotation getSpringClassAnnotation(Class clazz) { Annotation classAnnotation = AnnotationUtils.findAnnotation(clazz, Component.class); if (classAnnotation == null) { classAnnotation = AnnotationUtils.findAnnotation(clazz, Controller.class); } if (classAnnotation == null) { classAnnotation = AnnotationUtils.findAnnotation(clazz, RestController.class); } if (classAnnotation == null) { classAnnotation = AnnotationUtils.findAnnotation(clazz, Service.class); } if (classAnnotation == null) { classAnnotation = AnnotationUtils.findAnnotation(clazz, Repository.class); } return classAnnotation; }
public DwrClassPathBeanDefinitionScanner(BeanDefinitionRegistry registry) { super(registry, false); addExcludeFilter(new AnnotationTypeFilter(Component.class)); addExcludeFilter(new AnnotationTypeFilter(Service.class)); addExcludeFilter(new AnnotationTypeFilter(Repository.class)); addExcludeFilter(new AnnotationTypeFilter(Controller.class)); setScopedProxyMode(ScopedProxyMode.INTERFACES); }
private URI copyE5Data(URI contextUri, EntityManager em) { final URI targetContext = generateContextUri(); try { final org.eclipse.rdf4j.repository.Repository targetRepository = em .unwrap(org.eclipse.rdf4j.repository.Repository.class); try (final RepositoryConnection targetConnection = targetRepository.getConnection()) { targetConnection.begin(); dataDao.getRepositoryData(contextUri, new StatementCopyingHandler(targetConnection, targetContext)); targetConnection.commit(); } } catch (RepositoryException e) { LOG.error("Unable to copy E5Data.", e); } return targetContext; }
@Test public void testWithComponentAnnotationOnly() { ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(false); provider.addIncludeFilter(new AnnotationTypeFilter(Component.class)); provider.addExcludeFilter(new AnnotationTypeFilter(Repository.class)); provider.addExcludeFilter(new AnnotationTypeFilter(Service.class)); provider.addExcludeFilter(new AnnotationTypeFilter(Controller.class)); Set<BeanDefinition> candidates = provider.findCandidateComponents(TEST_BASE_PACKAGE); assertEquals(2, candidates.size()); assertTrue(containsBeanClass(candidates, NamedComponent.class)); assertTrue(containsBeanClass(candidates, ServiceInvocationCounter.class)); assertFalse(containsBeanClass(candidates, FooServiceImpl.class)); assertFalse(containsBeanClass(candidates, StubFooDao.class)); assertFalse(containsBeanClass(candidates, NamedStubDao.class)); }
@Override protected void addPersistenceExceptionTranslation(ProxyFactory pf, PersistenceExceptionTranslator pet) { if (AnnotationUtils.findAnnotation(pf.getTargetClass(), Repository.class) != null) { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); bf.registerBeanDefinition("peti", new RootBeanDefinition(PersistenceExceptionTranslationInterceptor.class)); bf.registerSingleton("pet", pet); pf.addAdvice((PersistenceExceptionTranslationInterceptor) bf.getBean("peti")); } }
public static String getRepositoryId(Annotation[] annotations) { String repoId = ""; if (annotations == null) { return repoId; } for (Annotation anno : annotations) { if (anno instanceof Repository) { repoId = ((Repository)anno).value(); } } return repoId; }
private String getTargetType(MethodInvocation methodInvocation) { Class<?> targetType = methodInvocation.getThis().getClass(); if (hasAnnotation(targetType, Controller.class)) { return "Controller"; } else if (hasAnnotation(targetType, Service.class)) { return "Service"; } else if (hasAnnotation(targetType, Repository.class)) { return "Repository"; } else { return "Trace"; } }
private Annotation getSpringClassAnnotation(Class clazz){ Annotation classAnnotation = AnnotationUtils.findAnnotation(clazz, Component.class); if (classAnnotation == null) { classAnnotation = AnnotationUtils.findAnnotation(clazz, Controller.class); } if (classAnnotation == null) { classAnnotation = AnnotationUtils.findAnnotation(clazz, Service.class); } if (classAnnotation == null) { classAnnotation = AnnotationUtils.findAnnotation(clazz, Repository.class); } return classAnnotation; }
private Type[] getRepostoryGenericInterfaces(final RepositoryMetadata metadata) { final Type[] repositoryGenericInterfaces = metadata.getRepositoryInterface().getGenericInterfaces(); if (repositoryGenericInterfaces.length < 1) { throw new RuntimeException("Don't recognize " + metadata.getRepositoryInterface() + " as a Repository (does it extend " + Repository.class.getName() + "?)"); } return repositoryGenericInterfaces; }
private Map<String, BeanDefinition> findMyBatisBeanDefinitions() { String[] enumsLocations = attributes.getStringArray(Constant.ENUMS_LOCATIONS_ATTRIBUTE_NAME); String[] basePackages = attributes.getStringArray(Constant.BASE_PACKAGES_ATTRIBUTE_NAME); AnnotationAttributes[] plugins = attributes.getAnnotationArray(Constant.PLUGINS_ATTRIBUTE_NAME); if (ArrayUtils.isEmpty(enumsLocations)) { enumsLocations = findDefaultPackage(metadata); } if (ArrayUtils.isEmpty(basePackages)) { basePackages = findDefaultPackage(metadata); } Map<String, BeanDefinition> beanDefinitions = new HashMap<>(16); BeanDefinitionBuilder sqlSessionFactoryBean = BeanDefinitionBuilder.genericBeanDefinition(SqlSessionFactoryBean.class); if (useFlyway) { sqlSessionFactoryBean.addDependsOn(Constant.FLYWAY_BEAN_NAME); } sqlSessionFactoryBean.addPropertyReference("dataSource", Constant.DATA_SOURCE_BEAN_NAME); sqlSessionFactoryBean.addPropertyValue("mapperLocations", "classpath*:/META-INF/mybatis/**/*Mapper.xml"); sqlSessionFactoryBean.addPropertyValue("configLocation", "classpath:/META-INF/mybatis/mybatis.xml"); TypeHandlerScanner scanner = new TypeHandlerScanner(); sqlSessionFactoryBean.addPropertyValue("typeHandlers", scanner.find(StringUtils.join(enumsLocations, ","))); List<Interceptor> pluginsList = new ArrayList<>(plugins.length); List<Class<? extends Interceptor>> clazz = new ArrayList<>(plugins.length); for (AnnotationAttributes plugin : plugins) { Class<? extends Interceptor> pluginClass = plugin.getClass("value"); AnnotationAttributes[] params = plugin.getAnnotationArray("params"); clazz.add(pluginClass); Interceptor interceptor = BeanUtils.instantiate(pluginClass); BeanWrapper beanWrapper = new BeanWrapperImpl(interceptor); for (AnnotationAttributes param : params) { String key = param.getString("key"); String value = param.getString("value"); PropertyDescriptor descriptor = beanWrapper.getPropertyDescriptor(key); Method writeMethod = descriptor.getWriteMethod(); Method readMethod = descriptor.getReadMethod(); writeMethod.setAccessible(true); try { Class<?> returnType = readMethod.getReturnType(); Object valueObject = value; if (Integer.class.equals(returnType) || int.class.equals(returnType)) { valueObject = Integer.valueOf(value); } else if (Long.class.equals(returnType) || long.class.equals(returnType)) { valueObject = Long.valueOf(value); } else if (Boolean.class.equals(returnType) || boolean.class.equals(returnType)) { valueObject = Boolean.valueOf(value); } else if (Double.class.equals(returnType) || double.class.equals(returnType)) { valueObject = Double.valueOf(value); } writeMethod.invoke(interceptor, valueObject); } catch (Exception e) { e.printStackTrace(); } } pluginsList.add(interceptor); } if (!clazz.contains(PaginationInterceptor.class)) { pluginsList.add(BeanUtils.instantiate(PaginationInterceptor.class)); } sqlSessionFactoryBean.addPropertyValue("plugins", pluginsList); BeanDefinitionBuilder scannerConfigurerBean = BeanDefinitionBuilder.genericBeanDefinition(MapperScannerConfigurer.class); scannerConfigurerBean.addPropertyValue("basePackage", StringUtils.join(basePackages, ",")); scannerConfigurerBean.addPropertyValue("annotationClass", Repository.class); scannerConfigurerBean.addPropertyValue("sqlSessionFactoryBeanName", "sqlSessionFactory"); beanDefinitions.put("sqlSessionFactory", sqlSessionFactoryBean.getBeanDefinition()); beanDefinitions.put("mapperScannerConfigurer", scannerConfigurerBean.getBeanDefinition()); return beanDefinitions; }
protected void addPersistenceExceptionTranslation(ProxyFactory pf, PersistenceExceptionTranslator pet) { pf.addAdvisor(new PersistenceExceptionTranslationAdvisor(pet, Repository.class)); }
public void reloadEvent(String typename, Class<?> clazz) { synchronized (lock) { log.trace("Hot reloading - checking if this is a Spring bean: {}", typename); boolean startReloading = false; if (AnnotationUtils.findAnnotation(clazz, Repository.class) != null || ClassUtils.isAssignable(clazz, org.springframework.data.repository.Repository.class)) { log.trace("{} is a Spring Repository", typename); repositories.add(clazz); startReloading = true; } else if (AnnotationUtils.findAnnotation(clazz, Service.class) != null) { log.trace("{} is a Spring Service", typename); services.add(clazz); startReloading = true; } else if (AnnotationUtils.findAnnotation(clazz, Controller.class) != null || AnnotationUtils.findAnnotation(clazz, RestController.class) != null) { log.trace("{} is a Spring Controller", typename); controllers.add(clazz); startReloading = true; } else if (AnnotationUtils.findAnnotation(clazz, Component.class) != null) { log.trace("{} is a Spring Component", typename); components.add(clazz); startReloading = true; } else if (typename.startsWith("com.exteso.lab.pf.domain")) { log.trace("{} is in the JPA package, checking if it is an entity", typename); if (AnnotationUtils.findAnnotation(clazz, Entity.class) != null) { log.trace("{} is a JPA Entity", typename); entities.add(clazz); startReloading = true; } } else if (typename.startsWith("com.exteso.lab.pf.web.rest.dto")) { log.debug("{} is a REST DTO", typename); dtos.add(clazz); startReloading = true; } if (startReloading) { hotReloadTriggered = true; isWaitingForNewClasses = true; } } }