public class ClassPathBeanDefinitionScanner extends ClassPathScanningCandidateComponentProvider
BeanFactory
or ApplicationContext
).
Candidate classes are detected through configurable type filters. The
default filters include classes that are annotated with Spring's
@Component
,
@Repository
,
@Service
, or
@Controller
stereotype.
Also supports Java EE 6's javax.annotation.ManagedBean
and
JSR-330's javax.inject.Named
annotations, if available.
AnnotationConfigApplicationContext.scan(java.lang.String...)
,
Component
,
Repository
,
Service
,
Controller
Modifier and Type | Field and Description |
---|---|
private java.lang.String[] |
autowireCandidatePatterns |
private BeanDefinitionDefaults |
beanDefinitionDefaults |
private BeanNameGenerator |
beanNameGenerator |
private boolean |
includeAnnotationConfig |
private BeanDefinitionRegistry |
registry |
private ScopeMetadataResolver |
scopeMetadataResolver |
DEFAULT_RESOURCE_PATTERN, logger
Constructor and Description |
---|
ClassPathBeanDefinitionScanner(BeanDefinitionRegistry registry)
Create a new
ClassPathBeanDefinitionScanner for the given bean factory. |
ClassPathBeanDefinitionScanner(BeanDefinitionRegistry registry,
boolean useDefaultFilters)
Create a new
ClassPathBeanDefinitionScanner for the given bean factory. |
ClassPathBeanDefinitionScanner(BeanDefinitionRegistry registry,
boolean useDefaultFilters,
Environment environment)
Create a new
ClassPathBeanDefinitionScanner for the given bean factory and
using the given Environment when evaluating bean definition profile metadata. |
Modifier and Type | Method and Description |
---|---|
protected boolean |
checkCandidate(java.lang.String beanName,
BeanDefinition beanDefinition)
Check the given candidate's bean name, determining whether the corresponding
bean definition needs to be registered or conflicts with an existing definition.
|
protected java.util.Set<BeanDefinitionHolder> |
doScan(java.lang.String... basePackages)
Perform a scan within the specified base packages,
returning the registered bean definitions.
|
private static Environment |
getOrCreateEnvironment(BeanDefinitionRegistry registry)
Get the Environment from the given registry if possible, otherwise return a new
StandardEnvironment.
|
BeanDefinitionRegistry |
getRegistry()
Return the BeanDefinitionRegistry that this scanner operates on.
|
protected boolean |
isCompatible(BeanDefinition newDefinition,
BeanDefinition existingDefinition)
Determine whether the given new bean definition is compatible with
the given existing bean definition.
|
protected void |
postProcessBeanDefinition(AbstractBeanDefinition beanDefinition,
java.lang.String beanName)
Apply further settings to the given bean definition,
beyond the contents retrieved from scanning the component class.
|
protected void |
registerBeanDefinition(BeanDefinitionHolder definitionHolder,
BeanDefinitionRegistry registry)
Register the specified bean with the given registry.
|
int |
scan(java.lang.String... basePackages)
Perform a scan within the specified base packages.
|
void |
setAutowireCandidatePatterns(java.lang.String[] autowireCandidatePatterns)
Set the name-matching patterns for determining autowire candidates.
|
void |
setBeanDefinitionDefaults(BeanDefinitionDefaults beanDefinitionDefaults)
Set the defaults to use for detected beans.
|
void |
setBeanNameGenerator(BeanNameGenerator beanNameGenerator)
Set the BeanNameGenerator to use for detected bean classes.
|
void |
setIncludeAnnotationConfig(boolean includeAnnotationConfig)
Specify whether to register annotation config post-processors.
|
void |
setScopedProxyMode(ScopedProxyMode scopedProxyMode)
Specify the proxy behavior for non-singleton scoped beans.
|
void |
setScopeMetadataResolver(ScopeMetadataResolver scopeMetadataResolver)
Set the ScopeMetadataResolver to use for detected bean classes.
|
addExcludeFilter, addIncludeFilter, clearCache, findCandidateComponents, getEnvironment, getMetadataReaderFactory, getResourceLoader, isCandidateComponent, isCandidateComponent, registerDefaultFilters, resetFilters, resolveBasePackage, setEnvironment, setMetadataReaderFactory, setResourceLoader, setResourcePattern
private final BeanDefinitionRegistry registry
private BeanDefinitionDefaults beanDefinitionDefaults
private java.lang.String[] autowireCandidatePatterns
private BeanNameGenerator beanNameGenerator
private ScopeMetadataResolver scopeMetadataResolver
private boolean includeAnnotationConfig
public ClassPathBeanDefinitionScanner(BeanDefinitionRegistry registry)
ClassPathBeanDefinitionScanner
for the given bean factory.registry
- the BeanFactory
to load bean definitions into, in the form
of a BeanDefinitionRegistry
public ClassPathBeanDefinitionScanner(BeanDefinitionRegistry registry, boolean useDefaultFilters)
ClassPathBeanDefinitionScanner
for the given bean factory.
If the passed-in bean factory does not only implement the
BeanDefinitionRegistry
interface but also the ResourceLoader
interface, it will be used as default ResourceLoader
as well. This will
usually be the case for ApplicationContext
implementations.
If given a plain BeanDefinitionRegistry
, the default ResourceLoader
will be a PathMatchingResourcePatternResolver
.
If the the passed-in bean factory also implements EnvironmentCapable
its
environment will be used by this reader. Otherwise, the reader will initialize and
use a StandardEnvironment
. All
ApplicationContext
implementations are EnvironmentCapable
, while
normal BeanFactory
implementations are not.
registry
- the BeanFactory
to load bean definitions into, in the form
of a BeanDefinitionRegistry
useDefaultFilters
- whether to include the default filters for the
@Component
,
@Repository
,
@Service
, and
@Controller
stereotype
annotations.ClassPathScanningCandidateComponentProvider.setResourceLoader(org.springframework.core.io.ResourceLoader)
,
ClassPathScanningCandidateComponentProvider.setEnvironment(org.springframework.core.env.Environment)
public ClassPathBeanDefinitionScanner(BeanDefinitionRegistry registry, boolean useDefaultFilters, Environment environment)
ClassPathBeanDefinitionScanner
for the given bean factory and
using the given Environment
when evaluating bean definition profile metadata.
If the passed-in bean factory does not only implement the BeanDefinitionRegistry
interface but also the ResourceLoader
interface, it
will be used as default ResourceLoader
as well. This will usually be the
case for ApplicationContext
implementations.
If given a plain BeanDefinitionRegistry
, the default ResourceLoader
will be a PathMatchingResourcePatternResolver
.
registry
- the BeanFactory
to load bean definitions into, in the form
of a BeanDefinitionRegistry
useDefaultFilters
- whether to include the default filters for theenvironment
- the Spring Environment
to use when evaluating bean
definition profile metadata.
@Component
,
@Repository
,
@Service
, and
@Controller
stereotype
annotations.ClassPathScanningCandidateComponentProvider.setResourceLoader(org.springframework.core.io.ResourceLoader)
public final BeanDefinitionRegistry getRegistry()
public void setBeanDefinitionDefaults(BeanDefinitionDefaults beanDefinitionDefaults)
BeanDefinitionDefaults
public void setAutowireCandidatePatterns(java.lang.String[] autowireCandidatePatterns)
autowireCandidatePatterns
- the patterns to match againstpublic void setBeanNameGenerator(BeanNameGenerator beanNameGenerator)
Default is a AnnotationBeanNameGenerator
.
public void setScopeMetadataResolver(ScopeMetadataResolver scopeMetadataResolver)
The default is an AnnotationScopeMetadataResolver
.
public void setScopedProxyMode(ScopedProxyMode scopedProxyMode)
The default is ScopedProxyMode.NO
.
public void setIncludeAnnotationConfig(boolean includeAnnotationConfig)
The default is to register the post-processors. Turn this off to be able to ignore the annotations or to process them differently.
public int scan(java.lang.String... basePackages)
basePackages
- the packages to check for annotated classesprotected java.util.Set<BeanDefinitionHolder> doScan(java.lang.String... basePackages)
This method does not register an annotation config processor but rather leaves this up to the caller.
basePackages
- the packages to check for annotated classesnull
)protected void postProcessBeanDefinition(AbstractBeanDefinition beanDefinition, java.lang.String beanName)
beanDefinition
- the scanned bean definitionbeanName
- the generated bean name for the given beanprotected void registerBeanDefinition(BeanDefinitionHolder definitionHolder, BeanDefinitionRegistry registry)
Can be overridden in subclasses, e.g. to adapt the registration process or to register further bean definitions for each scanned bean.
definitionHolder
- the bean definition plus bean name for the beanregistry
- the BeanDefinitionRegistry to register the bean withprotected boolean checkCandidate(java.lang.String beanName, BeanDefinition beanDefinition) throws java.lang.IllegalStateException
beanName
- the suggested name for the beanbeanDefinition
- the corresponding bean definitiontrue
if the bean can be registered as-is;
false
if it should be skipped because there is an
existing, compatible bean definition for the specified nameConflictingBeanDefinitionException
- if an existing, incompatible
bean definition has been found for the specified namejava.lang.IllegalStateException
protected boolean isCompatible(BeanDefinition newDefinition, BeanDefinition existingDefinition)
The default implementation considers them as compatible when the existing bean definition comes from the same source or from a non-scanning source.
newDefinition
- the new bean definition, originated from scanningexistingDefinition
- the existing bean definition, potentially an
explicitly defined one or a previously generated one from scanningprivate static Environment getOrCreateEnvironment(BeanDefinitionRegistry registry)