下面列出了org.springframework.beans.factory.xml.ParserContext#getRegistry ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Parses the specified XML element which contains the properties of the
* <code>{@link org.springmodules.cache.provider.CacheProviderFacade}</code>
* to register in the given registry of bean definitions.
*
* @param element
* the XML element to parse
* @param parserContext
* the parser context
* @throws IllegalStateException
* if the value of the property <code>serializableFactory</code>
* is not equal to "NONE" or "XSTREAM"
*
* @see BeanDefinitionParser#parse(Element, ParserContext)
*/
public final BeanDefinition parse(Element element, ParserContext parserContext)
throws IllegalStateException {
String id = element.getAttribute("id");
// create the cache provider facade
Class clazz = getCacheProviderFacadeClass();
MutablePropertyValues propertyValues = new MutablePropertyValues();
RootBeanDefinition cacheProviderFacade = new RootBeanDefinition(clazz,
propertyValues);
propertyValues.addPropertyValue(parseFailQuietlyEnabledProperty(element));
propertyValues.addPropertyValue(parseSerializableFactoryProperty(element));
BeanDefinitionRegistry registry = parserContext.getRegistry();
registry.registerBeanDefinition(id, cacheProviderFacade);
doParse(id, element, registry);
return null;
}
@Override
public final BeanDefinition parse( Element anElement, ParserContext aParserContext )
{
String serviceId = anElement.getAttribute( SERVICE_ID );
// Service factory bean
BeanDefinitionBuilder builder = rootBeanDefinition( ServiceFactoryBean.class );
builder.addConstructorArgReference( BEAN_ID_POLYGENE_APPLICATION );
builder.addConstructorArgValue( serviceId );
AbstractBeanDefinition definition = builder.getBeanDefinition();
// Register service factory bean
BeanDefinitionRegistry definitionRegistry = aParserContext.getRegistry();
definitionRegistry.registerBeanDefinition( serviceId, definition );
return definition;
}
@Override
public BeanDefinition parse(Element element, ParserContext parserContext) {
if (ready.compareAndSet(false, true)) {
BeanDefinitionRegistry registry = parserContext.getRegistry();
if (!registry.containsBeanDefinition(DependsOnDefinitionPostProcessor.BEAN_NAME)) {
registry.registerBeanDefinition(DependsOnDefinitionPostProcessor.BEAN_NAME,
new RootBeanDefinition(DependsOnDefinitionPostProcessor.class));
}
}
return super.parse(element, parserContext);
}
@Override
protected AbstractBeanDefinition parseInternal(Element element,
ParserContext parserContext) {
BeanDefinitionRegistry registry = parserContext.getRegistry();
// Register Dependent Beans
registerNacosPropertySourcePostProcessor(registry);
registerXmlNacosPropertySourceBuilder(registry);
NacosPropertySourceXmlBeanDefinition beanDefinition = new NacosPropertySourceXmlBeanDefinition();
beanDefinition.setElement(element);
beanDefinition.setXmlReaderContext(parserContext.getReaderContext());
return beanDefinition;
}
@Override
public BeanDefinition parse(Element element, ParserContext parserContext) {
// Get Environment
Environment environment = parserContext.getDelegate().getReaderContext()
.getReader().getEnvironment();
// Get BeanDefinitionRegistry
BeanDefinitionRegistry registry = parserContext.getRegistry();
// Register Nacos Annotation Beans
NacosBeanDefinitionRegistrar registrar = new NacosBeanDefinitionRegistrar();
registrar.setEnvironment(environment);
registrar.registerNacosAnnotationBeans(registry);
return null;
}
@Override
public BeanDefinition parse(Element element, ParserContext parserContext) {
Properties properties = new Properties();
Environment environment = parserContext.getDelegate().getReaderContext()
.getReader().getEnvironment();
properties.setProperty(PropertyKeyConst.ENDPOINT, element.getAttribute(ENDPOINT));
properties.setProperty(PropertyKeyConst.NAMESPACE,
element.getAttribute(NAMESPACE));
properties.setProperty(PropertyKeyConst.ACCESS_KEY,
element.getAttribute(ACCESS_KEY));
properties.setProperty(PropertyKeyConst.SECRET_KEY,
element.getAttribute(SECRET_KEY));
properties.setProperty(PropertyKeyConst.SERVER_ADDR,
element.getAttribute(SERVER_ADDR));
properties.setProperty(PropertyKeyConst.CLUSTER_NAME,
element.getAttribute(CLUSTER_NAME));
properties.setProperty(PropertyKeyConst.ENCODE, element.getAttribute(ENCODE));
properties.setProperty(PropertyKeyConst.USERNAME, element.getAttribute(USERNAME));
properties.setProperty(PropertyKeyConst.PASSWORD, element.getAttribute(PASSWORD));
BeanDefinitionRegistry registry = parserContext.getRegistry();
// Register Global Nacos Properties as Spring singleton bean
registerGlobalNacosProperties(properties, registry, environment,
GLOBAL_NACOS_PROPERTIES_BEAN_NAME);
return null;
}
@Override
protected AbstractBeanDefinition parseInternal(Element element, ParserContext parserContext) {
BeanDefinitionRegistry registry = parserContext.getRegistry();
BeanDefinitionBuilder builder = getBeanDefinitionBuilder(element, parserContext);
if (builder == null) {
return null;
}
AbstractBeanDefinition beanDefinition = builder.getBeanDefinition();
String beanName = resolveId(element, beanDefinition, parserContext);
registry.registerBeanDefinition(beanName, beanDefinition);
return beanDefinition;
}
/**
* @see CachingListenerValidator#validate(Object, int, ParserContext)
*/
public void validate(Object cachingListener, int index,
ParserContext parserContext) throws IllegalStateException {
BeanDefinitionRegistry registry = parserContext.getRegistry();
BeanDefinition beanDefinition = null;
if (cachingListener instanceof RuntimeBeanReference) {
String beanName = ((RuntimeBeanReference) cachingListener).getBeanName();
beanDefinition = registry.getBeanDefinition(beanName);
} else if (cachingListener instanceof BeanDefinitionHolder) {
beanDefinition = ((BeanDefinitionHolder) cachingListener)
.getBeanDefinition();
} else {
throw new IllegalStateException("The caching listener reference/holder ["
+ index + "] should be an instance of <"
+ RuntimeBeanReference.class.getName() + "> or <"
+ BeanDefinitionHolder.class.getName() + ">");
}
Class expectedClass = CachingListener.class;
Class actualClass = resolveBeanClass(beanDefinition);
if (!expectedClass.isAssignableFrom(actualClass)) {
throw new IllegalStateException("The caching listener [" + index
+ "] should be an instance of <" + expectedClass.getName() + ">");
}
}
/**
* Parses the given XML element containing the properties and/or sub-elements
* necessary to configure a strategy for setting up declarative caching
* services.
*
* @param element
* the XML element to parse
* @param parserContext
* the parser context
* @throws IllegalStateException
* if the bean definition registry does not have a definition for
* the <code>CacheProviderFacade</code> registered under the name
* specified in the XML attribute "providerId"
* @throws IllegalStateException
* if the cache provider facade is in invalid state
* @throws IllegalStateException
* if any of the caching listeners is not an instance of
* <code>CachingListener</code>
*
* @see BeanDefinitionParser#parse(Element, ParserContext)
*/
public final BeanDefinition parse(Element element, ParserContext parserContext)
throws NoSuchBeanDefinitionException, IllegalStateException {
String cacheProviderFacadeId = element.getAttribute("providerId");
BeanDefinitionRegistry registry = parserContext.getRegistry();
if (!registry.containsBeanDefinition(cacheProviderFacadeId)) {
throw new IllegalStateException(
"An implementation of CacheProviderFacade should be registered under the name "
+ StringUtils.quote(cacheProviderFacadeId));
}
RuntimeBeanReference cacheProviderFacadeReference = new RuntimeBeanReference(
cacheProviderFacadeId);
Object cacheKeyGenerator = parseCacheKeyGenerator(element, parserContext);
List cachingListeners = parseCachingListeners(element, parserContext);
Map cachingModels = parseCachingModels(element);
Map flushingModels = parseFlushingModels(element);
CacheSetupStrategyPropertySource ps = new CacheSetupStrategyPropertySource(
cacheKeyGenerator, cacheProviderFacadeReference, cachingListeners,
cachingModels, flushingModels);
parseCacheSetupStrategy(element, parserContext, ps);
return null;
}
/**
* @param registry
* @param bcd
*/
private void registerBeanComponentDefinition(Element element, ParserContext parserContext, BeanComponentDefinition bcd) {
parserContext.getDelegate().parseBeanDefinitionAttributes(element, bcd.getBeanName(), null,
(AbstractBeanDefinition) bcd.getBeanDefinition());
BeanDefinitionRegistry registry = parserContext.getRegistry();
registry.registerBeanDefinition(bcd.getBeanName(), bcd.getBeanDefinition());
}
@Override
protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
if (StringUtil.isEmpty(element.getAttribute(NAME_ATTRIBUTE))) {
parserContext.getReaderContext().error("CommonBaseService must have name attribute", element);
}
AbstractBeanDefinition beanDefinition = builder.getBeanDefinition();
BeanDefinitionRegistry beanDefinitionRegistry = parserContext.getRegistry();
BeanDefinitionParserDelegate delegate = parserContext.getDelegate();
String name = element.getAttribute(NAME_ATTRIBUTE);
beanDefinition.setAttribute(ID_ATTRIBUTE, name + COMMON_BASE_SERVICE_SUFFIX);
beanDefinition.setBeanClassName(CLASS_NAME);
beanDefinitionRegistry.registerBeanDefinition(name + DATA_SOURCE_SUFFIX, buildDataSourceBeanDefinition(element, name));
beanDefinitionRegistry.registerBeanDefinition(name + SQL_SESSION_FACTORY_SUFFIX, buildSqlSessionFactoryBeanDefinition(element, name));
beanDefinitionRegistry.registerBeanDefinition(name + SESSION_FACTORY_SUFFIX, buildSessionFactoryBeanDefinition(element, name, parserContext.getDelegate(), beanDefinitionRegistry));
beanDefinitionRegistry.registerBeanDefinition(name + SQL_SESSION_TEMPLATE_SUFFIX, buildSqlSessionTemplateBeanDefinition(element, name));
beanDefinitionRegistry.registerBeanDefinition(name + COMMON_BASE_DAO_SUFFIX, buildCommonBaseDaoBeanDefinition(element, name));
builder.addPropertyReference(COMMON_BASE_DAO_FIELD_NAME, name + COMMON_BASE_DAO_SUFFIX);
element.setAttribute(ID_ATTRIBUTE, name + COMMON_BASE_SERVICE_SUFFIX);
List<String> expressionList = buildExpressionList(element, delegate);
if (expressionList.size() > 0) {
beanDefinitionRegistry.registerBeanDefinition(name + HIBERNATE_TRANSACTION_MANAGER_SUFFIX, buildHibernateTransactionManagerBeanDefinition(element, name));
beanDefinitionRegistry.registerBeanDefinition(name + TRANSACTION_ATTRIBUTE_SOURCE_SUFFIX, buildTransactionAttributeSourceBeanDefinition());
beanDefinitionRegistry.registerBeanDefinition(name + HIBERNATE_ADVICE_SUFFIX, buildHibernateAdviceBeanDefinition(element, name));
buildPointcutAndAdvisorBeanDefinition(name, expressionList, parserContext, beanDefinitionRegistry);
}
}
/**
* Register a BeanComponentDefinition
* @param element
* @param parserContext
* @param BeanComponentDefinition
*/
private void registerBeanComponentDefinition(Element element, ParserContext parserContext, BeanComponentDefinition bcd) {
parserContext.getDelegate().parseBeanDefinitionAttributes(element, bcd.getBeanName(), null,
(AbstractBeanDefinition) bcd.getBeanDefinition());
BeanDefinitionRegistry registry = parserContext.getRegistry();
registry.registerBeanDefinition(bcd.getBeanName(), bcd.getBeanDefinition());
}
@Override
@Nullable
public BeanDefinition parse(Element element, ParserContext parserContext) {
Object source = parserContext.extractSource(element);
// Register component for the surrounding <jms:annotation-driven> element.
CompositeComponentDefinition compDefinition = new CompositeComponentDefinition(element.getTagName(), source);
parserContext.pushContainingComponent(compDefinition);
// Nest the concrete post-processor bean in the surrounding component.
BeanDefinitionRegistry registry = parserContext.getRegistry();
if (registry.containsBeanDefinition(JmsListenerConfigUtils.JMS_LISTENER_ANNOTATION_PROCESSOR_BEAN_NAME)) {
parserContext.getReaderContext().error(
"Only one JmsListenerAnnotationBeanPostProcessor may exist within the context.", source);
}
else {
BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(
"org.springframework.jms.annotation.JmsListenerAnnotationBeanPostProcessor");
builder.getRawBeanDefinition().setSource(source);
String endpointRegistry = element.getAttribute("registry");
if (StringUtils.hasText(endpointRegistry)) {
builder.addPropertyReference("endpointRegistry", endpointRegistry);
}
else {
registerDefaultEndpointRegistry(source, parserContext);
}
String containerFactory = element.getAttribute("container-factory");
if (StringUtils.hasText(containerFactory)) {
builder.addPropertyValue("containerFactoryBeanName", containerFactory);
}
String handlerMethodFactory = element.getAttribute("handler-method-factory");
if (StringUtils.hasText(handlerMethodFactory)) {
builder.addPropertyReference("messageHandlerMethodFactory", handlerMethodFactory);
}
registerInfrastructureBean(parserContext, builder,
JmsListenerConfigUtils.JMS_LISTENER_ANNOTATION_PROCESSOR_BEAN_NAME);
}
// Finally register the composite component.
parserContext.popAndRegisterContainingComponent();
return null;
}
@Override
@Nullable
public BeanDefinition parse(Element element, ParserContext parserContext) {
Object source = parserContext.extractSource(element);
// Register component for the surrounding <jms:annotation-driven> element.
CompositeComponentDefinition compDefinition = new CompositeComponentDefinition(element.getTagName(), source);
parserContext.pushContainingComponent(compDefinition);
// Nest the concrete post-processor bean in the surrounding component.
BeanDefinitionRegistry registry = parserContext.getRegistry();
if (registry.containsBeanDefinition(JmsListenerConfigUtils.JMS_LISTENER_ANNOTATION_PROCESSOR_BEAN_NAME)) {
parserContext.getReaderContext().error(
"Only one JmsListenerAnnotationBeanPostProcessor may exist within the context.", source);
}
else {
BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(
"org.springframework.jms.annotation.JmsListenerAnnotationBeanPostProcessor");
builder.getRawBeanDefinition().setSource(source);
String endpointRegistry = element.getAttribute("registry");
if (StringUtils.hasText(endpointRegistry)) {
builder.addPropertyReference("endpointRegistry", endpointRegistry);
}
else {
registerDefaultEndpointRegistry(source, parserContext);
}
String containerFactory = element.getAttribute("container-factory");
if (StringUtils.hasText(containerFactory)) {
builder.addPropertyValue("containerFactoryBeanName", containerFactory);
}
String handlerMethodFactory = element.getAttribute("handler-method-factory");
if (StringUtils.hasText(handlerMethodFactory)) {
builder.addPropertyReference("messageHandlerMethodFactory", handlerMethodFactory);
}
registerInfrastructureBean(parserContext, builder,
JmsListenerConfigUtils.JMS_LISTENER_ANNOTATION_PROCESSOR_BEAN_NAME);
}
// Finally register the composite component.
parserContext.popAndRegisterContainingComponent();
return null;
}
private void assembleProcessor(ParserContext parserContext) {
BeanDefinitionRegistry registry = parserContext.getRegistry();
String name = StringUtility.uncapitalize(ResourceAccessorProcessor.class.getSimpleName());
BeanDefinitionBuilder factory = BeanDefinitionBuilder.genericBeanDefinition(ResourceAccessorProcessor.class);
registry.registerBeanDefinition(name, factory.getBeanDefinition());
}
private void assembleProcessor(ParserContext context) {
BeanDefinitionRegistry registry = context.getRegistry();
String name = StringUtility.uncapitalize(CacheAccessorProcessor.class.getSimpleName());
BeanDefinitionBuilder factory = BeanDefinitionBuilder.genericBeanDefinition(CacheAccessorProcessor.class);
registry.registerBeanDefinition(name, factory.getBeanDefinition());
}
@Override
public BeanDefinition parse(Element element, ParserContext parserContext) {
Object source = parserContext.extractSource(element);
// Register component for the surrounding <jms:annotation-driven> element.
CompositeComponentDefinition compDefinition = new CompositeComponentDefinition(element.getTagName(), source);
parserContext.pushContainingComponent(compDefinition);
// Nest the concrete post-processor bean in the surrounding component.
BeanDefinitionRegistry registry = parserContext.getRegistry();
if (registry.containsBeanDefinition(JmsListenerConfigUtils.JMS_LISTENER_ANNOTATION_PROCESSOR_BEAN_NAME)) {
parserContext.getReaderContext().error(
"Only one JmsListenerAnnotationBeanPostProcessor may exist within the context.", source);
}
else {
BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(
"org.springframework.jms.annotation.JmsListenerAnnotationBeanPostProcessor");
builder.getRawBeanDefinition().setSource(source);
String endpointRegistry = element.getAttribute("registry");
if (StringUtils.hasText(endpointRegistry)) {
builder.addPropertyReference("endpointRegistry", endpointRegistry);
}
else {
registerDefaultEndpointRegistry(source, parserContext);
}
String containerFactory = element.getAttribute("container-factory");
if (StringUtils.hasText(containerFactory)) {
builder.addPropertyValue("containerFactoryBeanName", containerFactory);
}
String handlerMethodFactory = element.getAttribute("handler-method-factory");
if (StringUtils.hasText(handlerMethodFactory)) {
builder.addPropertyReference("messageHandlerMethodFactory", handlerMethodFactory);
}
registerInfrastructureBean(parserContext, builder,
JmsListenerConfigUtils.JMS_LISTENER_ANNOTATION_PROCESSOR_BEAN_NAME);
}
// Finally register the composite component.
parserContext.popAndRegisterContainingComponent();
return null;
}
/**
* Creates and registers a <code>{@link CacheProxyFactoryBean}</code> by
* parsing the given XML element.
*
* @param element
* the XML element to parse
* @param parserContext
* the registry of bean definitions
* @param propertySource
* contains common properties for the different cache setup
* strategies
* @throws IllegalStateException
* if the "proxy" tag does not contain any reference to an existing
* bean or if it does not contain a bean definition
*
* @see AbstractCacheSetupStrategyParser#parseCacheSetupStrategy(Element,
* ParserContext, CacheSetupStrategyPropertySource)
*/
protected void parseCacheSetupStrategy(Element element,
ParserContext parserContext,
CacheSetupStrategyPropertySource propertySource) {
Object target = getBeanReferenceParser().parse(element, parserContext);
RootBeanDefinition cacheProxyFactoryBean = new RootBeanDefinition(
CacheProxyFactoryBean.class, propertySource.getAllProperties());
cacheProxyFactoryBean.getPropertyValues()
.addPropertyValue("target", target);
String id = element.getAttribute("id");
BeanDefinitionRegistry registry = parserContext.getRegistry();
registry.registerBeanDefinition(id, cacheProxyFactoryBean);
}
private void registerBean( ParserContext aParserContext, BeanDefinition aBeanDefinition )
{
BeanDefinitionRegistry registry = aParserContext.getRegistry();
registry.registerBeanDefinition( BEAN_ID_POLYGENE_APPLICATION, aBeanDefinition );
}
/**
* Creates and registers instances
* <code>{@link MethodMapCachingInterceptor}</code> and
* <code>{@link MethodMapFlushingInterceptor}</code> by parsing the given
* XML element.
*
* @param element
* the XML element to parse
* @param parserContext
* the registry of bean definitions
* @param propertySource
* contains common properties for the different cache setup
* strategies
*
* @see AbstractCacheSetupStrategyParser#parseCacheSetupStrategy(Element,
* ParserContext, CacheSetupStrategyPropertySource)
*/
protected void parseCacheSetupStrategy(Element element,
ParserContext parserContext,
CacheSetupStrategyPropertySource propertySource) {
BeanDefinitionRegistry registry = parserContext.getRegistry();
String cachingInterceptorId = element.getAttribute("cachingInterceptorId");
registerCachingInterceptor(cachingInterceptorId, registry, propertySource);
String flushingInterceptorId = element
.getAttribute("flushingInterceptorId");
registerFlushingInterceptor(flushingInterceptorId, registry, propertySource);
}