下面列出了org.springframework.beans.MutablePropertyValues#getPropertyValue ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
String[] beanNameArray = beanFactory.getBeanDefinitionNames();
for (int i = 0; i < beanNameArray.length; i++) {
BeanDefinition beanDef = beanFactory.getBeanDefinition(beanNameArray[i]);
String beanClassName = beanDef.getBeanClassName();
if (FEIGN_FACTORY_CLASS.equals(beanClassName) == false) {
continue;
}
MutablePropertyValues mpv = beanDef.getPropertyValues();
PropertyValue pv = mpv.getPropertyValue("name");
String client = String.valueOf(pv.getValue() == null ? "" : pv.getValue());
if (StringUtils.isNotBlank(client)) {
this.transientClientSet.add(client);
}
}
this.fireAfterPropertiesSet();
}
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
String[] beanNameArray = beanFactory.getBeanDefinitionNames();
for (int i = 0; i < beanNameArray.length; i++) {
BeanDefinition beanDef = beanFactory.getBeanDefinition(beanNameArray[i]);
String beanClassName = beanDef.getBeanClassName();
if (FEIGN_FACTORY_CLASS.equals(beanClassName) == false) {
continue;
}
MutablePropertyValues mpv = beanDef.getPropertyValues();
PropertyValue pv = mpv.getPropertyValue("name");
String client = String.valueOf(pv.getValue() == null ? "" : pv.getValue());
if (StringUtils.isNotBlank(client)) {
this.transientClientSet.add(client);
}
}
this.fireAfterPropertiesSet();
}
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
String[] beanNameArray = beanFactory.getBeanDefinitionNames();
for (int i = 0; i < beanNameArray.length; i++) {
BeanDefinition beanDef = beanFactory.getBeanDefinition(beanNameArray[i]);
String beanClassName = beanDef.getBeanClassName();
if (FEIGN_FACTORY_CLASS.equals(beanClassName) == false) {
continue;
}
MutablePropertyValues mpv = beanDef.getPropertyValues();
PropertyValue pv = mpv.getPropertyValue("name");
String client = String.valueOf(pv.getValue() == null ? "" : pv.getValue());
if (StringUtils.isNotBlank(client)) {
this.transientClientSet.add(client);
}
}
this.fireAfterPropertiesSet();
}
/**
* Retrieves the expression graph map set on the bean with given name. If the bean has not been processed
* by the bean factory post processor, that is done before retrieving the map
*
* @param parentBeanName name of the parent bean to retrieve map for (if empty a new map will be returned)
* @param beanFactory bean factory to retrieve bean definition from
* @param processedBeanNames set of bean names that have been processed so far
* @return expression graph map from parent or new instance
*/
protected Map<String, String> getExpressionGraphFromParent(String parentBeanName,
ConfigurableListableBeanFactory beanFactory, Set<String> processedBeanNames) {
Map<String, String> expressionGraph = new HashMap<String, String>();
if (StringUtils.isBlank(parentBeanName) || !beanFactory.containsBeanDefinition(parentBeanName)) {
return expressionGraph;
}
BeanDefinition beanDefinition = beanFactory.getBeanDefinition(parentBeanName);
if (!processedBeanNames.contains(parentBeanName)) {
processBeanDefinition(parentBeanName, beanDefinition, beanFactory, processedBeanNames);
}
MutablePropertyValues pvs = beanDefinition.getPropertyValues();
PropertyValue propertyExpressionsPV = pvs.getPropertyValue(UifPropertyPaths.EXPRESSION_GRAPH);
if (propertyExpressionsPV != null) {
Object value = propertyExpressionsPV.getValue();
if ((value != null) && (value instanceof ManagedMap)) {
expressionGraph.putAll((ManagedMap) value);
}
}
return expressionGraph;
}
/**
* Attempts to find a property value for the given property name within the bean definition, if the property
* does not exist in the bean and there is a parent, the parent is checked for containing the property. This
* continues until a property value is found or all the parents have been traversed
*
* @param beanDefinition bean definition to find property value in
* @param propertyName name of the property to find the value for
* @return String value for property in the bean definition or null if the property was not found
*/
protected String findPropertyValueInBeanDefinition(BeanDefinition beanDefinition, String propertyName) {
String beanPropertyValue = null;
MutablePropertyValues pvs = beanDefinition.getPropertyValues();
if (pvs.contains(propertyName)) {
PropertyValue propertyValue = pvs.getPropertyValue(propertyName);
if (propertyValue.getValue() != null) {
beanPropertyValue = propertyValue.getValue().toString();
}
} else {
if (StringUtils.isNotBlank(beanDefinition.getParentName())) {
BeanDefinition parentBeanDefinition = beanFactory.getBeanDefinition(beanDefinition.getParentName());
beanPropertyValue = findPropertyValueInBeanDefinition(parentBeanDefinition, propertyName);
}
}
return beanPropertyValue;
}
/**
* Applies the given message text to the bean definition with the given property name, if a current
* value exists for the property name the value is checked for expressions which are then merged with
* the message
*
* @param propertyName - name of the property to set on the bean definition
* @param messageText - message text that will be the property value
* @param beanDefinition - bean definition to set property on
*/
protected void applyMessageTextToPropertyValue(String propertyName, String messageText,
BeanDefinition beanDefinition) {
String newPropertyValue = messageText;
MutablePropertyValues pvs = beanDefinition.getPropertyValues();
if (pvs.contains(propertyName)) {
PropertyValue propertyValue = pvs.getPropertyValue(propertyName);
String stringPropertyValue = getStringValue(propertyValue.getValue());
if (StringUtils.isNotBlank(stringPropertyValue)) {
newPropertyValue = getMergedMessageText(messageText, stringPropertyValue);
}
}
applyPropertyValueToBean(propertyName, newPropertyValue, pvs);
}
/**
* Retrieves the component code associated with the bean definition
*
* @param beanName name of the bean to find component code for
* @param beanDefinition bean definition to find component code for
* @return String component code for bean or null if a component code was not found
*/
protected String getComponentForBean(String beanName, BeanDefinition beanDefinition) {
String componentCode = null;
MutablePropertyValues pvs = beanDefinition.getPropertyValues();
if (pvs.contains(KRADPropertyConstants.COMPONENT_CODE)) {
PropertyValue propertyValue = pvs.getPropertyValue(KRADPropertyConstants.COMPONENT_CODE);
componentCode = getStringValue(propertyValue.getValue());
}
if ((componentCode == null) && StringUtils.isNotBlank(beanName) && !isGeneratedBeanName(beanName)) {
componentCode = beanName;
}
if (StringUtils.isNotBlank(componentCode)) {
componentCode = StringUtils.removeEnd(componentCode, KRADConstants.DICTIONARY_BEAN_PARENT_SUFFIX);
}
return componentCode;
}
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
String[] beanNameArray = beanFactory.getBeanDefinitionNames();
for (int i = 0; i < beanNameArray.length; i++) {
String beanName = beanNameArray[i];
BeanDefinition beanDef = beanFactory.getBeanDefinition(beanName);
String beanClassName = beanDef.getBeanClassName();
if (org.springframework.transaction.interceptor.TransactionProxyFactoryBean.class.getName().equals(beanClassName)) {
throw new FatalBeanException(String.format(
"Declaring transactions by configuration is not supported yet, please use annotations to declare transactions(beanId= %s).",
beanName));
}
if (org.springframework.transaction.interceptor.TransactionInterceptor.class.getName().equals(beanClassName)) {
boolean errorExists = true;
MutablePropertyValues mpv = beanDef.getPropertyValues();
PropertyValue pv = mpv.getPropertyValue("transactionAttributeSource");
Object value = pv == null ? null : pv.getValue();
if (value != null && RuntimeBeanReference.class.isInstance(value)) {
RuntimeBeanReference reference = (RuntimeBeanReference) value;
BeanDefinition refBeanDef = beanFactory.getBeanDefinition(reference.getBeanName());
String refBeanClassName = refBeanDef.getBeanClassName();
errorExists = AnnotationTransactionAttributeSource.class.getName().equals(refBeanClassName) == false;
}
if (errorExists) {
throw new FatalBeanException(String.format(
"Declaring transactions by configuration is not supported yet, please use annotations to declare transactions(beanId= %s).",
beanName));
} // end-if (errorExists)
}
} // end-for (int i = 0; i < beanNameArray.length; i++)
}
/**
* Applies the given message text to the property within the given bean definition
*
* <p>
* The message text is applied to the bean definiton based on the property path. The path could be nested
* in which case the property values of the bean definition are traversed to find the nested bean definition
* that should hold the property value. The path could also represent a nested list bean. In this case a
* helper method is called to find the correct list bean to apply the message to
* </p>
*
* @param message message containing the text to apply
* @param beanDefinition bean definition containing the property
* @param propertyPath path to property within the bean definition the message should be applied to
*/
protected void applyMessageToNestedBean(Message message, BeanDefinition beanDefinition, String propertyPath) {
MutablePropertyValues pvs = beanDefinition.getPropertyValues();
String beanPath = StringUtils.substringBefore(propertyPath, ".");
String nestedPropertyPath = StringUtils.substringAfter(propertyPath, ".");
boolean foundNestedBean = false;
while (StringUtils.isNotBlank(nestedPropertyPath)) {
if (pvs.contains(beanPath)) {
PropertyValue propertyValue = pvs.getPropertyValue(beanPath);
BeanDefinition propertyBeanDefinition = getPropertyValueBeanDefinition(propertyValue);
if (propertyBeanDefinition != null) {
applyMessageToNestedBean(message, propertyBeanDefinition, nestedPropertyPath);
foundNestedBean = true;
break;
} else if (propertyValue.getValue() instanceof List) {
applyMessageToNestedListBean(message, (List<?>) propertyValue.getValue(), nestedPropertyPath);
foundNestedBean = true;
break;
}
}
beanPath += "." + StringUtils.substringBefore(nestedPropertyPath, ".");
nestedPropertyPath = StringUtils.substringAfter(nestedPropertyPath, ".");
}
if (!foundNestedBean) {
applyMessageTextToPropertyValue(propertyPath, message.getText(), beanDefinition);
}
}
/**
* Retrieves the namespace associated with the bean definition
*
* @param beanName name of the bean to find namespace for
* @param beanDefinition bean definition to find namespace for
* @return String namespace for bean or null if a namespace was not found
*/
protected String getNamespaceForBean(String beanName, BeanDefinition beanDefinition) {
String namespace = null;
MutablePropertyValues pvs = beanDefinition.getPropertyValues();
if (pvs.contains(KRADPropertyConstants.NAMESPACE_CODE)) {
PropertyValue propertyValue = pvs.getPropertyValue(KRADPropertyConstants.NAMESPACE_CODE);
namespace = getStringValue(propertyValue.getValue());
} else if (StringUtils.isNotBlank(beanName) && !isGeneratedBeanName(beanName)) {
// if not on bean definition, get from associated module in the dictionary
namespace = dataDictionary.getNamespaceForBeanDefinition(beanName);
}
return namespace;
}
private void parseListener(Element containerEle, Element listenerEle, ParserContext parserContext,
MutablePropertyValues commonContainerProperties, PropertyValues specificContainerProperties) {
RootBeanDefinition listenerDef = new RootBeanDefinition();
listenerDef.setSource(parserContext.extractSource(listenerEle));
listenerDef.setBeanClassName("org.springframework.jms.listener.adapter.MessageListenerAdapter");
String ref = listenerEle.getAttribute(REF_ATTRIBUTE);
if (!StringUtils.hasText(ref)) {
parserContext.getReaderContext().error(
"Listener 'ref' attribute contains empty value.", listenerEle);
}
else {
listenerDef.getPropertyValues().add("delegate", new RuntimeBeanReference(ref));
}
if (listenerEle.hasAttribute(METHOD_ATTRIBUTE)) {
String method = listenerEle.getAttribute(METHOD_ATTRIBUTE);
if (!StringUtils.hasText(method)) {
parserContext.getReaderContext().error(
"Listener 'method' attribute contains empty value.", listenerEle);
}
listenerDef.getPropertyValues().add("defaultListenerMethod", method);
}
PropertyValue messageConverterPv = commonContainerProperties.getPropertyValue("messageConverter");
if (messageConverterPv != null) {
listenerDef.getPropertyValues().addPropertyValue(messageConverterPv);
}
BeanDefinition containerDef = createContainer(
containerEle, listenerEle, parserContext, commonContainerProperties, specificContainerProperties);
containerDef.getPropertyValues().add("messageListener", listenerDef);
if (listenerEle.hasAttribute(RESPONSE_DESTINATION_ATTRIBUTE)) {
String responseDestination = listenerEle.getAttribute(RESPONSE_DESTINATION_ATTRIBUTE);
Boolean pubSubDomain = (Boolean) commonContainerProperties.get("replyPubSubDomain");
if (pubSubDomain == null) {
pubSubDomain = false;
}
listenerDef.getPropertyValues().add(
pubSubDomain ? "defaultResponseTopicName" : "defaultResponseQueueName", responseDestination);
PropertyValue destinationResolver = containerDef.getPropertyValues().getPropertyValue("destinationResolver");
if (destinationResolver != null) {
listenerDef.getPropertyValues().addPropertyValue(destinationResolver);
}
}
String containerBeanName = listenerEle.getAttribute(ID_ATTRIBUTE);
// If no bean id is given auto generate one using the ReaderContext's BeanNameGenerator
if (!StringUtils.hasText(containerBeanName)) {
containerBeanName = parserContext.getReaderContext().generateBeanName(containerDef);
}
// Register the listener and fire event
parserContext.registerBeanComponent(new BeanComponentDefinition(containerDef, containerBeanName));
}
private void parseListener(Element containerEle, Element listenerEle, ParserContext parserContext,
MutablePropertyValues commonContainerProperties, PropertyValues specificContainerProperties) {
RootBeanDefinition listenerDef = new RootBeanDefinition();
listenerDef.setSource(parserContext.extractSource(listenerEle));
listenerDef.setBeanClassName("org.springframework.jms.listener.adapter.MessageListenerAdapter");
String ref = listenerEle.getAttribute(REF_ATTRIBUTE);
if (!StringUtils.hasText(ref)) {
parserContext.getReaderContext().error(
"Listener 'ref' attribute contains empty value.", listenerEle);
}
else {
listenerDef.getPropertyValues().add("delegate", new RuntimeBeanReference(ref));
}
if (listenerEle.hasAttribute(METHOD_ATTRIBUTE)) {
String method = listenerEle.getAttribute(METHOD_ATTRIBUTE);
if (!StringUtils.hasText(method)) {
parserContext.getReaderContext().error(
"Listener 'method' attribute contains empty value.", listenerEle);
}
listenerDef.getPropertyValues().add("defaultListenerMethod", method);
}
PropertyValue messageConverterPv = commonContainerProperties.getPropertyValue("messageConverter");
if (messageConverterPv != null) {
listenerDef.getPropertyValues().addPropertyValue(messageConverterPv);
}
BeanDefinition containerDef = createContainer(
containerEle, listenerEle, parserContext, commonContainerProperties, specificContainerProperties);
containerDef.getPropertyValues().add("messageListener", listenerDef);
if (listenerEle.hasAttribute(RESPONSE_DESTINATION_ATTRIBUTE)) {
String responseDestination = listenerEle.getAttribute(RESPONSE_DESTINATION_ATTRIBUTE);
Boolean pubSubDomain = (Boolean) commonContainerProperties.get("replyPubSubDomain");
if (pubSubDomain == null) {
pubSubDomain = false;
}
listenerDef.getPropertyValues().add(
pubSubDomain ? "defaultResponseTopicName" : "defaultResponseQueueName", responseDestination);
PropertyValue destinationResolver = containerDef.getPropertyValues().getPropertyValue("destinationResolver");
if (destinationResolver != null) {
listenerDef.getPropertyValues().addPropertyValue(destinationResolver);
}
}
String containerBeanName = listenerEle.getAttribute(ID_ATTRIBUTE);
// If no bean id is given auto generate one using the ReaderContext's BeanNameGenerator
if (!StringUtils.hasText(containerBeanName)) {
containerBeanName = parserContext.getReaderContext().generateBeanName(containerDef);
}
// Register the listener and fire event
parserContext.registerBeanComponent(new BeanComponentDefinition(containerDef, containerBeanName));
}
/**
* Given a bean name (assumed to implement {@link org.springframework.core.io.support.PropertiesLoaderSupport})
* checks whether it already references the <code>global-properties</code> bean. If not, 'upgrades' the bean by
* appending all additional resources it mentions in its <code>locations</code> property to
* <code>globalPropertyLocations</code>, except for those resources mentioned in <code>newLocations</code>. A
* reference to <code>global-properties</code> will then be added and the resource list in
* <code>newLocations<code> will then become the new <code>locations</code> list for the bean.
*
* @param beanFactory
* the bean factory
* @param globalPropertyLocations
* the list of global property locations to be appended to
* @param beanName
* the bean name
* @param newLocations
* the new locations to be set on the bean
* @return the mutable property values
*/
@SuppressWarnings("unchecked")
private MutablePropertyValues processLocations(ConfigurableListableBeanFactory beanFactory,
Collection<Object> globalPropertyLocations, String beanName, String[] newLocations)
{
// Get the bean an check its existing properties value
MutablePropertyValues beanProperties = beanFactory.getBeanDefinition(beanName).getPropertyValues();
PropertyValue pv = beanProperties.getPropertyValue(LegacyConfigPostProcessor.PROPERTY_PROPERTIES);
Object value;
// If the properties value already references the global-properties bean, we have nothing else to do. Otherwise,
// we have to 'upgrade' the bean definition.
if (pv == null || (value = pv.getValue()) == null || !(value instanceof BeanReference)
|| ((BeanReference) value).getBeanName().equals(LegacyConfigPostProcessor.BEAN_NAME_GLOBAL_PROPERTIES))
{
// Convert the array of new locations to a managed list of type string values, so that it is
// compatible with a bean definition
Collection<Object> newLocationList = new ManagedList(newLocations.length);
if (newLocations != null && newLocations.length > 0)
{
for (String preserveLocation : newLocations)
{
newLocationList.add(new TypedStringValue(preserveLocation));
}
}
// If there is currently a locations list, process it
pv = beanProperties.getPropertyValue(LegacyConfigPostProcessor.PROPERTY_LOCATIONS);
if (pv != null && (value = pv.getValue()) != null && value instanceof Collection)
{
Collection<Object> locations = (Collection<Object>) value;
// Compute the set of locations that need to be added to globalPropertyLocations (preserving order) and
// warn about each
Set<Object> addedLocations = new LinkedHashSet<Object>(locations);
addedLocations.removeAll(globalPropertyLocations);
addedLocations.removeAll(newLocationList);
for (Object location : addedLocations)
{
LegacyConfigPostProcessor.logger.warn("Legacy configuration detected: adding "
+ (location instanceof TypedStringValue ? ((TypedStringValue) location).getValue()
: location.toString()) + " to global-properties definition");
globalPropertyLocations.add(location);
}
}
// Ensure the bean now references global-properties
beanProperties.addPropertyValue(LegacyConfigPostProcessor.PROPERTY_PROPERTIES, new RuntimeBeanReference(
LegacyConfigPostProcessor.BEAN_NAME_GLOBAL_PROPERTIES));
// Ensure the new location list is now set on the bean
if (newLocationList.size() > 0)
{
beanProperties.addPropertyValue(LegacyConfigPostProcessor.PROPERTY_LOCATIONS, newLocationList);
}
else
{
beanProperties.removePropertyValue(LegacyConfigPostProcessor.PROPERTY_LOCATIONS);
}
}
return beanProperties;
}
private void validateReferenceConfig(String beanName, BeanDefinition beanDef) throws BeansException {
MutablePropertyValues mpv = beanDef.getPropertyValues();
PropertyValue group = mpv.getPropertyValue("group");
PropertyValue retries = mpv.getPropertyValue("retries");
PropertyValue loadbalance = mpv.getPropertyValue("loadbalance");
PropertyValue cluster = mpv.getPropertyValue("cluster");
PropertyValue filter = mpv.getPropertyValue("filter");
String filterValue = filter == null || filter.getValue() == null ? null : String.valueOf(filter.getValue());
String[] filterArray = filter == null ? new String[0] : filterValue.split("\\s*,\\s*");
if (group == null || group.getValue() == null //
|| ("x-bytejta".equals(group.getValue())
|| String.valueOf(group.getValue()).startsWith("x-bytejta-")) == false) {
throw new FatalBeanException(String.format(
"The value of attr 'group'(beanId= %s) should be 'x-bytejta' or starts with 'x-bytejta-'.", beanName));
} else if (retries == null || retries.getValue() == null || "-1".equals(retries.getValue()) == false) {
throw new FatalBeanException(String.format("The value of attr 'retries'(beanId= %s) should be '-1'.", beanName));
} else if (loadbalance == null || loadbalance.getValue() == null || "bytejta".equals(loadbalance.getValue()) == false) {
throw new FatalBeanException(
String.format("The value of attr 'loadbalance'(beanId= %s) should be 'bytejta'.", beanName));
} else if (cluster == null || cluster.getValue() == null || "failfast".equals(cluster.getValue()) == false) {
throw new FatalBeanException(
String.format("The value of attribute 'cluster' (beanId= %s) must be 'failfast'.", beanName));
} else if (filter == null || filter.getValue() == null || String.class.isInstance(filter.getValue()) == false) {
throw new FatalBeanException(String
.format("The value of attr 'filter'(beanId= %s) must be java.lang.String and cannot be null.", beanName));
} else if (StringUtils.equalsIgnoreCase(filterArray[filterArray.length - 1], "bytejta") == false) {
throw new FatalBeanException(
String.format("The last value of attr 'filter'(beanId= %s) should be 'bytejta'.", beanName));
}
PropertyValue pv = mpv.getPropertyValue("interface");
String clazzName = String.valueOf(pv.getValue());
ClassLoader cl = Thread.currentThread().getContextClassLoader();
Class<?> clazz = null;
try {
clazz = cl.loadClass(clazzName);
} catch (Exception ex) {
throw new FatalBeanException(String.format("Cannot load class %s.", clazzName));
}
Method[] methodArray = clazz.getMethods();
for (int i = 0; i < methodArray.length; i++) {
Method method = methodArray[i];
boolean declared = false;
Class<?>[] exceptionTypeArray = method.getExceptionTypes();
for (int j = 0; j < exceptionTypeArray.length; j++) {
Class<?> exceptionType = exceptionTypeArray[j];
if (RemotingException.class.isAssignableFrom(exceptionType)) {
declared = true;
break;
}
}
if (declared == false) {
logger.warn("The remote call method({}) should be declared to throw a remote exception: {}!", method,
RemotingException.class.getName());
}
}
}