📄 propertiesbeandefinitionreader.java
字号:
/*
* Copyright 2002-2006 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.beans.factory.support;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.ResourceBundle;
import org.springframework.beans.BeansException;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.PropertyAccessor;
import org.springframework.beans.PropertyValue;
import org.springframework.beans.factory.BeanDefinitionStoreException;
import org.springframework.beans.factory.config.RuntimeBeanReference;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.EncodedResource;
import org.springframework.util.DefaultPropertiesPersister;
import org.springframework.util.PropertiesPersister;
import org.springframework.util.StringUtils;
/**
* Bean definition reader for a simple properties format.
*
* <p>Provides bean definition registration methods for Map/Properties and
* ResourceBundle. Typically applied to a DefaultListableBeanFactory.
*
* <p><b>Example:</b>
*
* <pre>
* employee.(class)=MyClass // bean is of class MyClass
* employee.(abstract)=true // this bean can't be instantiated directly
* employee.group=Insurance // real property
* employee.usesDialUp=false // real property (potentially overridden)
*
* salesrep.(parent)=employee // derives from "employee" bean definition
* salesrep.(lazy-init)=true // lazily initialize this singleton bean
* salesrep.manager(ref)=tony // reference to another bean
* salesrep.department=Sales // real property
*
* techie.(parent)=employee // derives from "employee" bean definition
* techie.(singleton)=false // bean is a prototype (not a shared instance)
* techie.manager(ref)=jeff // reference to another bean
* techie.department=Engineering // real property
* techie.usesDialUp=true // real property (overriding parent value)</pre>
*
* <em><b>Note:</b> As of Spring 1.2.6, the use of <code>class</code> and
* <code>parent</code> has been deprecated in favor of <code>(class)</code> and
* <code>(parent)</code>, for consistency with all other special properties.
* Users should note that support for <code>class</code> and <code>parent</code>
* as special properties rather then actual bean properties will be removed in a
* future version.</em>
*
* @author Rod Johnson
* @author Juergen Hoeller
* @author Rob Harrop
* @since 26.11.2003
* @see DefaultListableBeanFactory
*/
public class PropertiesBeanDefinitionReader extends AbstractBeanDefinitionReader {
/**
* Value of a T/F attribute that represents true.
* Anything else represents false. Case seNsItive.
*/
public static final String TRUE_VALUE = "true";
/**
* Separator between bean name and property name.
* We follow normal Java conventions.
*/
public static final String SEPARATOR = ".";
/**
* Special key to distinguish owner.(class)=com.myapp.MyClass
*/
public static final String CLASS_KEY = "(class)";
/**
* Special key to distinguish owner.class=com.myapp.MyClass
* Deprecated in favor of .(class)=
*/
private static final String DEPRECATED_CLASS_KEY = "class";
/**
* Special key to distinguish owner.(parent)=parentBeanName
*/
public static final String PARENT_KEY = "(parent)";
/**
* Reserved "property" to indicate the parent of a child bean definition.
* Deprecated in favor of {@link #PARENT_KEY}
*/
private static final String DEPRECATED_PARENT_KEY = "parent";
/**
* Special key to distinguish owner.(abstract)=true
* Default is "false".
*/
public static final String ABSTRACT_KEY = "(abstract)";
/**
* Special key to distinguish owner.(singleton)=true
* Default is "true".
*/
public static final String SINGLETON_KEY = "(singleton)";
/**
* Special key to distinguish owner.(lazy-init)=true
* Default is "false".
*/
public static final String LAZY_INIT_KEY = "(lazy-init)";
/**
* Property suffix for references to other beans in the current
* BeanFactory: e.g. owner.dog(ref)=fido.
* Whether this is a reference to a singleton or a prototype
* will depend on the definition of the target bean.
*/
public static final String REF_SUFFIX = "(ref)";
/**
* Prefix before values referencing other beans.
*/
public static final String REF_PREFIX = "*";
private String defaultParentBean;
private PropertiesPersister propertiesPersister = new DefaultPropertiesPersister();
/**
* Create new PropertiesBeanDefinitionReader for the given bean factory.
*/
public PropertiesBeanDefinitionReader(BeanDefinitionRegistry beanFactory) {
super(beanFactory);
}
/**
* Set the default parent bean for this bean factory.
* If a child bean definition handled by this factory provides neither
* a parent nor a class attribute, this default value gets used.
* <p>Can be used e.g. for view definition files, to define a parent
* with a default view class and common attributes for all views.
* View definitions that define their own parent or carry their own
* class can still override this.
* <p>Strictly speaking, the rule that a default parent setting does
* not apply to a bean definition that carries a class is there for
* backwards compatiblity reasons. It still matches the typical use case.
*/
public void setDefaultParentBean(String defaultParentBean) {
this.defaultParentBean = defaultParentBean;
}
/**
* Return the default parent bean for this bean factory.
*/
public String getDefaultParentBean() {
return defaultParentBean;
}
/**
* Set the PropertiesPersister to use for parsing properties files.
* The default is DefaultPropertiesPersister.
* @see org.springframework.util.DefaultPropertiesPersister
*/
public void setPropertiesPersister(PropertiesPersister propertiesPersister) {
this.propertiesPersister =
(propertiesPersister != null ? propertiesPersister : new DefaultPropertiesPersister());
}
/**
* Return the PropertiesPersister to use for parsing properties files.
*/
public PropertiesPersister getPropertiesPersister() {
return propertiesPersister;
}
/**
* Load bean definitions from the specified properties file,
* using all property keys (i.e. not filtering by prefix).
* @param resource the resource descriptor for the properties file
* @return the number of bean definitions found
* @throws BeansException in case of loading or parsing errors
* @see #loadBeanDefinitions(org.springframework.core.io.Resource, String)
*/
public int loadBeanDefinitions(Resource resource) throws BeansException {
return loadBeanDefinitions(new EncodedResource(resource), null);
}
/**
* Load bean definitions from the specified properties file.
* @param resource the resource descriptor for the properties file
* @param prefix match or filter within the keys in the map: e.g. 'beans.'
* (can be empty or <code>null</code>)
* @return the number of bean definitions found
* @throws BeansException in case of loading or parsing errors
*/
public int loadBeanDefinitions(Resource resource, String prefix) throws BeansException {
return loadBeanDefinitions(new EncodedResource(resource), prefix);
}
/**
* Load bean definitions from the specified properties file.
* @param encodedResource the resource descriptor for the properties file,
* allowing to specify an encoding to use for parsing the file
* @return the number of bean definitions found
* @throws BeansException in case of loading or parsing errors
*/
public int loadBeanDefinitions(EncodedResource encodedResource) throws BeansException {
return loadBeanDefinitions(encodedResource, null);
}
/**
* Load bean definitions from the specified properties file.
* @param encodedResource the resource descriptor for the properties file,
* allowing to specify an encoding to use for parsing the file
* @return the number of bean definitions found
* @throws BeansException in case of loading or parsing errors
*/
public int loadBeanDefinitions(EncodedResource encodedResource, String prefix) throws BeansException {
Properties props = new Properties();
try {
InputStream is = encodedResource.getResource().getInputStream();
try {
if (encodedResource.getEncoding() != null) {
getPropertiesPersister().load(props, new InputStreamReader(is, encodedResource.getEncoding()));
}
else {
getPropertiesPersister().load(props, is);
}
}
finally {
is.close();
}
return registerBeanDefinitions(props, prefix, encodedResource.getResource().getDescription());
}
catch (IOException ex) {
throw new BeanDefinitionStoreException("Could not parse properties from " + encodedResource.getResource(), ex);
}
}
/**
* Register bean definitions contained in a resource bundle,
* using all property keys (i.e. not filtering by prefix).
* @param rb the ResourceBundle to load from
* @return the number of bean definitions found
* @throws BeansException in case of loading or parsing errors
* @see #registerBeanDefinitions(java.util.ResourceBundle, String)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -