org.springframework.stereotype.Repository Java Examples

The following examples show how to use org.springframework.stereotype.Repository. 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
Source File: ClassPathScanningCandidateComponentProviderTests.java    From spring-analysis-note with MIT License 7 votes vote down vote up
@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
Source File: ClassReloaderImpl.java    From tephra with 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 #3
Source File: ClassPathScanningCandidateComponentProviderTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@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 #4
Source File: PersistenceExceptionTranslationInterceptorTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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 #5
Source File: SpringBeanFactory.java    From super-csv-annotation with 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 #6
Source File: DaoArchitectureCompliance.java    From waltz with 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 #7
Source File: AspectConstants.java    From bamboobsc with 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 #8
Source File: PersistenceExceptionTranslationInterceptorTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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 #9
Source File: ClassPathScanningCandidateComponentProviderTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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 #10
Source File: RootConfig.java    From PedestrianDetectionSystem with 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 #11
Source File: ModuleClassLoader.java    From TrackRay with 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 File: ClassPathScanningCandidateComponentProviderTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
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 #13
Source File: SecondaryAdaptersComponentsTest.java    From archunit-examples with MIT License 5 votes vote down vote up
@Test
public void noClassesWithRepositoryAnnotationShouldResideOutsideOfSecondaryAdaptersPackages() {
  ArchRule rule = ArchRuleDefinition.noClasses()
    .that().areAnnotatedWith(Repository.class)
    .should().resideOutsideOfPackage(SECONDARY_ADAPTERS_PACKAGES);
  rule.check(classes);
}
 
Example #14
Source File: SecondaryAdaptersComponentsTest.java    From archunit-examples with MIT License 5 votes vote down vote up
@Test
public void repositoryClassesShouldBeAnnotatedWithRepositoryAnnotation() {
  ArchRule rule = ArchRuleDefinition.classes()
    .that().haveSimpleNameEndingWith("Repository")
    .and().areNotInterfaces()
    .should().beAnnotatedWith(Repository.class);
  rule.check(classes);
}
 
Example #15
Source File: SpringCodingRulesTest.java    From archunit-examples with 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 #16
Source File: PersistenceExceptionTranslationInterceptorTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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 #17
Source File: ClassPathScanningCandidateComponentProviderTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
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 #18
Source File: PersistenceExceptionTranslationAdvisorTests.java    From java-technology-stack with MIT License 4 votes vote down vote up
protected void addPersistenceExceptionTranslation(ProxyFactory pf, PersistenceExceptionTranslator pet) {
	pf.addAdvisor(new PersistenceExceptionTranslationAdvisor(pet, Repository.class));
}
 
Example #19
Source File: RepositoryGroup.java    From springboot-plugin-framework-parent with Apache License 2.0 4 votes vote down vote up
@Override
public boolean filter(Class<?> aClass) {
    return AnnotationsUtils.haveAnnotations(aClass, false, Repository.class);
}
 
Example #20
Source File: PersistenceExceptionTranslationAdvisorTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
protected void addPersistenceExceptionTranslation(ProxyFactory pf, PersistenceExceptionTranslator pet) {
	pf.addAdvisor(new PersistenceExceptionTranslationAdvisor(pet, Repository.class));
}
 
Example #21
Source File: PersistenceExceptionTranslationAdvisorTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
protected void addPersistenceExceptionTranslation(ProxyFactory pf, PersistenceExceptionTranslator pet) {
	pf.addAdvisor(new PersistenceExceptionTranslationAdvisor(pet, Repository.class));
}
 
Example #22
Source File: SpringBeanFactory.java    From xlsmapper with 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
Source File: ReactivePersistenceExceptionTranslationPostProcessor.java    From sdn-rx with Apache License 2.0 2 votes vote down vote up
public ReactivePersistenceExceptionTranslationPostProcessor() {

		this(Repository.class);
	}