Java Code Examples for org.springframework.stereotype.Component

The following code examples are extracted from open source projects. You can click to vote up the examples that are useful to you.

Example 1

From project spring-js, under directory /src/main/java/org/springframework/context/annotation/.

Source file: ConfigurationClassBeanDefinitionReader.java

  22 
vote

/** 
 * Check whether the given bean definition is a candidate for a configuration class, and mark it accordingly.
 * @param beanDef the bean definition to check
 * @param metadataReaderFactory the current factory in use by the caller
 * @return whether the candidate qualifies as (any kind of) configuration class
 */
public static boolean checkConfigurationClassCandidate(BeanDefinition beanDef,MetadataReaderFactory metadataReaderFactory){
  AnnotationMetadata metadata=null;
  if (beanDef instanceof AbstractBeanDefinition && ((AbstractBeanDefinition)beanDef).hasBeanClass()) {
    metadata=new StandardAnnotationMetadata(((AbstractBeanDefinition)beanDef).getBeanClass());
  }
 else {
    String className=beanDef.getBeanClassName();
    if (className != null) {
      try {
        MetadataReader metadataReader=metadataReaderFactory.getMetadataReader(className);
        metadata=metadataReader.getAnnotationMetadata();
      }
 catch (      IOException ex) {
        if (logger.isDebugEnabled()) {
          logger.debug("Could not find class file for introspecting factory methods: " + className,ex);
        }
        return false;
      }
    }
  }
  if (metadata != null) {
    if (metadata.isAnnotated(Configuration.class.getName())) {
      beanDef.setAttribute(CONFIGURATION_CLASS_ATTRIBUTE,CONFIGURATION_CLASS_FULL);
      return true;
    }
 else     if (metadata.isAnnotated(Component.class.getName()) || metadata.hasAnnotatedMethods(Bean.class.getName())) {
      beanDef.setAttribute(CONFIGURATION_CLASS_ATTRIBUTE,CONFIGURATION_CLASS_LITE);
      return true;
    }
  }
  return false;
}
 

Example 2

From project spring-js, under directory /src/test/java/org/springframework/context/annotation/.

Source file: ClassPathScanningCandidateComponentProviderTests.java

  22 
vote

@Test public void testExcludeTakesPrecedence(){
  ClassPathScanningCandidateComponentProvider provider=new ClassPathScanningCandidateComponentProvider(false);
  provider.addIncludeFilter(new AnnotationTypeFilter(Component.class));
  provider.addIncludeFilter(new AssignableTypeFilter(FooServiceImpl.class));
  provider.addExcludeFilter(new AssignableTypeFilter(FooService.class));
  Set<BeanDefinition> candidates=provider.findCandidateComponents(TEST_BASE_PACKAGE);
  assertEquals(5,candidates.size());
  assertTrue(containsBeanClass(candidates,NamedComponent.class));
  assertTrue(containsBeanClass(candidates,ServiceInvocationCounter.class));
  assertFalse(containsBeanClass(candidates,FooServiceImpl.class));
}
 

Example 3

From project spring-js, under directory /src/test/java/org/springframework/context/annotation/.

Source file: ClassPathScanningCandidateComponentProviderTests.java

  20 
vote

@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 4

From project spring-js, under directory /src/test/java/org/springframework/context/annotation/.

Source file: ClassPathScanningCandidateComponentProviderTests.java

  20 
vote

@Test public void testWithMultipleMatchingFilters(){
  ClassPathScanningCandidateComponentProvider provider=new ClassPathScanningCandidateComponentProvider(false);
  provider.addIncludeFilter(new AnnotationTypeFilter(Component.class));
  provider.addIncludeFilter(new AssignableTypeFilter(FooServiceImpl.class));
  Set<BeanDefinition> candidates=provider.findCandidateComponents(TEST_BASE_PACKAGE);
  assertEquals(6,candidates.size());
  assertTrue(containsBeanClass(candidates,NamedComponent.class));
  assertTrue(containsBeanClass(candidates,ServiceInvocationCounter.class));
  assertTrue(containsBeanClass(candidates,FooServiceImpl.class));
}
 

Example 5

From project Blitz, under directory /src/com/laxser/blitz/web/impl/module/.

Source file: ModulesBuilderImpl.java

  19 
vote

private void registerBeanDefinitions(XmlWebApplicationContext context,List<Class<?>> classes){
  DefaultListableBeanFactory bf=(DefaultListableBeanFactory)context.getBeanFactory();
  String[] definedClasses=new String[bf.getBeanDefinitionCount()];
  String[] definitionNames=bf.getBeanDefinitionNames();
  for (int i=0; i < definedClasses.length; i++) {
    String name=definitionNames[i];
    definedClasses[i]=bf.getBeanDefinition(name).getBeanClassName();
  }
  for (  Class<?> clazz : classes) {
    if (!isCandidate(clazz)) {
      continue;
    }
    String clazzName=clazz.getName();
    if (ArrayUtils.contains(definedClasses,clazzName)) {
      if (logger.isDebugEnabled()) {
        logger.debug("Ignores bean definition because it has been exist in context: " + clazz.getName());
      }
      continue;
    }
    String beanName=null;
    if (StringUtils.isEmpty(beanName) && clazz.isAnnotationPresent(Component.class)) {
      beanName=clazz.getAnnotation(Component.class).value();
    }
    if (StringUtils.isEmpty(beanName) && clazz.isAnnotationPresent(Resource.class)) {
      beanName=clazz.getAnnotation(Resource.class).name();
    }
    if (StringUtils.isEmpty(beanName) && clazz.isAnnotationPresent(Service.class)) {
      beanName=clazz.getAnnotation(Service.class).value();
    }
    if (StringUtils.isEmpty(beanName)) {
      beanName=AUTO_BEAN_NAME_PREFIX + clazz.getName();
    }
    bf.registerBeanDefinition(beanName,new AnnotatedGenericBeanDefinition(clazz));
  }
}
 

Example 6

From project Blitz, under directory /src/com/laxser/blitz/web/instruction/.

Source file: InstructionExecutorImpl.java

  19 
vote

/** 
 * @param inv
 * @param instruction
 * @return
 * @throws StackOverflowError
 */
private Instruction translatesToInstructionObject(InvocationBean inv,Object instruction) throws StackOverflowError {
  int count=0;
  while (!(instruction instanceof Instruction)) {
    if (count++ > 50) {
      throw new StackOverflowError("Unable to parse the instruction to an" + " Instruction object less than " + count + " times. Is the instruction"+ " that returned by your controller"+ " action is right?");
    }
    if (Thread.interrupted() || instruction == null) {
      return null;
    }
 else {
      if (instruction.getClass() != String.class && !ClassUtils.isPrimitiveOrWrapper(instruction.getClass()) && instruction.getClass().getComponentType() == null && instruction.getClass().getAnnotation(Component.class) != null) {
        SpringUtils.autowire(instruction,inv.getApplicationContext());
      }
      instruction=parseInstruction(inv,instruction);
    }
  }
  return (Instruction)instruction;
}
 

Example 7

From project spring-js, under directory /src/main/java/org/springframework/context/annotation/.

Source file: ClassPathScanningCandidateComponentProvider.java

  19 
vote

/** 
 * Register the default filter for  {@link Component @Component}. <p>This will implicitly register all annotations that have the {@link Component @Component} meta-annotation including the{@link Repository @Repository},  {@link Service @Service}, and {@link Controller @Controller} stereotype annotations.<p>Also supports Java EE 6's  {@link javax.annotation.ManagedBean} andJSR-330's  {@link javax.inject.Named} annotations, if available.
 */
@SuppressWarnings("unchecked") protected void registerDefaultFilters(){
  this.includeFilters.add(new AnnotationTypeFilter(Component.class));
  ClassLoader cl=ClassPathScanningCandidateComponentProvider.class.getClassLoader();
  try {
    this.includeFilters.add(new AnnotationTypeFilter(((Class<? extends Annotation>)cl.loadClass("javax.annotation.ManagedBean")),false));
    logger.info("JSR-250 'javax.annotation.ManagedBean' found and supported for component scanning");
  }
 catch (  ClassNotFoundException ex) {
  }
  try {
    this.includeFilters.add(new AnnotationTypeFilter(((Class<? extends Annotation>)cl.loadClass("javax.inject.Named")),false));
    logger.info("JSR-330 'javax.inject.Named' annotation found and supported for component scanning");
  }
 catch (  ClassNotFoundException ex) {
  }
}
 

Example 8

From project SpringSecurityDemo, under directory /src/main/java/com/github/peholmst/springsecuritydemo/ui/.

Source file: SpringSecurityDemoApp.java

  19 
vote

@SuppressWarnings("serial") @Override public void init(){
  if (logger.isDebugEnabled()) {
    logger.debug("Initializing application [" + this + "]");
  }
  getContext().addTransactionListener(this);
  loginView=new LoginView(this,authenticationManager);
  loginView.setSizeFull();
  setTheme("SpringSecurityDemo");
  final Window loginWindow=new Window(getMessage("app.title",getVersion()),loginView);
  setMainWindow(loginWindow);
  loginView.addListener(new com.vaadin.ui.Component.Listener(){
    @Override public void componentEvent(    Event event){
      if (event instanceof LoginView.LoginEvent) {
        if (logger.isDebugEnabled()) {
          logger.debug("User logged on [" + ((LoginView.LoginEvent)event).getAuthentication() + "]");
        }
        setUser(((LoginView.LoginEvent)event).getAuthentication());
        SecurityContextHolder.getContext().setAuthentication(((LoginView.LoginEvent)event).getAuthentication());
        removeWindow(loginWindow);
        loginView=null;
        mainView=new MainView(SpringSecurityDemoApp.this,categoryService);
        mainView.setSizeFull();
        setMainWindow(new Window(getMessage("app.title",getVersion()),mainView));
      }
    }
  }
);
}