📄 propertiesbeandefinitionreader.java
字号:
// Simply create a map and call overloaded method
Map map = new HashMap();
Enumeration keys = rb.getKeys();
while (keys.hasMoreElements()) {
String key = (String) keys.nextElement();
map.put(key, rb.getObject(key));
}
return registerBeanDefinitions(map, prefix);
}
/**
* Register bean definitions contained in a Map,
* using all property keys (i.e. not filtering by prefix).
* @param map Map name -> property (String or Object). Property values
* will be strings if coming from a Properties file etc. Property names
* (keys) <b>must</b> be Strings. Class keys must be Strings.
* @return the number of bean definitions found
* @throws BeansException in case of loading or parsing errors
* @see #registerBeanDefinitions(java.util.Map, String, String)
*/
public int registerBeanDefinitions(Map map) throws BeansException {
return registerBeanDefinitions(map, null);
}
/**
* Register bean definitions contained in a Map.
* Ignore ineligible properties.
* @param map Map name -> property (String or Object). Property values
* will be strings if coming from a Properties file etc. Property names
* (keys) <b>must</b> be Strings. Class keys must be Strings.
* @param prefix The match or filter within the keys in the map: e.g. 'beans.'
* @return the number of bean definitions found
* @throws BeansException in case of loading or parsing errors
*/
public int registerBeanDefinitions(Map map, String prefix) throws BeansException {
return registerBeanDefinitions(map, prefix, "Map " + map);
}
/**
* Register bean definitions contained in a Map.
* Ignore ineligible properties.
* @param map Map name -> property (String or Object). Property values
* will be strings if coming from a Properties file etc. Property names
* (keys) <b>must</b> be strings. Class keys must be Strings.
* @param prefix match or filter within the keys in the map: e.g. 'beans.'
* @param resourceDescription description of the resource that the Map came from
* (for logging purposes)
* @return the number of bean definitions found
* @throws BeansException in case of loading or parsing errors
* @see #registerBeanDefinitions(Map, String)
*/
public int registerBeanDefinitions(Map map, String prefix, String resourceDescription)
throws BeansException {
if (prefix == null) {
prefix = "";
}
int beanCount = 0;
for (Iterator it = map.keySet().iterator(); it.hasNext();) {
Object key = it.next();
if (!(key instanceof String)) {
throw new IllegalArgumentException("Illegal key [" + key + "]: only Strings allowed");
}
String keyString = (String) key;
if (keyString.startsWith(prefix)) {
// Key is of form: prefix<name>.property
String nameAndProperty = keyString.substring(prefix.length());
// Find dot before property name, ignoring dots in property keys.
int sepIdx = -1;
int propKeyIdx = nameAndProperty.indexOf(PropertyAccessor.PROPERTY_KEY_PREFIX);
if (propKeyIdx != -1) {
sepIdx = nameAndProperty.lastIndexOf(SEPARATOR, propKeyIdx);
}
else {
sepIdx = nameAndProperty.lastIndexOf(SEPARATOR);
}
if (sepIdx != -1) {
String beanName = nameAndProperty.substring(0, sepIdx);
if (logger.isDebugEnabled()) {
logger.debug("Found bean name '" + beanName + "'");
}
if (!getBeanFactory().containsBeanDefinition(beanName)) {
// If we haven't already registered it...
registerBeanDefinition(beanName, map, prefix + beanName, resourceDescription);
++beanCount;
}
}
else {
// Ignore it: It wasn't a valid bean name and property,
// although it did start with the required prefix.
if (logger.isDebugEnabled()) {
logger.debug("Invalid bean name and property [" + nameAndProperty + "]");
}
}
}
}
return beanCount;
}
/**
* Get all property values, given a prefix (which will be stripped)
* and add the bean they define to the factory with the given name
* @param beanName name of the bean to define
* @param map Map containing string pairs
* @param prefix prefix of each entry, which will be stripped
* @param resourceDescription description of the resource that the Map came from
* (for logging purposes)
* @throws BeansException if the bean definition could not be parsed or registered
*/
protected void registerBeanDefinition(String beanName, Map map, String prefix, String resourceDescription)
throws BeansException {
String className = null;
String parent = null;
boolean isAbstract = false;
boolean singleton = true;
boolean lazyInit = false;
MutablePropertyValues pvs = new MutablePropertyValues();
for (Iterator it = map.entrySet().iterator(); it.hasNext();) {
Map.Entry entry = (Map.Entry) it.next();
String key = (String) entry.getKey();
if (key.startsWith(prefix + SEPARATOR)) {
String property = key.substring(prefix.length() + SEPARATOR.length());
if (property.equals(CLASS_KEY)) {
className = (String) entry.getValue();
}
else if (property.equals(ABSTRACT_KEY)) {
String val = (String) entry.getValue();
isAbstract = val.equals(TRUE_VALUE);
}
else if (property.equals(SINGLETON_KEY)) {
String val = (String) entry.getValue();
singleton = (val == null) || val.equals(TRUE_VALUE);
}
else if (property.equals(LAZY_INIT_KEY)) {
String val = (String) entry.getValue();
lazyInit = val.equals(TRUE_VALUE);
}
else if (property.equals(PARENT_KEY)) {
parent = (String) entry.getValue();
}
else if (property.endsWith(REF_SUFFIX)) {
// This isn't a real property, but a reference to another prototype
// Extract property name: property is of form dog(ref)
property = property.substring(0, property.length() - REF_SUFFIX.length());
String ref = (String) entry.getValue();
// It doesn't matter if the referenced bean hasn't yet been registered:
// this will ensure that the reference is resolved at rungime
// Default is not to use singleton
Object val = new RuntimeBeanReference(ref);
pvs.addPropertyValue(new PropertyValue(property, val));
}
else{
// normal bean property
Object val = entry.getValue();
if (val instanceof String) {
String strVal = (String) val;
// if it starts with a reference prefix...
if (strVal.startsWith(REF_PREFIX)) {
// expand reference
String targetName = strVal.substring(1);
if (targetName.startsWith(REF_PREFIX)) {
// escaped prefix -> use plain value
val = targetName;
}
else {
val = new RuntimeBeanReference(targetName);
}
}
}
pvs.addPropertyValue(new PropertyValue(property, val));
}
}
}
if (logger.isDebugEnabled()) {
logger.debug("Registering bean definition for bean name '" + beanName + "' with " + pvs);
}
// Just use default parent if we're not dealing with the parent itself,
// and if there's no class name specified. The latter has to happen for
// backwards compatibility reasons.
if (parent == null && className == null && !beanName.equals(this.defaultParentBean)) {
parent = this.defaultParentBean;
}
try {
AbstractBeanDefinition bd = BeanDefinitionReaderUtils.createBeanDefinition(
className, parent, null, pvs, getBeanClassLoader());
bd.setAbstract(isAbstract);
bd.setSingleton(singleton);
bd.setLazyInit(lazyInit);
getBeanFactory().registerBeanDefinition(beanName, bd);
}
catch (ClassNotFoundException ex) {
throw new BeanDefinitionStoreException(resourceDescription, beanName,
"Bean class [" + className + "] not found", ex);
}
catch (NoClassDefFoundError err) {
throw new BeanDefinitionStoreException(resourceDescription, beanName,
"Class that bean class [" + className + "] depends on not found", err);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -