Java Code Examples for org.springframework.stereotype.Repository

The following examples show how to use org.springframework.stereotype.Repository. These examples are extracted from open source projects. 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
@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(3, candidates.size());
	assertTrue(containsBeanClass(candidates, NamedComponent.class));
	assertTrue(containsBeanClass(candidates, ServiceInvocationCounter.class));
	assertTrue(containsBeanClass(candidates, BarComponent.class));
	assertFalse(containsBeanClass(candidates, FooServiceImpl.class));
	assertFalse(containsBeanClass(candidates, StubFooDao.class));
	assertFalse(containsBeanClass(candidates, NamedStubDao.class));
}
 
Example 2
@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(3, candidates.size());
	assertTrue(containsBeanClass(candidates, NamedComponent.class));
	assertTrue(containsBeanClass(candidates, ServiceInvocationCounter.class));
	assertTrue(containsBeanClass(candidates, BarComponent.class));
	assertFalse(containsBeanClass(candidates, FooServiceImpl.class));
	assertFalse(containsBeanClass(candidates, StubFooDao.class));
	assertFalse(containsBeanClass(candidates, NamedStubDao.class));
}
 
Example 3
Source Project: tephra   Source File: ClassReloaderImpl.java    License: MIT License 6 votes vote down vote up
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;
}
 
Example 4
private void testCustomSupportedIncludeAndExcludeFilter(ClassPathScanningCandidateComponentProvider provider,
		Class<? extends BeanDefinition> expectedBeanDefinitionType) {
	provider.addIncludeFilter(new AnnotationTypeFilter(Component.class));
	provider.addExcludeFilter(new AnnotationTypeFilter(Service.class));
	provider.addExcludeFilter(new AnnotationTypeFilter(Repository.class));
	Set<BeanDefinition> candidates = provider.findCandidateComponents(TEST_BASE_PACKAGE);
	assertTrue(containsBeanClass(candidates, NamedComponent.class));
	assertTrue(containsBeanClass(candidates, ServiceInvocationCounter.class));
	assertTrue(containsBeanClass(candidates, BarComponent.class));
	assertEquals(3, candidates.size());
	assertBeanDefinitionType(candidates, expectedBeanDefinitionType);
}
 
Example 5
@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"));
	}
}
 
Example 6
Source Project: archunit-examples   Source File: SpringCodingRulesTest.java    License: MIT License 5 votes vote down vote up
@Test
public void springSingletonComponentsShouldOnlyHaveFinalFields() {
  ArchRule rule = ArchRuleDefinition.classes()
    .that().areAnnotatedWith(Service.class)
    .or().areAnnotatedWith(Component.class)
    .and().areNotAnnotatedWith(ConfigurationProperties.class)
    .or().areAnnotatedWith(Controller.class)
    .or().areAnnotatedWith(RestController.class)
    .or().areAnnotatedWith(Repository.class)
    .should().haveOnlyFinalFields();
  rule.check(classes);
}
 
Example 7
@Test
public void repositoryClassesShouldBeAnnotatedWithRepositoryAnnotation() {
  ArchRule rule = ArchRuleDefinition.classes()
    .that().haveSimpleNameEndingWith("Repository")
    .and().areNotInterfaces()
    .should().beAnnotatedWith(Repository.class);
  rule.check(classes);
}
 
Example 8
@Test
public void noClassesWithRepositoryAnnotationShouldResideOutsideOfSecondaryAdaptersPackages() {
  ArchRule rule = ArchRuleDefinition.noClasses()
    .that().areAnnotatedWith(Repository.class)
    .should().resideOutsideOfPackage(SECONDARY_ADAPTERS_PACKAGES);
  rule.check(classes);
}
 
Example 9
private void testCustomSupportedIncludeAndExcludeFilter(ClassPathScanningCandidateComponentProvider provider,
		Class<? extends BeanDefinition> expectedBeanDefinitionType) {
	provider.addIncludeFilter(new AnnotationTypeFilter(Component.class));
	provider.addExcludeFilter(new AnnotationTypeFilter(Service.class));
	provider.addExcludeFilter(new AnnotationTypeFilter(Repository.class));
	Set<BeanDefinition> candidates = provider.findCandidateComponents(TEST_BASE_PACKAGE);
	assertTrue(containsBeanClass(candidates, NamedComponent.class));
	assertTrue(containsBeanClass(candidates, ServiceInvocationCounter.class));
	assertTrue(containsBeanClass(candidates, BarComponent.class));
	assertEquals(3, candidates.size());
	assertBeanDefinitionType(candidates, expectedBeanDefinitionType);
}
 
Example 10
@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"));
	}
}
 
Example 11
Source Project: TrackRay   Source File: ModuleClassLoader.java    License: GNU General Public License v3.0 5 votes vote down vote up
/**
 * 方法描述 判断class对象是否带有spring的注解
 * @method isSpringBeanClass
 * @param cla jar中的每一个class
 * @return true 是spring bean   false 不是spring bean
 */
public boolean isSpringBeanClass(Class<?> cla){
    if(cla==null){
        return false;
    }
    //是否是接口
    if(cla.isInterface()){
        return false;
    }

    //是否是抽象类
    if( Modifier.isAbstract(cla.getModifiers())){
        return false;
    }

    if(cla.getAnnotation(Component.class)!=null){
        return true;
    }
    if(cla.getAnnotation(Repository.class)!=null){
        return true;
    }
    if(cla.getAnnotation(Service.class)!=null){
        return true;
    }

    return false;
}
 
Example 12
Source Project: PedestrianDetectionSystem   Source File: RootConfig.java    License: Apache License 2.0 5 votes vote down vote up
@Bean
public MapperScannerConfigurer initMapperScannerConfigurer(){
    MapperScannerConfigurer msc = new MapperScannerConfigurer();
    msc.setBasePackage("com.webprague");
    msc.setSqlSessionFactoryBeanName("sqlSessionFactory");
    msc.setAnnotationClass(Repository.class);
    return msc;
}
 
Example 13
@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));
}
 
Example 14
@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"));
	}
}
 
Example 15
Source Project: bamboobsc   Source File: AspectConstants.java    License: Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 16
Source Project: waltz   Source File: DaoArchitectureCompliance.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void daosNeedRepositoryAnnotation() {
    ArchRule rule = classes().that()
            .areNotInterfaces()
            .and()
            .resideInAPackage("..khartec..")
            .and()
            .haveNameMatching(".*Dao")
            .should()
            .beAnnotatedWith(Repository.class);
    rule.check(waltzAndJavaUtilClasses);
}
 
Example 17
Source Project: super-csv-annotation   Source File: SpringBeanFactory.java    License: Apache License 2.0 5 votes vote down vote up
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名の指定がない場合は、クラス名の先頭を小文字にした名称とする。
        String simpleName = uncapitalize(clazz.getSimpleName());

        String[] names = applicationContext.getBeanNamesForType(clazz);
        if(names.length == 0) {
            return simpleName;
        }

        // 複数存在する場合、候補の中から一番シンプルな形式の名前で一致するものを選択する
        for(String name : names) {
            if(name.equals(simpleName)) {
                return name;
            }
        }

        // 見つからない場合は、候補の先頭のものにする。
        return names[0];
    }
 
Example 18
protected void addPersistenceExceptionTranslation(ProxyFactory pf, PersistenceExceptionTranslator pet) {
	pf.addAdvisor(new PersistenceExceptionTranslationAdvisor(pet, Repository.class));
}
 
Example 19
@Override
public boolean filter(Class<?> aClass) {
    return AnnotationsUtils.haveAnnotations(aClass, false, Repository.class);
}
 
Example 20
protected void addPersistenceExceptionTranslation(ProxyFactory pf, PersistenceExceptionTranslator pet) {
	pf.addAdvisor(new PersistenceExceptionTranslationAdvisor(pet, Repository.class));
}
 
Example 21
protected void addPersistenceExceptionTranslation(ProxyFactory pf, PersistenceExceptionTranslator pet) {
	pf.addAdvisor(new PersistenceExceptionTranslationAdvisor(pet, Repository.class));
}
 
Example 22
Source Project: xlsmapper   Source File: SpringBeanFactory.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * クラスタイプから、候補となるSpringBean名を取得する
 * @param targetClass SpringBeanのクラスタイプ
 * @return SpringBean名
 */
private String getBeanName(final Class<?> targetClass) {

    final Component componentAnno = targetClass.getAnnotation(Component.class);
    if(componentAnno != null && !componentAnno.value().isEmpty()) {
        return componentAnno.value();
    }

    final Service serviceAnno = targetClass.getAnnotation(Service.class);
    if(serviceAnno != null && !serviceAnno.value().isEmpty()) {
        return serviceAnno.value();
    }

    final Repository repositoryAnno = targetClass.getAnnotation(Repository.class);
    if(repositoryAnno != null && !repositoryAnno.value().isEmpty()) {
        return repositoryAnno.value();
    }

    final Controller controllerAnno = targetClass.getAnnotation(Controller.class);
    if(controllerAnno != null && !controllerAnno.value().isEmpty()) {
        return controllerAnno.value();
    }

    // ステレオタイプのアノテーションでBean名の指定がない場合は、クラス名の先頭を小文字にした名称とする。
    String simpleName = Utils.uncapitalize(targetClass.getSimpleName());

    String[] names = applicationContext.getBeanNamesForType(targetClass);
    if(names.length == 0) {
        return simpleName;
    }

    // 複数存在する場合、候補の中から一番シンプルな形式の名前で一致するものを選択する
    for(String name : names) {
        if(name.equals(simpleName)) {
            return name;
        }
    }

    // 見つからない場合は、候補の先頭のものにする。
    return names[0];
}
 
Example 23
public ReactivePersistenceExceptionTranslationPostProcessor() {

		this(Repository.class);
	}