本篇文章為大家展示了spring中 PropertySource類的作用是什么,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。
創(chuàng)新互聯(lián)建站服務(wù)項(xiàng)目包括棗莊網(wǎng)站建設(shè)、棗莊網(wǎng)站制作、棗莊網(wǎng)頁制作以及棗莊網(wǎng)絡(luò)營銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢、行業(yè)經(jīng)驗(yàn)、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機(jī)構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,棗莊網(wǎng)站推廣取得了明顯的社會效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到棗莊省份的部分城市,未來相信會繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!
源碼
package org.springframework.core.env; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.lang.Nullable; import org.springframework.util.Assert; import org.springframework.util.ObjectUtils; public abstract class PropertySource{ protected final Log logger = LogFactory.getLog(getClass()); protected final String name; protected final T source; /** * Create a new {@code PropertySource} with the given name and source object. * @param name the associated name * @param source the source object */ public PropertySource(String name, T source) { Assert.hasText(name, "Property source name must contain at least one character"); Assert.notNull(source, "Property source must not be null"); this.name = name; this.source = source; } /** * Create a new {@code PropertySource} with the given name and with a new * {@code Object} instance as the underlying source. * Often useful in testing scenarios when creating anonymous implementations * that never query an actual source but rather return hard-coded values. */ @SuppressWarnings("unchecked") public PropertySource(String name) { this(name, (T) new Object()); } /** * Return the name of this {@code PropertySource}. */ public String getName() { return this.name; } /** * Return the underlying source object for this {@code PropertySource}. */ public T getSource() { return this.source; } /** * Return whether this {@code PropertySource} contains the given name. *
This implementation simply checks for a {@code null} return value * from {@link #getProperty(String)}. Subclasses may wish to implement * a more efficient algorithm if possible. * @param name the property name to find */ public boolean containsProperty(String name) { return (getProperty(name) != null); } /** * Return the value associated with the given name, * or {@code null} if not found. * @param name the property to find * @see PropertyResolver#getRequiredProperty(String) */ @Nullable public abstract Object getProperty(String name); /** * This {@code PropertySource} object is equal to the given object if: *
*
*- they are the same instance *
- the {@code name} properties for both objects are equal *
No properties other than {@code name} are evaluated. */ @Override public boolean equals(@Nullable Object other) { return (this == other || (other instanceof PropertySource && ObjectUtils.nullSafeEquals(getName(), ((PropertySource>) other).getName()))); } /** * Return a hash code derived from the {@code name} property * of this {@code PropertySource} object. */ @Override public int hashCode() { return ObjectUtils.nullSafeHashCode(getName()); } /** * Produce concise output (type and name) if the current log level does not include * debug. If debug is enabled, produce verbose output including the hash code of the * PropertySource instance and every name/value property pair. *
This variable verbosity is useful as a property source such as system properties * or environment variables may contain an arbitrary number of property pairs, * potentially leading to difficult to read exception and log messages. * @see Log#isDebugEnabled() */ @Override public String toString() { if (logger.isDebugEnabled()) { return getClass().getSimpleName() + "@" + System.identityHashCode(this) + " {name='" + getName() + "', properties=" + getSource() + "}"; } else { return getClass().getSimpleName() + " {name='" + getName() + "'}"; } } /** * Return a {@code PropertySource} implementation intended for collection comparison purposes only. *
Primarily for internal use, but given a collection of {@code PropertySource} objects, may be * used as follows: *
* {@code List* The returned {@code PropertySource} will throw {@code UnsupportedOperationException} * if any methods other than {@code equals(Object)}, {@code hashCode()}, and {@code toString()} * are called. * @param name the name of the comparison {@code PropertySource} to be created and returned. */ public static PropertySource> named(String name) { return new ComparisonPropertySource(name); } /** * {@code PropertySource} to be used as a placeholder in cases where an actual * property source cannot be eagerly initialized at application context * creation time. For example, a {@code ServletContext}-based property source * must wait until the {@code ServletContext} object is available to its enclosing * {@code ApplicationContext}. In such cases, a stub should be used to hold the * intended default position/order of the property source, then be replaced * during context refresh. * @see org.springframework.context.support.AbstractApplicationContext#initPropertySources() * @see org.springframework.web.context.support.StandardServletEnvironment * @see org.springframework.web.context.support.ServletContextPropertySource */ public static class StubPropertySource extends PropertySource> sources = new ArrayList >(); * sources.add(new MapPropertySource("sourceA", mapA)); * sources.add(new MapPropertySource("sourceB", mapB)); * assert sources.contains(PropertySource.named("sourceA")); * assert sources.contains(PropertySource.named("sourceB")); * assert !sources.contains(PropertySource.named("sourceC")); * }
PropertySource 抽象類將一個(gè)對象封裝成name/value的形式, 方便資源定位,在使用@PropertySource注解的時(shí)候,和注解name和value對應(yīng)
@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Repeatable(PropertySources.class) public @interface PropertySource { /** * Indicate the name of this property source. If omitted, the {@link #factory} * will generate a name based on the underlying resource (in the case of * {@link org.springframework.core.io.support.DefaultPropertySourceFactory}: * derived from the resource description through a corresponding name-less * {@link org.springframework.core.io.support.ResourcePropertySource} constructor). * @see org.springframework.core.env.PropertySource#getName() * @see org.springframework.core.io.Resource#getDescription() */ String name() default ""; /** * Indicate the resource location(s) of the properties file to be loaded. *Both traditional and XML-based properties file formats are supported * — for example, {@code "classpath:/com/myco/app.properties"} * or {@code "file:/path/to/file.xml"}. *
Resource location wildcards (e.g. **/*.properties) are not permitted; * each location must evaluate to exactly one {@code .properties} or {@code .xml} * resource. *
${...} placeholders will be resolved against any/all property sources already * registered with the {@code Environment}. See {@linkplain PropertySource above} * for examples. *
Each location will be added to the enclosing {@code Environment} as its own * property source, and in the order declared. */ String[] value();
需要注意的是PropertySource類有兩個(gè)內(nèi)部類,StubPropertySource和ComparisonPropertySource; StubPropertySource用于占位,而ComparisonPropertySource則用于比較兩個(gè)PropertySource. 還有一個(gè)named方法,返回ComparisonPropertySource對象.
hashcode是由name生成的,equals是比較name值, 那么現(xiàn)在判斷兩個(gè)PropertySource是否相等,只要判斷name值相等就可以了.
如果沒有ComparisonPropertySource的話,判斷PropertySource的name值是否等于一個(gè)值,需要這樣做↓↓↓
Mapmap1 = new HashMap () {{ put("a", "b"); }}; MapPropertySource mp1 = new MapPropertySource("mps", map1);
System.out.println(mp1.getName().equals("mps"));
有named方法和ComparisonPropertySource之后,就可以這樣比較
System.out.println(mp1.equals(PropertySource.named("mps")));
好處是啥? 前者是字符串的比較,后者是對象的比較, 看他用在什么地方
PropertySource identity is determined not based on the content of encapsulated properties, but rather based on the name of the PropertySource alone. This is useful for manipulating PropertySource objects when in collection contexts. PropertySource的定位不是封裝數(shù)據(jù),而是對數(shù)據(jù)進(jìn)行命名,以及方法在集合中操作.
如果是為了方便在集合中操作,那么就很有用了,如果你沒有named方法,你必須要遍歷List的數(shù)據(jù),一個(gè)一個(gè)取出name值判斷; 有了named方法后,現(xiàn)在你可以直接使用Collection中的方法來判斷數(shù)據(jù)存不存在,刪除數(shù)據(jù)等↓↓↓
List> propertySources = new ArrayList<>(); propertySources.contains(PropertySource.named("ps1")); propertySources.remove(PropertySource.named("ps1"))
上述內(nèi)容就是spring中 PropertySource類的作用是什么,你們學(xué)到知識或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識儲備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。